PeerTube/server/initializers/migrations/0345-video-playlists.ts

89 lines
3.1 KiB
TypeScript
Raw Normal View History

2019-03-06 04:32:53 -06:00
import * as Sequelize from 'sequelize'
import { buildUUID } from '@shared/extra-utils'
2019-03-06 04:32:53 -06:00
import { VideoPlaylistPrivacy, VideoPlaylistType } from '../../../shared/models/videos'
2019-04-11 04:33:44 -05:00
import { WEBSERVER } from '../constants'
2019-03-06 04:32:53 -06:00
async function up (utils: {
2020-01-31 09:56:52 -06:00
transaction: Sequelize.Transaction
queryInterface: Sequelize.QueryInterface
2019-03-06 04:32:53 -06:00
sequelize: Sequelize.Sequelize
}): Promise<void> {
const transaction = utils.transaction
{
const query = `
CREATE TABLE IF NOT EXISTS "videoPlaylist"
(
"id" SERIAL,
"name" VARCHAR(255) NOT NULL,
"description" VARCHAR(255),
"privacy" INTEGER NOT NULL,
"url" VARCHAR(2000) NOT NULL,
"uuid" UUID NOT NULL,
"type" INTEGER NOT NULL DEFAULT 1,
"ownerAccountId" INTEGER NOT NULL REFERENCES "account" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"videoChannelId" INTEGER REFERENCES "videoChannel" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
"updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
PRIMARY KEY ("id")
);`
await utils.sequelize.query(query, { transaction })
}
{
const query = `
CREATE TABLE IF NOT EXISTS "videoPlaylistElement"
(
"id" SERIAL,
"url" VARCHAR(2000) NOT NULL,
"position" INTEGER NOT NULL DEFAULT 1,
"startTimestamp" INTEGER,
"stopTimestamp" INTEGER,
"videoPlaylistId" INTEGER NOT NULL REFERENCES "videoPlaylist" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"videoId" INTEGER NOT NULL REFERENCES "video" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
"updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
PRIMARY KEY ("id")
);`
await utils.sequelize.query(query, { transaction })
}
{
const userQuery = 'SELECT "username" FROM "user";'
2019-04-18 04:28:17 -05:00
const options = { transaction, type: Sequelize.QueryTypes.SELECT as Sequelize.QueryTypes.SELECT }
const userResult = await utils.sequelize.query<{ username: string }>(userQuery, options)
2019-03-06 04:32:53 -06:00
const usernames = userResult.map(r => r.username)
for (const username of usernames) {
const uuid = buildUUID()
2019-03-06 04:32:53 -06:00
2019-04-11 04:33:44 -05:00
const baseUrl = WEBSERVER.URL + '/video-playlists/' + uuid
2019-03-06 04:32:53 -06:00
const query = `
INSERT INTO "videoPlaylist" ("url", "uuid", "name", "privacy", "type", "ownerAccountId", "createdAt", "updatedAt")
SELECT '${baseUrl}' AS "url",
'${uuid}' AS "uuid",
'Watch later' AS "name",
${VideoPlaylistPrivacy.PRIVATE} AS "privacy",
${VideoPlaylistType.WATCH_LATER} AS "type",
"account"."id" AS "ownerAccountId",
NOW() as "createdAt",
NOW() as "updatedAt"
FROM "user" INNER JOIN "account" ON "user"."id" = "account"."userId"
WHERE "user"."username" = '${username}'`
await utils.sequelize.query(query, { transaction })
}
}
}
function down (options) {
throw new Error('Not implemented.')
}
export {
up,
down
}