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