0080-video-channels.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import * as Sequelize from 'sequelize'
  2. import * as uuidv4 from 'uuid/v4'
  3. async function up (utils: {
  4. transaction: Sequelize.Transaction,
  5. queryInterface: Sequelize.QueryInterface,
  6. sequelize: Sequelize.Sequelize,
  7. db: any
  8. }): Promise<void> {
  9. const q = utils.queryInterface
  10. // Assert not friends
  11. // Create uuid column for author
  12. const dataAuthorUUID = {
  13. type: Sequelize.UUID,
  14. defaultValue: Sequelize.UUIDV4,
  15. allowNull: true
  16. }
  17. await q.addColumn('Authors', 'uuid', dataAuthorUUID)
  18. // Set UUID to previous authors
  19. {
  20. const authors = await utils.db.Author.findAll()
  21. for (const author of authors) {
  22. author.uuid = uuidv4()
  23. await author.save()
  24. }
  25. }
  26. dataAuthorUUID.allowNull = false
  27. await q.changeColumn('Authors', 'uuid', dataAuthorUUID)
  28. // Create one author per user that does not already exist
  29. const users = await utils.db.User.findAll()
  30. for (const user of users) {
  31. const author = await utils.db.Author.find({ where: { userId: user.id } })
  32. if (!author) {
  33. await utils.db.Author.create({
  34. name: user.username,
  35. podId: null, // It is our pod
  36. userId: user.id
  37. })
  38. }
  39. }
  40. // Create video channels table
  41. await utils.db.VideoChannel.sync()
  42. // For each author, create its default video channel
  43. const authors = await utils.db.Author.findAll()
  44. for (const author of authors) {
  45. await utils.db.VideoChannel.create({
  46. name: `Default ${author.name} channel`,
  47. remote: false,
  48. authorId: author.id
  49. })
  50. }
  51. // Create channelId column for videos
  52. const dataChannelId = {
  53. type: Sequelize.INTEGER,
  54. defaultValue: null,
  55. allowNull: true
  56. }
  57. await q.addColumn('Videos', 'channelId', dataChannelId)
  58. const query = 'SELECT "id", "authorId" FROM "Videos"'
  59. const options = {
  60. type: Sequelize.QueryTypes.SELECT
  61. }
  62. const rawVideos = await utils.sequelize.query(query, options)
  63. for (const rawVideo of rawVideos) {
  64. const videoChannel = await utils.db.VideoChannel.findOne({ where: { authorId: rawVideo.authorId } })
  65. const video = await utils.db.Video.findById(rawVideo.id)
  66. video.channelId = videoChannel.id
  67. await video.save()
  68. }
  69. dataChannelId.allowNull = false
  70. await q.changeColumn('Videos', 'channelId', dataChannelId)
  71. const constraintName = 'Videos_channelId_fkey'
  72. const queryForeignKey = 'ALTER TABLE "Videos" ' +
  73. ' ADD CONSTRAINT "' + constraintName + '"' +
  74. ' FOREIGN KEY ("channelId") REFERENCES "VideoChannels" ON UPDATE CASCADE ON DELETE CASCADE'
  75. await utils.sequelize.query(queryForeignKey)
  76. await q.removeColumn('Videos', 'authorId')
  77. }
  78. function down (options) {
  79. // update "Applications" SET "migrationVersion" = 75;
  80. // delete from "Authors";
  81. // alter table "Authors" drop column "uuid";
  82. // ALTER SEQUENCE "Authors_id_seq" RESTART WITH 1
  83. // INSERT INTO "Authors" ("name", "createdAt", "updatedAt", "userId") VALUES ('root', NOW(), NOW(), 1);
  84. // alter table "Videos" drop column "channelId";
  85. // drop table "VideoChannels";
  86. // alter table "Videos" add column "authorId" INTEGER DEFAULT 1;
  87. // alter table "Videos" ADD CONSTRAINT "coucou" FOREIGN KEY ("authorId") REFERENCES "Authors"
  88. throw new Error('Not implemented.')
  89. }
  90. export {
  91. up,
  92. down
  93. }