tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
20201214182008_PGAddUserGroups.cs
Go to the documentation of this file.
1using System;
2
3using Microsoft.EntityFrameworkCore.Migrations;
4using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
5
7{
11 public partial class PGAddUserGroups : Migration
12 {
14 protected override void Up(MigrationBuilder migrationBuilder)
15 {
16 ArgumentNullException.ThrowIfNull(migrationBuilder);
17
18 migrationBuilder.AlterColumn<string>(
19 name: "SystemIdentifier",
20 table: "Users",
21 maxLength: 100,
22 nullable: true,
23 oldClrType: typeof(string),
24 oldType: "text",
25 oldNullable: true);
26
27 migrationBuilder.AlterColumn<string>(
28 name: "Name",
29 table: "Users",
30 maxLength: 100,
31 nullable: false,
32 oldClrType: typeof(string),
33 oldType: "character varying(10000)",
34 oldMaxLength: 10000);
35
36 migrationBuilder.AlterColumn<string>(
37 name: "CanonicalName",
38 table: "Users",
39 maxLength: 100,
40 nullable: false,
41 oldClrType: typeof(string),
42 oldType: "text");
43
44 migrationBuilder.AddColumn<long>(
45 name: "GroupId",
46 table: "Users",
47 nullable: true);
48
49 migrationBuilder.CreateTable(
50 name: "Groups",
51 columns: table => new
52 {
53 Id = table.Column<long>(nullable: false)
54 .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
55 Name = table.Column<string>(maxLength: 100, nullable: false),
56 },
57 constraints: table =>
58 {
59 table.PrimaryKey("PK_Groups", x => x.Id);
60 });
61
62 migrationBuilder.CreateTable(
63 name: "PermissionSets",
64 columns: table => new
65 {
66 Id = table.Column<long>(nullable: false)
67 .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
68 AdministrationRights = table.Column<decimal>(nullable: false),
69 InstanceManagerRights = table.Column<decimal>(nullable: false),
70 UserId = table.Column<long>(nullable: true),
71 GroupId = table.Column<long>(nullable: true),
72 },
73 constraints: table =>
74 {
75 table.PrimaryKey("PK_PermissionSets", x => x.Id);
76 table.ForeignKey(
77 name: "FK_PermissionSets_Groups_GroupId",
78 column: x => x.GroupId,
79 principalTable: "Groups",
80 principalColumn: "Id",
81 onDelete: ReferentialAction.Cascade);
82 table.ForeignKey(
83 name: "FK_PermissionSets_Users_UserId",
84 column: x => x.UserId,
85 principalTable: "Users",
86 principalColumn: "Id",
87 onDelete: ReferentialAction.Cascade);
88 });
89
90 migrationBuilder.CreateTable(
91 name: "InstancePermissionSets",
92 columns: table => new
93 {
94 Id = table.Column<long>(nullable: false)
95 .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
96 PermissionSetId = table.Column<long>(nullable: false),
97 InstancePermissionSetRights = table.Column<decimal>(nullable: false),
98 ByondRights = table.Column<decimal>(nullable: false),
99 DreamDaemonRights = table.Column<decimal>(nullable: false),
100 DreamMakerRights = table.Column<decimal>(nullable: false),
101 RepositoryRights = table.Column<decimal>(nullable: false),
102 ChatBotRights = table.Column<decimal>(nullable: false),
103 ConfigurationRights = table.Column<decimal>(nullable: false),
104 InstanceId = table.Column<long>(nullable: false),
105 },
106 constraints: table =>
107 {
108 table.PrimaryKey("PK_InstancePermissionSets", x => x.Id);
109 table.ForeignKey(
110 name: "FK_InstancePermissionSets_Instances_InstanceId",
111 column: x => x.InstanceId,
112 principalTable: "Instances",
113 principalColumn: "Id",
114 onDelete: ReferentialAction.Cascade);
115 table.ForeignKey(
116 name: "FK_InstancePermissionSets_PermissionSets_PermissionSetId",
117 column: x => x.PermissionSetId,
118 principalTable: "PermissionSets",
119 principalColumn: "Id",
120 onDelete: ReferentialAction.Cascade);
121 });
122
123 migrationBuilder.CreateIndex(
124 name: "IX_Users_GroupId",
125 table: "Users",
126 column: "GroupId");
127
128 migrationBuilder.CreateIndex(
129 name: "IX_Groups_Name",
130 table: "Groups",
131 column: "Name",
132 unique: true);
133
134 migrationBuilder.CreateIndex(
135 name: "IX_InstancePermissionSets_InstanceId",
136 table: "InstancePermissionSets",
137 column: "InstanceId");
138
139 migrationBuilder.CreateIndex(
140 name: "IX_InstancePermissionSets_PermissionSetId_InstanceId",
141 table: "InstancePermissionSets",
142 columns: new[] { "PermissionSetId", "InstanceId" },
143 unique: true);
144
145 migrationBuilder.CreateIndex(
146 name: "IX_PermissionSets_GroupId",
147 table: "PermissionSets",
148 column: "GroupId",
149 unique: true);
150
151 migrationBuilder.CreateIndex(
152 name: "IX_PermissionSets_UserId",
153 table: "PermissionSets",
154 column: "UserId",
155 unique: true);
156
157 migrationBuilder.AddForeignKey(
158 name: "FK_Users_Groups_GroupId",
159 table: "Users",
160 column: "GroupId",
161 principalTable: "Groups",
162 principalColumn: "Id",
163 onDelete: ReferentialAction.Restrict);
164
165 migrationBuilder.Sql(
166 "INSERT INTO \"PermissionSets\" (\"UserId\", \"AdministrationRights\", \"InstanceManagerRights\") SELECT \"Id\", \"AdministrationRights\", \"InstanceManagerRights\" FROM \"Users\"");
167
168 migrationBuilder.Sql(
169 "INSERT INTO \"InstancePermissionSets\" (\"PermissionSetId\", \"InstanceId\", \"InstancePermissionSetRights\", \"ByondRights\", \"DreamDaemonRights\", \"DreamMakerRights\", \"RepositoryRights\", \"ChatBotRights\", \"ConfigurationRights\") SELECT p.\"Id\", iu.\"InstanceId\", iu.\"InstanceUserRights\", iu.\"ByondRights\", iu.\"DreamDaemonRights\", iu.\"DreamMakerRights\", iu.\"RepositoryRights\", iu.\"ChatBotRights\", iu.\"ConfigurationRights\" FROM \"InstanceUsers\" iu JOIN \"PermissionSets\" p ON iu.\"UserId\" = p.\"UserId\"");
170
171 migrationBuilder.DropTable(
172 name: "InstanceUsers");
173
174 migrationBuilder.DropColumn(
175 name: "AdministrationRights",
176 table: "Users");
177
178 migrationBuilder.DropColumn(
179 name: "InstanceManagerRights",
180 table: "Users");
181 }
182
184 protected override void Down(MigrationBuilder migrationBuilder)
185 {
186 ArgumentNullException.ThrowIfNull(migrationBuilder);
187
188 migrationBuilder.AlterColumn<string>(
189 name: "SystemIdentifier",
190 table: "Users",
191 type: "text",
192 nullable: true,
193 oldClrType: typeof(string),
194 oldMaxLength: 100,
195 oldNullable: true);
196
197 migrationBuilder.AlterColumn<string>(
198 name: "Name",
199 table: "Users",
200 type: "character varying(10000)",
201 maxLength: 10000,
202 nullable: false,
203 oldClrType: typeof(string),
204 oldMaxLength: 100);
205
206 migrationBuilder.AlterColumn<string>(
207 name: "CanonicalName",
208 table: "Users",
209 type: "text",
210 nullable: false,
211 oldClrType: typeof(string),
212 oldMaxLength: 100);
213
214 migrationBuilder.AddColumn<decimal>(
215 name: "AdministrationRights",
216 table: "Users",
217 type: "numeric(20,0)",
218 nullable: false,
219 defaultValue: 0m);
220
221 migrationBuilder.AddColumn<decimal>(
222 name: "InstanceManagerRights",
223 table: "Users",
224 type: "numeric(20,0)",
225 nullable: false,
226 defaultValue: 0m);
227
228 migrationBuilder.CreateTable(
229 name: "InstanceUsers",
230 columns: table => new
231 {
232 Id = table.Column<long>(type: "bigint", nullable: false)
233 .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
234 ByondRights = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
235 ChatBotRights = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
236 ConfigurationRights = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
237 DreamDaemonRights = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
238 DreamMakerRights = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
239 InstanceId = table.Column<long>(type: "bigint", nullable: false),
240 InstanceUserRights = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
241 RepositoryRights = table.Column<decimal>(type: "numeric(20,0)", nullable: false),
242 UserId = table.Column<long>(type: "bigint", nullable: false),
243 },
244 constraints: table =>
245 {
246 table.PrimaryKey("PK_InstanceUsers", x => x.Id);
247 table.ForeignKey(
248 name: "FK_InstanceUsers_Instances_InstanceId",
249 column: x => x.InstanceId,
250 principalTable: "Instances",
251 principalColumn: "Id",
252 onDelete: ReferentialAction.Cascade);
253 table.ForeignKey(
254 name: "FK_InstanceUsers_Users_UserId",
255 column: x => x.UserId,
256 principalTable: "Users",
257 principalColumn: "Id",
258 onDelete: ReferentialAction.Cascade);
259 });
260
261 migrationBuilder.CreateIndex(
262 name: "IX_InstanceUsers_InstanceId",
263 table: "InstanceUsers",
264 column: "InstanceId");
265
266 migrationBuilder.CreateIndex(
267 name: "IX_InstanceUsers_UserId_InstanceId",
268 table: "InstanceUsers",
269 columns: new[] { "UserId", "InstanceId" },
270 unique: true);
271
272 migrationBuilder.Sql(
273 "UPDATE \"Users\" SET \"AdministrationRights\" = p.\"AdministrationRights\", \"InstanceManagerRights\" = p.\"InstanceManagerRights\" FROM \"Users\" u JOIN \"PermissionSets\" p ON u.\"Id\" = p.\"UserId\" WHERE p.\"UserId\" != NULL");
274
275 migrationBuilder.Sql(
276 "UPDATE \"Users\" SET \"AdministrationRights\" = p.\"AdministrationRights\", \"InstanceManagerRights\" = p.\"InstanceManagerRights\" FROM \"Users\" u JOIN \"PermissionSets\" p ON u.\"GroupId\" = p.\"GroupId\" WHERE p.\"GroupId\" != NULL");
277
278 migrationBuilder.Sql(
279 "INSERT INTO \"InstanceUsers\" (\"UserId\", \"InstanceId\", \"InstanceUserRights\", \"ByondRights\", \"DreamDaemonRights\", \"DreamMakerRights\", \"RepositoryRights\", \"ChatBotRights\", \"ConfigurationRights\") SELECT p.\"UserId\", ips.\"InstanceId\", ips.\"InstancePermissionSetRights\", ips.\"ByondRights\", ips.\"DreamDaemonRights\", ips.\"DreamMakerRights\", ips.\"RepositoryRights\", ips.\"ChatBotRights\", ips.\"ConfigurationRights\" FROM \"InstancePermissionSets\" ips JOIN \"PermissionSets\" p ON ips.\"PermissionSetId\" = p.\"Id\" WHERE p.\"UserId\" != NULL");
280
281 migrationBuilder.Sql(
282 "INSERT INTO \"InstanceUsers\" (\"UserId\", \"InstanceId\", \"InstanceUserRights\", \"ByondRights\", \"DreamDaemonRights\", \"DreamMakerRights\", \"RepositoryRights\", \"ChatBotRights\", \"ConfigurationRights\") SELECT u.\"Id\", ips.\"InstanceId\", ips.\"InstancePermissionSetRights\", ips.\"ByondRights\", ips.\"DreamDaemonRights\", ips.\"DreamMakerRights\", ips.\"RepositoryRights\", ips.\"ChatBotRights\", ips.\"ConfigurationRights\" FROM \"InstancePermissionSets\" ips JOIN \"PermissionSets\" p ON ips.\"PermissionSetId\" = p.\"Id\" JOIN \"Users\" u ON p.\"GroupId\" = u.\"GroupId\" WHERE p.\"GroupId\" != NULL");
283
284 migrationBuilder.DropForeignKey(
285 name: "FK_Users_Groups_GroupId",
286 table: "Users");
287
288 migrationBuilder.DropTable(
289 name: "InstancePermissionSets");
290
291 migrationBuilder.DropTable(
292 name: "PermissionSets");
293
294 migrationBuilder.DropTable(
295 name: "Groups");
296
297 migrationBuilder.DropIndex(
298 name: "IX_Users_GroupId",
299 table: "Users");
300
301 migrationBuilder.DropColumn(
302 name: "GroupId",
303 table: "Users");
304 }
305 }
306}