summaryrefslogtreecommitdiff
path: root/Migrations/20260131125650_InitialMigration.cs
blob: a1a7d8f4cd565ac9fa3dcf2f797eb8826105f77f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace HyperBooru.Migrations
{
    /// <inheritdoc />
    public partial class InitialMigration : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Objects",
                columns: table => new
                {
                    ObjectId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Guid = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Objects", x => x.ObjectId);
                });

            migrationBuilder.CreateTable(
                name: "TagDefinitions",
                columns: table => new
                {
                    ObjectId = table.Column<int>(type: "integer", nullable: false),
                    Source = table.Column<int>(type: "integer", nullable: false),
                    Namespace = table.Column<string>(type: "text", nullable: true),
                    Name = table.Column<string>(type: "text", nullable: false),
                    Alias = table.Column<string>(type: "text", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TagDefinitions", x => x.ObjectId);
                    table.ForeignKey(
                        name: "FK_TagDefinitions_Objects_ObjectId",
                        column: x => x.ObjectId,
                        principalTable: "Objects",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "Users",
                columns: table => new
                {
                    ObjectId = table.Column<int>(type: "integer", nullable: false),
                    Username = table.Column<string>(type: "text", nullable: false),
                    PasswordHash = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Users", x => x.ObjectId);
                    table.ForeignKey(
                        name: "FK_Users_Objects_ObjectId",
                        column: x => x.ObjectId,
                        principalTable: "Objects",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "TagDefinitionTagDefinition",
                columns: table => new
                {
                    ImplicitTagsObjectId = table.Column<int>(type: "integer", nullable: false),
                    TagDefinitionObjectId = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TagDefinitionTagDefinition", x => new { x.ImplicitTagsObjectId, x.TagDefinitionObjectId });
                    table.ForeignKey(
                        name: "FK_TagDefinitionTagDefinition_TagDefinitions_ImplicitTagsObjec~",
                        column: x => x.ImplicitTagsObjectId,
                        principalTable: "TagDefinitions",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_TagDefinitionTagDefinition_TagDefinitions_TagDefinitionObje~",
                        column: x => x.TagDefinitionObjectId,
                        principalTable: "TagDefinitions",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "Tags",
                columns: table => new
                {
                    ObjectId = table.Column<int>(type: "integer", nullable: false),
                    TagDefinitionId = table.Column<int>(type: "integer", nullable: false),
                    CreateTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    TargetObjectId = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Tags", x => x.ObjectId);
                    table.ForeignKey(
                        name: "FK_Tags_Objects_ObjectId",
                        column: x => x.ObjectId,
                        principalTable: "Objects",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_Tags_Objects_TargetObjectId",
                        column: x => x.TargetObjectId,
                        principalTable: "Objects",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_Tags_TagDefinitions_TagDefinitionId",
                        column: x => x.TagDefinitionId,
                        principalTable: "TagDefinitions",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "Media",
                columns: table => new
                {
                    ObjectId = table.Column<int>(type: "integer", nullable: false),
                    ShortDescription = table.Column<string>(type: "text", nullable: true),
                    LongDescription = table.Column<string>(type: "text", nullable: true),
                    CurrentUploadedFileId = table.Column<int>(type: "integer", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Media", x => x.ObjectId);
                    table.ForeignKey(
                        name: "FK_Media_Objects_ObjectId",
                        column: x => x.ObjectId,
                        principalTable: "Objects",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "OcrData",
                columns: table => new
                {
                    OcrDataId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    MediaId = table.Column<int>(type: "integer", nullable: false),
                    Text = table.Column<string>(type: "text", nullable: false),
                    SearchableText = table.Column<string>(type: "text", nullable: false),
                    Timestamp = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OcrData", x => x.OcrDataId);
                    table.ForeignKey(
                        name: "FK_OcrData_Media_MediaId",
                        column: x => x.MediaId,
                        principalTable: "Media",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "UploadedFiles",
                columns: table => new
                {
                    ObjectId = table.Column<int>(type: "integer", nullable: false),
                    Checksum = table.Column<string>(type: "text", nullable: false),
                    ChecksumVerified = table.Column<bool>(type: "boolean", nullable: false),
                    Filename = table.Column<string>(type: "text", nullable: true),
                    Length = table.Column<long>(type: "bigint", nullable: false),
                    MimeType = table.Column<string>(type: "text", nullable: false),
                    Width = table.Column<int>(type: "integer", nullable: true),
                    Height = table.Column<int>(type: "integer", nullable: true),
                    UploadTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    LastAccessTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    LastWriteTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    CreateTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    Path = table.Column<string>(type: "text", nullable: true),
                    PathType = table.Column<int>(type: "integer", nullable: true),
                    MediaObjectId = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UploadedFiles", x => x.ObjectId);
                    table.ForeignKey(
                        name: "FK_UploadedFiles_Media_MediaObjectId",
                        column: x => x.MediaObjectId,
                        principalTable: "Media",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_UploadedFiles_Objects_ObjectId",
                        column: x => x.ObjectId,
                        principalTable: "Objects",
                        principalColumn: "ObjectId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                table: "Objects",
                columns: new[] { "ObjectId", "Guid" },
                values: new object[,]
                {
                    { -3, new Guid("4fa948f4-7c45-4f81-bb6b-e417491e6c96") },
                    { -2, new Guid("ea212801-5bcc-4c0e-814f-fb9d30db58bc") },
                    { -1, new Guid("ebdad4f8-455a-4351-8017-1d4854d6fa38") }
                });

            migrationBuilder.InsertData(
                table: "TagDefinitions",
                columns: new[] { "ObjectId", "Alias", "Name", "Namespace", "Source" },
                values: new object[,]
                {
                    { -2, null, "ingest", null, 0 },
                    { -1, null, "nsfw", null, 0 }
                });

            migrationBuilder.InsertData(
                table: "Users",
                columns: new[] { "ObjectId", "PasswordHash", "Username" },
                values: new object[] { -3, "P4geAuE2yX/PDRHuJSq74FF5vO782rWz5c0LAQPR8m45DEYAONhu1wYnAn60PSNyjocqEBdnCeKCJfK3sKyuWw==", "admin" });

            migrationBuilder.CreateIndex(
                name: "IX_Media_CurrentUploadedFileId",
                table: "Media",
                column: "CurrentUploadedFileId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Objects_Guid",
                table: "Objects",
                column: "Guid");

            migrationBuilder.CreateIndex(
                name: "IX_OcrData_MediaId",
                table: "OcrData",
                column: "MediaId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_TagDefinitionTagDefinition_TagDefinitionObjectId",
                table: "TagDefinitionTagDefinition",
                column: "TagDefinitionObjectId");

            migrationBuilder.CreateIndex(
                name: "IX_Tags_TagDefinitionId",
                table: "Tags",
                column: "TagDefinitionId");

            migrationBuilder.CreateIndex(
                name: "IX_Tags_TargetObjectId",
                table: "Tags",
                column: "TargetObjectId");

            migrationBuilder.CreateIndex(
                name: "IX_UploadedFiles_MediaObjectId",
                table: "UploadedFiles",
                column: "MediaObjectId");

            migrationBuilder.CreateIndex(
                name: "IX_Users_Username",
                table: "Users",
                column: "Username");

            migrationBuilder.AddForeignKey(
                name: "FK_Media_UploadedFiles_CurrentUploadedFileId",
                table: "Media",
                column: "CurrentUploadedFileId",
                principalTable: "UploadedFiles",
                principalColumn: "ObjectId");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Media_Objects_ObjectId",
                table: "Media");

            migrationBuilder.DropForeignKey(
                name: "FK_UploadedFiles_Objects_ObjectId",
                table: "UploadedFiles");

            migrationBuilder.DropForeignKey(
                name: "FK_Media_UploadedFiles_CurrentUploadedFileId",
                table: "Media");

            migrationBuilder.DropTable(
                name: "OcrData");

            migrationBuilder.DropTable(
                name: "TagDefinitionTagDefinition");

            migrationBuilder.DropTable(
                name: "Tags");

            migrationBuilder.DropTable(
                name: "Users");

            migrationBuilder.DropTable(
                name: "TagDefinitions");

            migrationBuilder.DropTable(
                name: "Objects");

            migrationBuilder.DropTable(
                name: "UploadedFiles");

            migrationBuilder.DropTable(
                name: "Media");
        }
    }
}