From 0a6658fdcbd779ada8f3758048c326e997902d5a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 11 Jul 2017 16:01:56 +0200 Subject: Use global uuid instead of remoteId for videos --- server/initializers/migrations/0005-email-pod.ts | 2 +- server/initializers/migrations/0010-email-user.ts | 2 +- server/initializers/migrations/0015-video-views.ts | 2 +- server/initializers/migrations/0020-video-likes.ts | 2 +- .../initializers/migrations/0025-video-dislikes.ts | 2 +- .../initializers/migrations/0030-video-category.ts | 2 +- .../initializers/migrations/0035-video-licence.ts | 2 +- server/initializers/migrations/0040-video-nsfw.ts | 2 +- .../migrations/0045-user-display-nsfw.ts | 2 +- .../initializers/migrations/0050-video-language.ts | 2 +- server/initializers/migrations/0055-video-uuid.ts | 157 +++++++++++++++++++++ 11 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 server/initializers/migrations/0055-video-uuid.ts (limited to 'server/initializers/migrations') diff --git a/server/initializers/migrations/0005-email-pod.ts b/server/initializers/migrations/0005-email-pod.ts index ceefaad4a..ab60f3adb 100644 --- a/server/initializers/migrations/0005-email-pod.ts +++ b/server/initializers/migrations/0005-email-pod.ts @@ -26,7 +26,7 @@ function up (utils: { }) } -function down (options, callback) { +function down (options) { throw new Error('Not implemented.') } diff --git a/server/initializers/migrations/0010-email-user.ts b/server/initializers/migrations/0010-email-user.ts index e8865acdb..33d13ce55 100644 --- a/server/initializers/migrations/0010-email-user.ts +++ b/server/initializers/migrations/0010-email-user.ts @@ -25,7 +25,7 @@ function up (utils: { }) } -function down (options, callback) { +function down (options) { throw new Error('Not implemented.') } diff --git a/server/initializers/migrations/0015-video-views.ts b/server/initializers/migrations/0015-video-views.ts index df274d817..25164ff4d 100644 --- a/server/initializers/migrations/0015-video-views.ts +++ b/server/initializers/migrations/0015-video-views.ts @@ -17,7 +17,7 @@ function up (utils: { return q.addColumn('Videos', 'views', data) } -function down (options, callback) { +function down (options) { throw new Error('Not implemented.') } diff --git a/server/initializers/migrations/0020-video-likes.ts b/server/initializers/migrations/0020-video-likes.ts index 3d7182d0a..945be5a98 100644 --- a/server/initializers/migrations/0020-video-likes.ts +++ b/server/initializers/migrations/0020-video-likes.ts @@ -17,7 +17,7 @@ function up (utils: { return q.addColumn('Videos', 'likes', data) } -function down (options, callback) { +function down (options) { throw new Error('Not implemented.') } diff --git a/server/initializers/migrations/0025-video-dislikes.ts b/server/initializers/migrations/0025-video-dislikes.ts index ed41095dc..27144c437 100644 --- a/server/initializers/migrations/0025-video-dislikes.ts +++ b/server/initializers/migrations/0025-video-dislikes.ts @@ -17,7 +17,7 @@ function up (utils: { return q.addColumn('Videos', 'dislikes', data) } -function down (options, callback) { +function down (options) { throw new Error('Not implemented.') } diff --git a/server/initializers/migrations/0030-video-category.ts b/server/initializers/migrations/0030-video-category.ts index f5adee8f9..41bc1aa98 100644 --- a/server/initializers/migrations/0030-video-category.ts +++ b/server/initializers/migrations/0030-video-category.ts @@ -22,7 +22,7 @@ function up (utils: { }) } -function down (options, callback) { +function down (options) { throw new Error('Not implemented.') } diff --git a/server/initializers/migrations/0035-video-licence.ts b/server/initializers/migrations/0035-video-licence.ts index 00c64d8e7..7ab49e147 100644 --- a/server/initializers/migrations/0035-video-licence.ts +++ b/server/initializers/migrations/0035-video-licence.ts @@ -21,7 +21,7 @@ function up (utils: { }) } -function down (options, callback) { +function down (options) { throw new Error('Not implemented.') } diff --git a/server/initializers/migrations/0040-video-nsfw.ts b/server/initializers/migrations/0040-video-nsfw.ts index 046876b61..0460e661d 100644 --- a/server/initializers/migrations/0040-video-nsfw.ts +++ b/server/initializers/migrations/0040-video-nsfw.ts @@ -22,7 +22,7 @@ function up (utils: { }) } -function down (options, callback) { +function down (options) { throw new Error('Not implemented.') } diff --git a/server/initializers/migrations/0045-user-display-nsfw.ts b/server/initializers/migrations/0045-user-display-nsfw.ts index 75bd3bbea..aef420f0e 100644 --- a/server/initializers/migrations/0045-user-display-nsfw.ts +++ b/server/initializers/migrations/0045-user-display-nsfw.ts @@ -17,7 +17,7 @@ function up (utils: { return q.addColumn('Users', 'displayNSFW', data) } -function down (options, callback) { +function down (options) { throw new Error('Not implemented.') } diff --git a/server/initializers/migrations/0050-video-language.ts b/server/initializers/migrations/0050-video-language.ts index ed08f5866..796fa5f95 100644 --- a/server/initializers/migrations/0050-video-language.ts +++ b/server/initializers/migrations/0050-video-language.ts @@ -17,7 +17,7 @@ function up (utils: { return q.addColumn('Videos', 'language', data) } -function down (options, callback) { +function down (options) { throw new Error('Not implemented.') } diff --git a/server/initializers/migrations/0055-video-uuid.ts b/server/initializers/migrations/0055-video-uuid.ts new file mode 100644 index 000000000..9bc65917c --- /dev/null +++ b/server/initializers/migrations/0055-video-uuid.ts @@ -0,0 +1,157 @@ +import * as Sequelize from 'sequelize' +import * as Promise from 'bluebird' + +function up (utils: { + transaction: Sequelize.Transaction, + queryInterface: Sequelize.QueryInterface, + sequelize: Sequelize.Sequelize +}): Promise { + const q = utils.queryInterface + + const dataUUID = { + type: Sequelize.UUID, + defaultValue: Sequelize.UUIDV4, + allowNull: true + } + + 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 +} -- cgit v1.2.3