]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/migrator.js
Server: host -> hostname (host = hostname + port)
[github/Chocobozzz/PeerTube.git] / server / initializers / migrator.js
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