0055-video-uuid.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import * as Sequelize from 'sequelize'
  2. import * as Promise from 'bluebird'
  3. import { Migration } from '../../models/migrations'
  4. function up (utils: {
  5. transaction: Sequelize.Transaction,
  6. queryInterface: Sequelize.QueryInterface,
  7. sequelize: Sequelize.Sequelize
  8. }): Promise<void> {
  9. const q = utils.queryInterface
  10. const dataUUID = {
  11. type: Sequelize.UUID,
  12. defaultValue: Sequelize.UUIDV4,
  13. allowNull: true
  14. } as Migration.UUID
  15. return q.addColumn('Videos', 'uuid', dataUUID)
  16. .then(() => {
  17. const query = 'UPDATE "Videos" SET "uuid" = "id" WHERE "remoteId" IS NULL'
  18. return utils.sequelize.query(query)
  19. })
  20. .then(() => {
  21. const query = 'UPDATE "Videos" SET "uuid" = "remoteId" WHERE "remoteId" IS NOT NULL'
  22. return utils.sequelize.query(query)
  23. })
  24. .then(() => {
  25. dataUUID.defaultValue = null
  26. return q.changeColumn('Videos', 'uuid', dataUUID)
  27. })
  28. .then(() => {
  29. return removeForeignKey(utils.sequelize, 'RequestVideoQadus')
  30. })
  31. .then(() => {
  32. return removeForeignKey(utils.sequelize, 'RequestVideoEvents')
  33. })
  34. .then(() => {
  35. return removeForeignKey(utils.sequelize, 'BlacklistedVideos')
  36. })
  37. .then(() => {
  38. return removeForeignKey(utils.sequelize, 'UserVideoRates')
  39. })
  40. .then(() => {
  41. return removeForeignKey(utils.sequelize, 'VideoAbuses')
  42. })
  43. .then(() => {
  44. return removeForeignKey(utils.sequelize, 'VideoTags')
  45. })
  46. .then(() => {
  47. const query = 'ALTER TABLE "Videos" DROP CONSTRAINT "Videos_pkey"'
  48. return utils.sequelize.query(query)
  49. })
  50. .then(() => {
  51. const query = 'ALTER TABLE "Videos" ADD COLUMN "id2" SERIAL PRIMARY KEY'
  52. return utils.sequelize.query(query)
  53. })
  54. .then(() => {
  55. return q.renameColumn('Videos', 'id', 'oldId')
  56. })
  57. .then(() => {
  58. return q.renameColumn('Videos', 'id2', 'id')
  59. })
  60. .then(() => {
  61. return changeForeignKey(q, utils.sequelize, 'RequestVideoQadus', false)
  62. })
  63. .then(() => {
  64. return changeForeignKey(q, utils.sequelize, 'RequestVideoEvents', false)
  65. })
  66. .then(() => {
  67. return changeForeignKey(q, utils.sequelize, 'BlacklistedVideos', false)
  68. })
  69. .then(() => {
  70. return changeForeignKey(q, utils.sequelize, 'UserVideoRates', false)
  71. })
  72. .then(() => {
  73. return changeForeignKey(q, utils.sequelize, 'VideoAbuses', false)
  74. })
  75. .then(() => {
  76. return changeForeignKey(q, utils.sequelize, 'VideoTags', true)
  77. })
  78. .then(() => {
  79. return q.removeColumn('Videos', 'oldId')
  80. })
  81. .then(() => {
  82. const dataRemote = {
  83. type: Sequelize.BOOLEAN,
  84. defaultValue: false,
  85. allowNull: false
  86. }
  87. return q.addColumn('Videos', 'remote', dataRemote)
  88. })
  89. .then(() => {
  90. const query = 'UPDATE "Videos" SET "remote" = false WHERE "remoteId" IS NULL'
  91. return utils.sequelize.query(query)
  92. })
  93. .then(() => {
  94. const query = 'UPDATE "Videos" SET "remote" = true WHERE "remoteId" IS NOT NULL'
  95. return utils.sequelize.query(query)
  96. })
  97. .then(() => {
  98. return q.removeColumn('Videos', 'remoteId')
  99. })
  100. }
  101. function down (options) {
  102. throw new Error('Not implemented.')
  103. }
  104. function removeForeignKey (sequelize: Sequelize.Sequelize, tableName: string) {
  105. const query = 'ALTER TABLE "' + tableName + '" DROP CONSTRAINT "' + tableName + '_videoId_fkey' + '"'
  106. return sequelize.query(query)
  107. }
  108. function changeForeignKey (q: Sequelize.QueryInterface, sequelize: Sequelize.Sequelize, tableName: string, allowNull: boolean) {
  109. const data = {
  110. type: Sequelize.INTEGER,
  111. allowNull: true
  112. }
  113. return q.addColumn(tableName, 'videoId2', data)
  114. .then(() => {
  115. const query = 'UPDATE "' + tableName + '" SET "videoId2" = ' +
  116. '(SELECT "id" FROM "Videos" WHERE "' + tableName + '"."videoId" = "Videos"."oldId")'
  117. return sequelize.query(query)
  118. })
  119. .then(() => {
  120. if (allowNull === false) {
  121. data.allowNull = false
  122. return q.changeColumn(tableName, 'videoId2', data)
  123. }
  124. return Promise.resolve()
  125. })
  126. .then(() => {
  127. return q.removeColumn(tableName, 'videoId')
  128. })
  129. .then(() => {
  130. return q.renameColumn(tableName, 'videoId2', 'videoId')
  131. })
  132. .then(() => {
  133. return q.addIndex(tableName, [ 'videoId' ])
  134. })
  135. .then(() => {
  136. const constraintName = tableName + '_videoId_fkey'
  137. const query = 'ALTER TABLE "' + tableName + '" ' +
  138. ' ADD CONSTRAINT "' + constraintName + '"' +
  139. ' FOREIGN KEY ("videoId") REFERENCES "Videos" ON DELETE CASCADE'
  140. return sequelize.query(query)
  141. })
  142. }
  143. export {
  144. up,
  145. down
  146. }