From 2451916e45420fedf556913ce121f3964c4b57d6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 17 Feb 2021 09:36:09 +0100 Subject: Add video files migration --- .../migrations/0585-video-file-names.ts | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 server/initializers/migrations/0585-video-file-names.ts (limited to 'server/initializers/migrations') diff --git a/server/initializers/migrations/0585-video-file-names.ts b/server/initializers/migrations/0585-video-file-names.ts new file mode 100644 index 000000000..dd5fec3a1 --- /dev/null +++ b/server/initializers/migrations/0585-video-file-names.ts @@ -0,0 +1,55 @@ +import * as Sequelize from 'sequelize' + +async function up (utils: { + transaction: Sequelize.Transaction + queryInterface: Sequelize.QueryInterface + sequelize: Sequelize.Sequelize + db: any +}): Promise { + for (const column of [ 'filename', 'fileUrl', 'torrentFilename', 'torrentUrl' ]) { + const data = { + type: Sequelize.STRING, + allowNull: true, + defaultValue: null + } + + await utils.queryInterface.addColumn('videoFile', column, data) + } + + // Generate filenames for webtorrent files + { + const webtorrentQuery = `SELECT "videoFile".id, "video".uuid, "videoFile".resolution, "videoFile".extname ` + + `FROM video INNER JOIN "videoFile" ON "videoFile"."videoId" = video.id` + + const query = `UPDATE "videoFile" ` + + `SET filename = t.uuid || '-' || t.resolution || t.extname, ` + + `"torrentFilename" = t.uuid || '-' || t.resolution || '.torrent' ` + + `FROM (${webtorrentQuery}) AS t WHERE t.id = "videoFile"."id"` + + await utils.sequelize.query(query) + } + + // Generate filenames for HLS files + { + const hlsQuery = `SELECT "videoFile".id, "video".uuid, "videoFile".resolution, "videoFile".extname ` + + `FROM video ` + + `INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."videoId" = video.id ` + + `INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id` + + const query = `UPDATE "videoFile" ` + + `SET filename = t.uuid || '-' || t.resolution || '-fragmented' || t.extname, ` + + `"torrentFilename" = t.uuid || '-' || t.resolution || '-hls.torrent' ` + + `FROM (${hlsQuery}) AS t WHERE t.id = "videoFile"."id"` + + await utils.sequelize.query(query) + } +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} -- cgit v1.2.3