ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0760-video-live-replay-setting.ts 3.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import * as Sequelize from 'sequelize'
  2. async function up (utils: {
  3. transaction: Sequelize.Transaction
  4. queryInterface: Sequelize.QueryInterface
  5. sequelize: Sequelize.Sequelize
  6. }): Promise<void> {
  7. {
  8. const query = `
  9. CREATE TABLE IF NOT EXISTS "videoLiveReplaySetting" (
  10. "id" SERIAL ,
  11. "privacy" INTEGER NOT NULL,
  12. "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
  13. "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
  14. PRIMARY KEY ("id")
  15. );
  16. `
  17. await utils.sequelize.query(query, { transaction : utils.transaction })
  18. }
  19. {
  20. await utils.queryInterface.addColumn('videoLive', 'replaySettingId', {
  21. type: Sequelize.INTEGER,
  22. defaultValue: null,
  23. allowNull: true,
  24. references: {
  25. model: 'videoLiveReplaySetting',
  26. key: 'id'
  27. },
  28. onDelete: 'SET NULL'
  29. }, { transaction: utils.transaction })
  30. }
  31. {
  32. await utils.queryInterface.addColumn('videoLiveSession', 'replaySettingId', {
  33. type: Sequelize.INTEGER,
  34. defaultValue: null,
  35. allowNull: true,
  36. references: {
  37. model: 'videoLiveReplaySetting',
  38. key: 'id'
  39. },
  40. onDelete: 'SET NULL'
  41. }, { transaction: utils.transaction })
  42. }
  43. {
  44. const query = `
  45. SELECT live."id", v."privacy"
  46. FROM "videoLive" live
  47. INNER JOIN "video" v ON live."videoId" = v."id"
  48. WHERE live."saveReplay" = true
  49. `
  50. const videoLives = await utils.sequelize.query<{ id: number, privacy: number }>(
  51. query,
  52. { type: Sequelize.QueryTypes.SELECT, transaction: utils.transaction }
  53. )
  54. for (const videoLive of videoLives) {
  55. const query = `
  56. WITH new_replay_setting AS (
  57. INSERT INTO "videoLiveReplaySetting" ("privacy", "createdAt", "updatedAt")
  58. VALUES (:privacy, NOW(), NOW())
  59. RETURNING id
  60. )
  61. UPDATE "videoLive" SET "replaySettingId" = (SELECT id FROM new_replay_setting)
  62. WHERE "id" = :id
  63. `
  64. const options = {
  65. replacements: { privacy: videoLive.privacy, id: videoLive.id },
  66. type: Sequelize.QueryTypes.UPDATE,
  67. transaction: utils.transaction
  68. }
  69. await utils.sequelize.query(query, options)
  70. }
  71. }
  72. {
  73. const query = `
  74. SELECT session."id", v."privacy"
  75. FROM "videoLiveSession" session
  76. INNER JOIN "video" v ON session."liveVideoId" = v."id"
  77. WHERE session."saveReplay" = true
  78. AND session."liveVideoId" IS NOT NULL;
  79. `
  80. const videoLiveSessions = await utils.sequelize.query<{ id: number, privacy: number }>(
  81. query,
  82. { type: Sequelize.QueryTypes.SELECT, transaction: utils.transaction }
  83. )
  84. for (const videoLive of videoLiveSessions) {
  85. const query = `
  86. WITH new_replay_setting AS (
  87. INSERT INTO "videoLiveReplaySetting" ("privacy", "createdAt", "updatedAt")
  88. VALUES (:privacy, NOW(), NOW())
  89. RETURNING id
  90. )
  91. UPDATE "videoLiveSession" SET "replaySettingId" = (SELECT id FROM new_replay_setting)
  92. WHERE "id" = :id
  93. `
  94. const options = {
  95. replacements: { privacy: videoLive.privacy, id: videoLive.id },
  96. type: Sequelize.QueryTypes.UPDATE,
  97. transaction: utils.transaction
  98. }
  99. await utils.sequelize.query(query, options)
  100. }
  101. }
  102. }
  103. function down (options) {
  104. throw new Error('Not implemented.')
  105. }
  106. export {
  107. up,
  108. down
  109. }