3 const eachSeries = require('async/eachSeries')
4 const path = require('path')
6 const constants = require('./constants')
7 const db = require('./database')
8 const logger = require('../helpers/logger')
14 function migrate (callback) {
15 db.Application.loadSqlSchemaVersion(function (err, actualVersion) {
16 if (err) return callback(err)
18 // If there are a new mongo schemas
19 if (!actualVersion || actualVersion < constants.LAST_SQL_SCHEMA_VERSION) {
20 logger.info('Begin migrations.')
22 eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) {
23 const versionScript = entity.version
25 // Do not execute old migration scripts
26 if (versionScript <= actualVersion) return callbackEach(null)
28 // Load the migration module and run it
29 const migrationScriptName = entity.script
30 logger.info('Executing %s migration script.', migrationScriptName)
32 const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName))
33 migrationScript.up(function (err) {
34 if (err) return callbackEach(err)
36 // Update the new mongo version schema
37 db.Application.updateSqlSchemaVersion(versionScript, callbackEach)
40 if (err) return callback(err)
42 logger.info('Migrations finished. New SQL version schema: %s', constants.LAST_SQL_SCHEMA_VERSION)
51 // ---------------------------------------------------------------------------
53 module.exports = migrator