1 import * as Sequelize from 'sequelize'
2 import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto'
3 import { shareVideoByServerAndChannel } from '../../lib/activitypub/share'
4 import { getVideoActivityPubUrl, getVideoChannelActivityPubUrl } from '../../lib/activitypub/url'
5 import { createLocalAccountWithoutKeys } from '../../lib/user'
6 import { ApplicationModel } from '../../models/application/application'
7 import { SERVER_ACTOR_NAME } from '../constants'
9 async function up (utils: {
10 transaction: Sequelize.Transaction,
11 queryInterface: Sequelize.QueryInterface,
12 sequelize: Sequelize.Sequelize,
15 const q = utils.queryInterface
18 // Assert there are no friends
20 const query = 'SELECT COUNT(*) as total FROM "Pods"'
22 type: Sequelize.QueryTypes.SELECT
24 const res = await utils.sequelize.query(query, options)
26 if (!res[0] || res[0].total !== 0) {
27 throw new Error('You need to quit friends.')
32 await utils.queryInterface.renameTable('Pods', 'Servers')
34 // Create Account table
35 await db.Account.sync()
37 // Create AccountFollows table
38 await db.AccountFollow.sync()
40 // Modify video abuse table
41 await db.VideoAbuse.destroy({ truncate: true })
42 await utils.queryInterface.removeColumn('VideoAbuses', 'reporterPodId')
43 await utils.queryInterface.removeColumn('VideoAbuses', 'reporterUsername')
45 // Create column link with Account table
48 type: Sequelize.INTEGER,
56 await q.addColumn('VideoAbuses', 'reporterAccountId', data)
59 // Drop request tables
60 await utils.queryInterface.dropTable('RequestToPods')
61 await utils.queryInterface.dropTable('RequestVideoEvents')
62 await utils.queryInterface.dropTable('RequestVideoQadus')
63 await utils.queryInterface.dropTable('Requests')
65 // Create application account
67 const applicationInstance = await ApplicationModel.findOne()
68 const accountCreated = await createLocalAccountWithoutKeys(SERVER_ACTOR_NAME, null, applicationInstance.id, undefined)
70 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
71 accountCreated.set('publicKey', publicKey)
72 accountCreated.set('privateKey', privateKey)
74 await accountCreated.save()
77 // Drop old video channel foreign key (referencing Authors)
79 const query = 'ALTER TABLE "VideoChannels" DROP CONSTRAINT "VideoChannels_authorId_fkey"'
80 await utils.sequelize.query(query)
83 // Recreate accounts for each user
84 const users = await db.User.findAll()
85 for (const user of users) {
86 const account = await createLocalAccountWithoutKeys(user.username, user.id, null, undefined)
88 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
89 account.set('publicKey', publicKey)
90 account.set('privateKey', privateKey)
96 type: Sequelize.INTEGER,
104 await q.addColumn('VideoChannels', 'accountId', data)
107 const query = 'UPDATE "VideoChannels" SET "accountId" = ' +
108 '(SELECT "Accounts"."id" FROM "Accounts" INNER JOIN "Authors" ON "Authors"."userId" = "Accounts"."userId" ' +
109 'WHERE "VideoChannels"."authorId" = "Authors"."id")'
110 await utils.sequelize.query(query)
113 data.allowNull = false
114 await q.changeColumn('VideoChannels', 'accountId', data)
116 await q.removeColumn('VideoChannels', 'authorId')
119 // Add url column to "Videos"
122 type: Sequelize.STRING,
126 await q.addColumn('Videos', 'url', data)
128 const videos = await db.Video.findAll()
129 for (const video of videos) {
130 video.url = getVideoActivityPubUrl(video)
134 data.allowNull = false
135 await q.changeColumn('Videos', 'url', data)
138 // Add url column to "VideoChannels"
141 type: Sequelize.STRING,
145 await q.addColumn('VideoChannels', 'url', data)
147 const videoChannels = await db.VideoChannel.findAll()
148 for (const videoChannel of videoChannels) {
149 videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
150 await videoChannel.save()
153 data.allowNull = false
154 await q.changeColumn('VideoChannels', 'url', data)
157 // Loss old video rates, whatever
158 await utils.queryInterface.dropTable('UserVideoRates')
159 await db.AccountVideoRate.sync()
163 type: Sequelize.ENUM('transcoding', 'activitypub-http'),
164 defaultValue: 'transcoding',
167 await q.addColumn('Jobs', 'category', data)
170 await db.VideoShare.sync()
171 await db.VideoChannelShare.sync()
174 const videos = await db.Video.findAll({
177 model: db.Video['sequelize'].models.VideoChannel,
180 model: db.Video['sequelize'].models.Account,
181 include: [ { model: db.Video['sequelize'].models.Server, required: false } ]
186 model: db.Video['sequelize'].models.AccountVideoRate,
187 include: [ db.Video['sequelize'].models.Account ]
190 model: db.Video['sequelize'].models.VideoShare,
191 include: [ db.Video['sequelize'].models.Account ]
193 db.Video['sequelize'].models.Tag,
194 db.Video['sequelize'].models.VideoFile
198 for (const video of videos) {
199 await shareVideoByServerAndChannel(video, undefined)
204 function down (options) {
205 throw new Error('Not implemented.')