diff options
author | Chocobozzz <me@florianbigard.com> | 2020-07-07 14:34:16 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-07-10 14:02:41 +0200 |
commit | 4f32032fed8587ea97d45e235b167e8958efd81f (patch) | |
tree | 6ec4ce2d5d05a275fb7eeaf3aee33c2b69ebc043 /server/initializers/migrations/0520-abuses-split.ts | |
parent | 57f6896f67cfc570cf3605dd94b0778101b2d9b9 (diff) | |
download | PeerTube-4f32032fed8587ea97d45e235b167e8958efd81f.tar.gz PeerTube-4f32032fed8587ea97d45e235b167e8958efd81f.tar.zst PeerTube-4f32032fed8587ea97d45e235b167e8958efd81f.zip |
Add migrations
Diffstat (limited to 'server/initializers/migrations/0520-abuses-split.ts')
-rw-r--r-- | server/initializers/migrations/0520-abuses-split.ts | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/server/initializers/migrations/0520-abuses-split.ts b/server/initializers/migrations/0520-abuses-split.ts new file mode 100644 index 000000000..5898d501f --- /dev/null +++ b/server/initializers/migrations/0520-abuses-split.ts | |||
@@ -0,0 +1,92 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
3 | async function up (utils: { | ||
4 | transaction: Sequelize.Transaction | ||
5 | queryInterface: Sequelize.QueryInterface | ||
6 | sequelize: Sequelize.Sequelize | ||
7 | }): Promise<void> { | ||
8 | await utils.queryInterface.renameTable('videoAbuse', 'abuse') | ||
9 | |||
10 | await utils.sequelize.query(` | ||
11 | ALTER TABLE "abuse" | ||
12 | ADD COLUMN "flaggedAccountId" INTEGER REFERENCES "account" ("id") ON DELETE SET NULL ON UPDATE CASCADE | ||
13 | `) | ||
14 | |||
15 | await utils.sequelize.query(` | ||
16 | UPDATE "abuse" SET "videoId" = NULL | ||
17 | WHERE "videoId" NOT IN (SELECT "id" FROM "video") | ||
18 | `) | ||
19 | |||
20 | await utils.sequelize.query(` | ||
21 | UPDATE "abuse" SET "flaggedAccountId" = "videoChannel"."accountId" | ||
22 | FROM "video" INNER JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id" | ||
23 | WHERE "abuse"."videoId" = "video"."id" | ||
24 | `) | ||
25 | |||
26 | await utils.sequelize.query('DROP INDEX IF EXISTS video_abuse_video_id;') | ||
27 | await utils.sequelize.query('DROP INDEX IF EXISTS video_abuse_reporter_account_id;') | ||
28 | |||
29 | await utils.sequelize.query(` | ||
30 | CREATE TABLE IF NOT EXISTS "videoAbuse" ( | ||
31 | "id" serial, | ||
32 | "startAt" integer DEFAULT NULL, | ||
33 | "endAt" integer DEFAULT NULL, | ||
34 | "deletedVideo" jsonb DEFAULT NULL, | ||
35 | "abuseId" integer NOT NULL REFERENCES "abuse" ("id") ON DELETE CASCADE ON UPDATE CASCADE, | ||
36 | "videoId" integer REFERENCES "video" ("id") ON DELETE SET NULL ON UPDATE CASCADE, | ||
37 | "createdAt" TIMESTAMP WITH time zone NOT NULL, | ||
38 | "updatedAt" timestamp WITH time zone NOT NULL, | ||
39 | PRIMARY KEY ("id") | ||
40 | ); | ||
41 | `) | ||
42 | |||
43 | await utils.sequelize.query(` | ||
44 | CREATE TABLE IF NOT EXISTS "commentAbuse" ( | ||
45 | "id" serial, | ||
46 | "deletedComment" jsonb DEFAULT NULL, | ||
47 | "abuseId" integer NOT NULL REFERENCES "abuse" ("id") ON DELETE CASCADE ON UPDATE CASCADE, | ||
48 | "videoCommentId" integer REFERENCES "videoComment" ("id") ON DELETE SET NULL ON UPDATE CASCADE, | ||
49 | "createdAt" timestamp WITH time zone NOT NULL, | ||
50 | "updatedAt" timestamp WITH time zone NOT NULL, | ||
51 | "commentId" integer REFERENCES "videoComment" ("id") ON DELETE SET NULL ON UPDATE CASCADE, | ||
52 | PRIMARY KEY ("id") | ||
53 | ); | ||
54 | `) | ||
55 | |||
56 | await utils.sequelize.query(` | ||
57 | INSERT INTO "videoAbuse" ("startAt", "endAt", "deletedVideo", "abuseId", "videoId", "createdAt", "updatedAt") | ||
58 | SELECT "abuse"."startAt", "abuse"."endAt", "abuse"."deletedVideo", "abuse"."id", "abuse"."videoId", | ||
59 | "abuse"."createdAt", "abuse"."updatedAt" | ||
60 | FROM "abuse" | ||
61 | `) | ||
62 | |||
63 | await utils.queryInterface.removeColumn('abuse', 'startAt') | ||
64 | await utils.queryInterface.removeColumn('abuse', 'endAt') | ||
65 | await utils.queryInterface.removeColumn('abuse', 'deletedVideo') | ||
66 | await utils.queryInterface.removeColumn('abuse', 'videoId') | ||
67 | |||
68 | await utils.sequelize.query('DROP INDEX IF EXISTS user_notification_video_abuse_id') | ||
69 | await utils.queryInterface.renameColumn('userNotification', 'videoAbuseId', 'abuseId') | ||
70 | await utils.sequelize.query( | ||
71 | 'ALTER TABLE "userNotification" RENAME CONSTRAINT "userNotification_videoAbuseId_fkey" TO "userNotification_abuseId_fkey"' | ||
72 | ) | ||
73 | |||
74 | await utils.sequelize.query( | ||
75 | 'ALTER TABLE "abuse" RENAME CONSTRAINT "videoAbuse_reporterAccountId_fkey" TO "abuse_reporterAccountId_fkey"' | ||
76 | ) | ||
77 | |||
78 | await utils.sequelize.query( | ||
79 | 'ALTER INDEX IF EXISTS "videoAbuse_pkey" RENAME TO "abuse_pkey"' | ||
80 | ) | ||
81 | |||
82 | await utils.queryInterface.renameColumn('userNotificationSetting', 'videoAbuseAsModerator', 'abuseAsModerator') | ||
83 | } | ||
84 | |||
85 | function down (options) { | ||
86 | throw new Error('Not implemented.') | ||
87 | } | ||
88 | |||
89 | export { | ||
90 | up, | ||
91 | down | ||
92 | } | ||