diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-11-14 22:49:19 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-11-16 20:29:26 +0100 |
commit | 2550fab35e0113264369f9637e1bea169efdfc8f (patch) | |
tree | 23480a9565de29791545612864af240ccceb5d1f /server/initializers | |
parent | c92372d0aa8bd2bc02152941cfd4546b45949368 (diff) | |
download | PeerTube-2550fab35e0113264369f9637e1bea169efdfc8f.tar.gz PeerTube-2550fab35e0113264369f9637e1bea169efdfc8f.tar.zst PeerTube-2550fab35e0113264369f9637e1bea169efdfc8f.zip |
Server: add migration scripts to the new mongo schemes
Diffstat (limited to 'server/initializers')
-rw-r--r-- | server/initializers/constants.js | 16 | ||||
-rw-r--r-- | server/initializers/migrations/0025-video-filenames.js | 57 | ||||
-rw-r--r-- | server/initializers/migrations/0030-video-magnet.js | 32 | ||||
-rw-r--r-- | server/initializers/migrations/0035-url-to-host.js | 30 | ||||
-rw-r--r-- | server/initializers/migrations/0040-video-remote-id.js | 63 |
5 files changed, 198 insertions, 0 deletions
diff --git a/server/initializers/constants.js b/server/initializers/constants.js index d8a13d066..40e1c5381 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js | |||
@@ -104,6 +104,22 @@ const MONGO_MIGRATION_SCRIPTS = [ | |||
104 | { | 104 | { |
105 | script: '0020-requests-endpoint', | 105 | script: '0020-requests-endpoint', |
106 | version: 20 | 106 | version: 20 |
107 | }, | ||
108 | { | ||
109 | script: '0025-video-filenames', | ||
110 | version: 25 | ||
111 | }, | ||
112 | { | ||
113 | script: '0030-video-magnet', | ||
114 | version: 30 | ||
115 | }, | ||
116 | { | ||
117 | script: '0035-url-to-host', | ||
118 | version: 35 | ||
119 | }, | ||
120 | { | ||
121 | script: '0040-video-remote-id', | ||
122 | version: 40 | ||
107 | } | 123 | } |
108 | ] | 124 | ] |
109 | const LAST_MONGO_SCHEMA_VERSION = (maxBy(MONGO_MIGRATION_SCRIPTS, 'version'))['version'] | 125 | const LAST_MONGO_SCHEMA_VERSION = (maxBy(MONGO_MIGRATION_SCRIPTS, 'version'))['version'] |
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 @@ | |||
1 | /* | ||
2 | Rename thumbnails and video filenames to _id.extension | ||
3 | */ | ||
4 | |||
5 | const each = require('async/each') | ||
6 | const fs = require('fs') | ||
7 | const path = require('path') | ||
8 | const mongoose = require('mongoose') | ||
9 | |||
10 | const constants = require('../constants') | ||
11 | const logger = require('../../helpers/logger') | ||
12 | |||
13 | const Video = mongoose.model('Video') | ||
14 | |||
15 | exports.up = function (callback) { | ||
16 | // Use of lean because the new Video scheme does not have filename field | ||
17 | Video.find({ filename: { $ne: null } }).lean().exec(function (err, videos) { | ||
18 | if (err) throw err | ||
19 | |||
20 | each(videos, function (video, callbackEach) { | ||
21 | const torrentName = video.filename + '.torrent' | ||
22 | const thumbnailName = video.thumbnail | ||
23 | const thumbnailExtension = path.extname(thumbnailName) | ||
24 | const videoName = video.filename | ||
25 | const videoExtension = path.extname(videoName) | ||
26 | |||
27 | const newTorrentName = video._id + '.torrent' | ||
28 | const newThumbnailName = video._id + thumbnailExtension | ||
29 | const newVideoName = video._id + videoExtension | ||
30 | |||
31 | const torrentsDir = constants.CONFIG.STORAGE.TORRENTS_DIR | ||
32 | const thumbnailsDir = constants.CONFIG.STORAGE.THUMBNAILS_DIR | ||
33 | const videosDir = constants.CONFIG.STORAGE.VIDEOS_DIR | ||
34 | |||
35 | logger.info('Renaming %s to %s.', torrentsDir + torrentName, torrentsDir + newTorrentName) | ||
36 | fs.renameSync(torrentsDir + torrentName, torrentsDir + newTorrentName) | ||
37 | |||
38 | logger.info('Renaming %s to %s.', thumbnailsDir + thumbnailName, thumbnailsDir + newThumbnailName) | ||
39 | fs.renameSync(thumbnailsDir + thumbnailName, thumbnailsDir + newThumbnailName) | ||
40 | |||
41 | logger.info('Renaming %s to %s.', videosDir + videoName, videosDir + newVideoName) | ||
42 | fs.renameSync(videosDir + videoName, videosDir + newVideoName) | ||
43 | |||
44 | Video.load(video._id, function (err, videoObj) { | ||
45 | if (err) return callbackEach(err) | ||
46 | |||
47 | videoObj.extname = videoExtension | ||
48 | videoObj.remoteId = null | ||
49 | videoObj.save(callbackEach) | ||
50 | }) | ||
51 | }, callback) | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | exports.down = function (callback) { | ||
56 | throw new Error('Not implemented.') | ||
57 | } | ||
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 @@ | |||
1 | /* | ||
2 | Change video magnet structures | ||
3 | */ | ||
4 | |||
5 | const each = require('async/each') | ||
6 | const magnet = require('magnet-uri') | ||
7 | const mongoose = require('mongoose') | ||
8 | |||
9 | const Video = mongoose.model('Video') | ||
10 | |||
11 | exports.up = function (callback) { | ||
12 | // Use of lean because the new Video scheme does not have magnetUri field | ||
13 | Video.find({ }).lean().exec(function (err, videos) { | ||
14 | if (err) throw err | ||
15 | |||
16 | each(videos, function (video, callbackEach) { | ||
17 | const parsed = magnet.decode(video.magnetUri) | ||
18 | const infoHash = parsed.infoHash | ||
19 | |||
20 | Video.load(video._id, function (err, videoObj) { | ||
21 | if (err) return callbackEach(err) | ||
22 | |||
23 | videoObj.magnet.infoHash = infoHash | ||
24 | videoObj.save(callbackEach) | ||
25 | }) | ||
26 | }, callback) | ||
27 | }) | ||
28 | } | ||
29 | |||
30 | exports.down = function (callback) { | ||
31 | throw new Error('Not implemented.') | ||
32 | } | ||
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 @@ | |||
1 | /* | ||
2 | Change video magnet structures | ||
3 | */ | ||
4 | |||
5 | const each = require('async/each') | ||
6 | const mongoose = require('mongoose') | ||
7 | |||
8 | const Video = mongoose.model('Video') | ||
9 | |||
10 | exports.up = function (callback) { | ||
11 | // Use of lean because the new Video scheme does not have podUrl field | ||
12 | Video.find({ }).lean().exec(function (err, videos) { | ||
13 | if (err) throw err | ||
14 | |||
15 | each(videos, function (video, callbackEach) { | ||
16 | Video.load(video._id, function (err, videoObj) { | ||
17 | if (err) return callbackEach(err) | ||
18 | |||
19 | const host = video.podUrl.split('://')[1] | ||
20 | |||
21 | videoObj.podHost = host | ||
22 | videoObj.save(callbackEach) | ||
23 | }) | ||
24 | }, callback) | ||
25 | }) | ||
26 | } | ||
27 | |||
28 | exports.down = function (callback) { | ||
29 | throw new Error('Not implemented.') | ||
30 | } | ||
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 @@ | |||
1 | /* | ||
2 | Use remote id as identifier | ||
3 | */ | ||
4 | |||
5 | const each = require('async/each') | ||
6 | const map = require('lodash/map') | ||
7 | const mongoose = require('mongoose') | ||
8 | const readline = require('readline') | ||
9 | |||
10 | const rl = readline.createInterface({ | ||
11 | input: process.stdin, | ||
12 | output: process.stdout | ||
13 | }) | ||
14 | |||
15 | const logger = require('../../helpers/logger') | ||
16 | const friends = require('../../lib/friends') | ||
17 | |||
18 | const Pod = mongoose.model('Pod') | ||
19 | const Video = mongoose.model('Video') | ||
20 | |||
21 | exports.up = function (callback) { | ||
22 | Pod.find({}).lean().exec(function (err, pods) { | ||
23 | if (err) return callback(err) | ||
24 | |||
25 | // We need to quit friends first | ||
26 | if (pods.length === 0) { | ||
27 | return setVideosRemoteId(callback) | ||
28 | } | ||
29 | |||
30 | const timeout = setTimeout(function () { | ||
31 | throw new Error('You need to enter a value!') | ||
32 | }, 10000) | ||
33 | |||
34 | rl.question('I am sorry but I need to quit friends for upgrading. Do you want to continue? (yes/*)', function (answer) { | ||
35 | if (answer !== 'yes') throw new Error('I cannot continue.') | ||
36 | |||
37 | clearTimeout(timeout) | ||
38 | rl.close() | ||
39 | |||
40 | const urls = map(pods, 'url') | ||
41 | logger.info('Saying goodbye to: ' + urls.join(', ')) | ||
42 | |||
43 | friends.quitFriends(function () { | ||
44 | setVideosRemoteId(callback) | ||
45 | }) | ||
46 | }) | ||
47 | }) | ||
48 | } | ||
49 | |||
50 | exports.down = function (callback) { | ||
51 | throw new Error('Not implemented.') | ||
52 | } | ||
53 | |||
54 | function setVideosRemoteId (callback) { | ||
55 | Video.find({}, function (err, videos) { | ||
56 | if (err) return callback(err) | ||
57 | |||
58 | each(videos, function (video, callbackEach) { | ||
59 | video.remoteId = null | ||
60 | video.save(callbackEach) | ||
61 | }, callback) | ||
62 | }) | ||
63 | } | ||