From 2550fab35e0113264369f9637e1bea169efdfc8f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 14 Nov 2016 22:49:19 +0100 Subject: Server: add migration scripts to the new mongo schemes --- .../migrations/0025-video-filenames.js | 57 ++++++++++++++++++++ .../initializers/migrations/0030-video-magnet.js | 32 +++++++++++ server/initializers/migrations/0035-url-to-host.js | 30 +++++++++++ .../migrations/0040-video-remote-id.js | 63 ++++++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 server/initializers/migrations/0025-video-filenames.js create mode 100644 server/initializers/migrations/0030-video-magnet.js create mode 100644 server/initializers/migrations/0035-url-to-host.js create mode 100644 server/initializers/migrations/0040-video-remote-id.js (limited to 'server/initializers/migrations') diff --git a/server/initializers/migrations/0025-video-filenames.js b/server/initializers/migrations/0025-video-filenames.js new file mode 100644 index 000000000..df21494d7 --- /dev/null +++ b/server/initializers/migrations/0025-video-filenames.js @@ -0,0 +1,57 @@ +/* + Rename thumbnails and video filenames to _id.extension +*/ + +const each = require('async/each') +const fs = require('fs') +const path = require('path') +const mongoose = require('mongoose') + +const constants = require('../constants') +const logger = require('../../helpers/logger') + +const Video = mongoose.model('Video') + +exports.up = function (callback) { + // Use of lean because the new Video scheme does not have filename field + Video.find({ filename: { $ne: null } }).lean().exec(function (err, videos) { + if (err) throw err + + each(videos, function (video, callbackEach) { + const torrentName = video.filename + '.torrent' + const thumbnailName = video.thumbnail + const thumbnailExtension = path.extname(thumbnailName) + const videoName = video.filename + const videoExtension = path.extname(videoName) + + const newTorrentName = video._id + '.torrent' + const newThumbnailName = video._id + thumbnailExtension + const newVideoName = video._id + videoExtension + + const torrentsDir = constants.CONFIG.STORAGE.TORRENTS_DIR + const thumbnailsDir = constants.CONFIG.STORAGE.THUMBNAILS_DIR + const videosDir = constants.CONFIG.STORAGE.VIDEOS_DIR + + logger.info('Renaming %s to %s.', torrentsDir + torrentName, torrentsDir + newTorrentName) + fs.renameSync(torrentsDir + torrentName, torrentsDir + newTorrentName) + + logger.info('Renaming %s to %s.', thumbnailsDir + thumbnailName, thumbnailsDir + newThumbnailName) + fs.renameSync(thumbnailsDir + thumbnailName, thumbnailsDir + newThumbnailName) + + logger.info('Renaming %s to %s.', videosDir + videoName, videosDir + newVideoName) + fs.renameSync(videosDir + videoName, videosDir + newVideoName) + + Video.load(video._id, function (err, videoObj) { + if (err) return callbackEach(err) + + videoObj.extname = videoExtension + videoObj.remoteId = null + videoObj.save(callbackEach) + }) + }, callback) + }) +} + +exports.down = function (callback) { + throw new Error('Not implemented.') +} diff --git a/server/initializers/migrations/0030-video-magnet.js b/server/initializers/migrations/0030-video-magnet.js new file mode 100644 index 000000000..b9119d61c --- /dev/null +++ b/server/initializers/migrations/0030-video-magnet.js @@ -0,0 +1,32 @@ +/* + Change video magnet structures +*/ + +const each = require('async/each') +const magnet = require('magnet-uri') +const mongoose = require('mongoose') + +const Video = mongoose.model('Video') + +exports.up = function (callback) { + // Use of lean because the new Video scheme does not have magnetUri field + Video.find({ }).lean().exec(function (err, videos) { + if (err) throw err + + each(videos, function (video, callbackEach) { + const parsed = magnet.decode(video.magnetUri) + const infoHash = parsed.infoHash + + Video.load(video._id, function (err, videoObj) { + if (err) return callbackEach(err) + + videoObj.magnet.infoHash = infoHash + videoObj.save(callbackEach) + }) + }, callback) + }) +} + +exports.down = function (callback) { + throw new Error('Not implemented.') +} diff --git a/server/initializers/migrations/0035-url-to-host.js b/server/initializers/migrations/0035-url-to-host.js new file mode 100644 index 000000000..6243304d5 --- /dev/null +++ b/server/initializers/migrations/0035-url-to-host.js @@ -0,0 +1,30 @@ +/* + Change video magnet structures +*/ + +const each = require('async/each') +const mongoose = require('mongoose') + +const Video = mongoose.model('Video') + +exports.up = function (callback) { + // Use of lean because the new Video scheme does not have podUrl field + Video.find({ }).lean().exec(function (err, videos) { + if (err) throw err + + each(videos, function (video, callbackEach) { + Video.load(video._id, function (err, videoObj) { + if (err) return callbackEach(err) + + const host = video.podUrl.split('://')[1] + + videoObj.podHost = host + videoObj.save(callbackEach) + }) + }, callback) + }) +} + +exports.down = function (callback) { + throw new Error('Not implemented.') +} diff --git a/server/initializers/migrations/0040-video-remote-id.js b/server/initializers/migrations/0040-video-remote-id.js new file mode 100644 index 000000000..5cf856b2e --- /dev/null +++ b/server/initializers/migrations/0040-video-remote-id.js @@ -0,0 +1,63 @@ +/* + Use remote id as identifier +*/ + +const each = require('async/each') +const map = require('lodash/map') +const mongoose = require('mongoose') +const readline = require('readline') + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}) + +const logger = require('../../helpers/logger') +const friends = require('../../lib/friends') + +const Pod = mongoose.model('Pod') +const Video = mongoose.model('Video') + +exports.up = function (callback) { + Pod.find({}).lean().exec(function (err, pods) { + if (err) return callback(err) + + // We need to quit friends first + if (pods.length === 0) { + return setVideosRemoteId(callback) + } + + const timeout = setTimeout(function () { + throw new Error('You need to enter a value!') + }, 10000) + + rl.question('I am sorry but I need to quit friends for upgrading. Do you want to continue? (yes/*)', function (answer) { + if (answer !== 'yes') throw new Error('I cannot continue.') + + clearTimeout(timeout) + rl.close() + + const urls = map(pods, 'url') + logger.info('Saying goodbye to: ' + urls.join(', ')) + + friends.quitFriends(function () { + setVideosRemoteId(callback) + }) + }) + }) +} + +exports.down = function (callback) { + throw new Error('Not implemented.') +} + +function setVideosRemoteId (callback) { + Video.find({}, function (err, videos) { + if (err) return callback(err) + + each(videos, function (video, callbackEach) { + video.remoteId = null + video.save(callbackEach) + }, callback) + }) +} -- cgit v1.2.3