diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-02 15:39:09 +0200 |
commit | a6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (patch) | |
tree | 03204a408d56311692c3528bedcf95d2455e94f2 /server/initializers/migrator.js | |
parent | 052937db8a8d282eccdbdf38d487ed8d85d3c0a7 (diff) | |
parent | c4403b29ad4db097af528a7f04eea07e0ed320d0 (diff) | |
download | PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.gz PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.tar.zst PeerTube-a6375e69668ea42e19531c6bc68dcd37f3f7cbd7.zip |
Merge branch 'master' into webseed-merged
Diffstat (limited to 'server/initializers/migrator.js')
-rw-r--r-- | server/initializers/migrator.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/server/initializers/migrator.js b/server/initializers/migrator.js new file mode 100644 index 000000000..6b31d994f --- /dev/null +++ b/server/initializers/migrator.js | |||
@@ -0,0 +1,56 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const eachSeries = require('async/eachSeries') | ||
4 | const mongoose = require('mongoose') | ||
5 | const path = require('path') | ||
6 | |||
7 | const constants = require('./constants') | ||
8 | const logger = require('../helpers/logger') | ||
9 | |||
10 | const Application = mongoose.model('Application') | ||
11 | |||
12 | const migrator = { | ||
13 | migrate: migrate | ||
14 | } | ||
15 | |||
16 | function migrate (callback) { | ||
17 | Application.loadMongoSchemaVersion(function (err, actualVersion) { | ||
18 | if (err) return callback(err) | ||
19 | |||
20 | // If there are a new mongo schemas | ||
21 | if (!actualVersion || actualVersion < constants.LAST_MONGO_SCHEMA_VERSION) { | ||
22 | logger.info('Begin migrations.') | ||
23 | |||
24 | eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) { | ||
25 | const versionScript = entity.version | ||
26 | |||
27 | // Do not execute old migration scripts | ||
28 | if (versionScript <= actualVersion) return callbackEach(null) | ||
29 | |||
30 | // Load the migration module and run it | ||
31 | const migrationScriptName = entity.script | ||
32 | logger.info('Executing %s migration script.', migrationScriptName) | ||
33 | |||
34 | const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) | ||
35 | migrationScript.up(function (err) { | ||
36 | if (err) return callbackEach(err) | ||
37 | |||
38 | // Update the new mongo version schema | ||
39 | Application.updateMongoSchemaVersion(versionScript, callbackEach) | ||
40 | }) | ||
41 | }, function (err) { | ||
42 | if (err) return callback(err) | ||
43 | |||
44 | logger.info('Migrations finished. New mongo version schema: %s', constants.LAST_MONGO_SCHEMA_VERSION) | ||
45 | return callback(null) | ||
46 | }) | ||
47 | } else { | ||
48 | return callback(null) | ||
49 | } | ||
50 | }) | ||
51 | } | ||
52 | |||
53 | // --------------------------------------------------------------------------- | ||
54 | |||
55 | module.exports = migrator | ||
56 | |||