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) as any
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({
69 name: SERVER_ACTOR_NAME,
71 applicationId: applicationInstance.id,
75 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
76 accountCreated.Actor.publicKey = publicKey
77 accountCreated.Actor.privateKey = privateKey
79 await accountCreated.save()
82 // Drop old video channel foreign key (referencing Authors)
84 const query = 'ALTER TABLE "VideoChannels" DROP CONSTRAINT "VideoChannels_authorId_fkey"'
85 await utils.sequelize.query(query)
88 // Recreate accounts for each user
89 const users = await db.User.findAll()
90 for (const user of users) {
91 const account = await createLocalAccountWithoutKeys({ name: user.username, userId: user.id, applicationId: null, t: undefined })
93 const { publicKey, privateKey } = await createPrivateAndPublicKeys()
94 account.Actor.publicKey = publicKey
95 account.Actor.privateKey = privateKey
101 type: Sequelize.INTEGER,
109 await q.addColumn('VideoChannels', 'accountId', data)
112 const query = 'UPDATE "VideoChannels" SET "accountId" = ' +
113 '(SELECT "Accounts"."id" FROM "Accounts" INNER JOIN "Authors" ON "Authors"."userId" = "Accounts"."userId" ' +
114 'WHERE "VideoChannels"."authorId" = "Authors"."id")'
115 await utils.sequelize.query(query)
118 data.allowNull = false
119 await q.changeColumn('VideoChannels', 'accountId', data)
121 await q.removeColumn('VideoChannels', 'authorId')
124 // Add url column to "Videos"
127 type: Sequelize.STRING,
131 await q.addColumn('Videos', 'url', data)
133 const videos = await db.Video.findAll()
134 for (const video of videos) {
135 video.url = getVideoActivityPubUrl(video)
139 data.allowNull = false
140 await q.changeColumn('Videos', 'url', data)
143 // Add url column to "VideoChannels"
146 type: Sequelize.STRING,
150 await q.addColumn('VideoChannels', 'url', data)
152 const videoChannels = await db.VideoChannel.findAll()
153 for (const videoChannel of videoChannels) {
154 videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
155 await videoChannel.save()
158 data.allowNull = false
159 await q.changeColumn('VideoChannels', 'url', data)
162 // Loss old video rates, whatever
163 await utils.queryInterface.dropTable('UserVideoRates')
164 await db.AccountVideoRate.sync()
168 type: Sequelize.ENUM('transcoding', 'activitypub-http'),
169 defaultValue: 'transcoding',
172 await q.addColumn('Jobs', 'category', data)
175 await db.VideoShare.sync()
176 await db.VideoChannelShare.sync()
179 const videos = await db.Video.findAll({
182 model: db.Video['sequelize'].models.VideoChannel,
185 model: db.Video['sequelize'].models.Account,
186 include: [ { model: db.Video['sequelize'].models.Server, required: false } ]
191 model: db.Video['sequelize'].models.AccountVideoRate,
192 include: [ db.Video['sequelize'].models.Account ]
195 model: db.Video['sequelize'].models.VideoShare,
196 include: [ db.Video['sequelize'].models.Account ]
198 db.Video['sequelize'].models.Tag,
199 db.Video['sequelize'].models.VideoFile
203 for (const video of videos) {
204 await shareVideoByServerAndChannel(video, undefined)
209 function down (options) {
210 throw new Error('Not implemented.')