aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrations/0055-video-uuid.ts
blob: 8a58aebb838db4f607182811dc92a58f298e49e8 (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
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
import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird'
import { Migration } from '../../models/migrations'

function up (utils: {
  transaction: Sequelize.Transaction
  queryInterface: Sequelize.QueryInterface
  sequelize: Sequelize.Sequelize
}): Promise<void> {
  const q = utils.queryInterface

  const dataUUID = {
    type: Sequelize.UUID,
    defaultValue: Sequelize.UUIDV4,
    allowNull: true
  } as Migration.UUID

  return q.addColumn('Videos', 'uuid', dataUUID)
    .then(() => {
      const query = 'UPDATE "Videos" SET "uuid" = "id" WHERE "remoteId" IS NULL'
      return utils.sequelize.query(query)
    })
    .then(() => {
      const query = 'UPDATE "Videos" SET "uuid" = "remoteId" WHERE "remoteId" IS NOT NULL'
      return utils.sequelize.query(query)
    })
    .then(() => {
      dataUUID.defaultValue = null

      return q.changeColumn('Videos', 'uuid', dataUUID)
    })
    .then(() => {
      return removeForeignKey(utils.sequelize, 'RequestVideoQadus')
    })
    .then(() => {
      return removeForeignKey(utils.sequelize, 'RequestVideoEvents')
    })
    .then(() => {
      return removeForeignKey(utils.sequelize, 'BlacklistedVideos')
    })
    .then(() => {
      return removeForeignKey(utils.sequelize, 'UserVideoRates')
    })
    .then(() => {
      return removeForeignKey(utils.sequelize, 'VideoAbuses')
    })
    .then(() => {
      return removeForeignKey(utils.sequelize, 'VideoTags')
    })
    .then(() => {
      const query = 'ALTER TABLE "Videos" DROP CONSTRAINT "Videos_pkey"'
      return utils.sequelize.query(query)
    })
    .then(() => {
      const query = 'ALTER TABLE "Videos" ADD COLUMN "id2" SERIAL PRIMARY KEY'
      return utils.sequelize.query(query)
    })
    .then(() => {
      return q.renameColumn('Videos', 'id', 'oldId')
    })
    .then(() => {
      return q.renameColumn('Videos', 'id2', 'id')
    })
    .then(() => {
      return changeForeignKey(q, utils.sequelize, 'RequestVideoQadus', false)
    })
    .then(() => {
      return changeForeignKey(q, utils.sequelize, 'RequestVideoEvents', false)
    })
    .then(() => {
      return changeForeignKey(q, utils.sequelize, 'BlacklistedVideos', false)
    })
    .then(() => {
      return changeForeignKey(q, utils.sequelize, 'UserVideoRates', false)
    })
    .then(() => {
      return changeForeignKey(q, utils.sequelize, 'VideoAbuses', false)
    })
    .then(() => {
      return changeForeignKey(q, utils.sequelize, 'VideoTags', true)
    })
    .then(() => {
      return q.removeColumn('Videos', 'oldId')
    })
    .then(() => {
      const dataRemote = {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
        allowNull: false
      }
      return q.addColumn('Videos', 'remote', dataRemote)
    })
    .then(() => {
      const query = 'UPDATE "Videos" SET "remote" = false WHERE "remoteId" IS NULL'
      return utils.sequelize.query(query)
    })
    .then(() => {
      const query = 'UPDATE "Videos" SET "remote" = true WHERE "remoteId" IS NOT NULL'
      return utils.sequelize.query(query)
    })
    .then(() => {
      return q.removeColumn('Videos', 'remoteId')
    })
}

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

function removeForeignKey (sequelize: Sequelize.Sequelize, tableName: string) {
  const query = 'ALTER TABLE "' + tableName + '" DROP CONSTRAINT "' + tableName + '_videoId_fkey' + '"'
  return sequelize.query(query)
}

function changeForeignKey (q: Sequelize.QueryInterface, sequelize: Sequelize.Sequelize, tableName: string, allowNull: boolean) {
  const data = {
    type: Sequelize.INTEGER,
    allowNull: true
  }

  return q.addColumn(tableName, 'videoId2', data)
    .then(() => {
      const query = 'UPDATE "' + tableName + '" SET "videoId2" = ' +
                    '(SELECT "id" FROM "Videos" WHERE "' + tableName + '"."videoId" = "Videos"."oldId")'
      return sequelize.query(query)
    })
    .then(() => {
      if (allowNull === false) {
        data.allowNull = false

        return q.changeColumn(tableName, 'videoId2', data)
      }

      return Promise.resolve()
    })
    .then(() => {
      return q.removeColumn(tableName, 'videoId')
    })
    .then(() => {
      return q.renameColumn(tableName, 'videoId2', 'videoId')
    })
    .then(() => {
      return q.addIndex(tableName, [ 'videoId' ])
    })
    .then(() => {
      const constraintName = tableName + '_videoId_fkey'
      const query = 'ALTER TABLE "' + tableName + '" ' +
                    ' ADD CONSTRAINT "' + constraintName + '"' +
                    ' FOREIGN KEY ("videoId") REFERENCES "Videos" ON DELETE CASCADE'

      return sequelize.query(query)
    })
}

export {
  up,
  down
}