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