aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrations/0520-abuses-split.ts
blob: b02a21989b251c864c66ad38e68db6c88df60cdf (plain) (blame)
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
import * as Sequelize from 'sequelize'

async function up (utils: {
  transaction: Sequelize.Transaction
  queryInterface: Sequelize.QueryInterface
  sequelize: Sequelize.Sequelize
}): Promise<void> {
  await utils.queryInterface.renameTable('videoAbuse', 'abuse')

  await utils.sequelize.query(`
    ALTER TABLE "abuse"
    ADD COLUMN "flaggedAccountId" INTEGER REFERENCES "account" ("id") ON DELETE SET NULL ON UPDATE CASCADE
  `)

  await utils.sequelize.query(`
    UPDATE "abuse" SET "videoId" = NULL
    WHERE "videoId" NOT IN (SELECT "id" FROM "video")
  `)

  await utils.sequelize.query(`
    UPDATE "abuse" SET "flaggedAccountId" = "videoChannel"."accountId"
    FROM "video" INNER JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id"
    WHERE "abuse"."videoId" = "video"."id"
  `)

  await utils.sequelize.query('DROP INDEX IF EXISTS video_abuse_video_id;')
  await utils.sequelize.query('DROP INDEX IF EXISTS video_abuse_reporter_account_id;')

  await utils.sequelize.query(`
    CREATE TABLE IF NOT EXISTS "videoAbuse" (
      "id" serial,
      "startAt" integer DEFAULT NULL,
      "endAt" integer DEFAULT NULL,
      "deletedVideo" jsonb DEFAULT NULL,
      "abuseId" integer NOT NULL REFERENCES "abuse" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
      "videoId" integer REFERENCES "video" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
      "createdAt" TIMESTAMP WITH time zone NOT NULL,
      "updatedAt" timestamp WITH time zone NOT NULL,
      PRIMARY KEY ("id")
    );
  `)

  await utils.sequelize.query(`
    CREATE TABLE IF NOT EXISTS "commentAbuse" (
      "id" serial,
      "abuseId" integer NOT NULL REFERENCES "abuse" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
      "videoCommentId" integer REFERENCES "videoComment" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
      "createdAt" timestamp WITH time zone NOT NULL,
      "updatedAt" timestamp WITH time zone NOT NULL,
      PRIMARY KEY ("id")
    );
  `)

  await utils.sequelize.query(`
      INSERT INTO "videoAbuse" ("startAt", "endAt", "deletedVideo", "abuseId", "videoId", "createdAt", "updatedAt")
      SELECT "abuse"."startAt", "abuse"."endAt", "abuse"."deletedVideo", "abuse"."id", "abuse"."videoId",
      "abuse"."createdAt", "abuse"."updatedAt"
      FROM "abuse"
  `)

  await utils.queryInterface.removeColumn('abuse', 'startAt')
  await utils.queryInterface.removeColumn('abuse', 'endAt')
  await utils.queryInterface.removeColumn('abuse', 'deletedVideo')
  await utils.queryInterface.removeColumn('abuse', 'videoId')

  await utils.sequelize.query('DROP INDEX IF EXISTS user_notification_video_abuse_id')
  await utils.queryInterface.renameColumn('userNotification', 'videoAbuseId', 'abuseId')
  await utils.sequelize.query(
    'ALTER TABLE "userNotification" RENAME CONSTRAINT "userNotification_videoAbuseId_fkey" TO "userNotification_abuseId_fkey"'
  )

  await utils.sequelize.query(
    'ALTER TABLE "abuse" RENAME CONSTRAINT "videoAbuse_reporterAccountId_fkey" TO "abuse_reporterAccountId_fkey"'
  )

  await utils.sequelize.query(
    'ALTER INDEX IF EXISTS "videoAbuse_pkey" RENAME TO "abuse_pkey"'
  )

  await utils.queryInterface.renameColumn('userNotificationSetting', 'videoAbuseAsModerator', 'abuseAsModerator')
}

function down (options) {
  throw new Error('Not implemented.')
}

export {
  up,
  down
}