2016-09-26 16:36:36 -04:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const eachSeries = require('async/eachSeries')
|
2016-12-25 03:44:57 -05:00
|
|
|
const fs = require('fs')
|
2016-09-26 16:36:36 -04:00
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
const constants = require('./constants')
|
2016-12-11 15:50:51 -05:00
|
|
|
const db = require('./database')
|
2016-09-26 16:36:36 -04:00
|
|
|
const logger = require('../helpers/logger')
|
|
|
|
|
|
|
|
const migrator = {
|
|
|
|
migrate: migrate
|
|
|
|
}
|
|
|
|
|
|
|
|
function migrate (callback) {
|
2016-12-25 03:44:57 -05:00
|
|
|
db.Application.loadMigrationVersion(function (err, actualVersion) {
|
2016-09-26 16:36:36 -04:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-12-25 03:44:57 -05:00
|
|
|
// If there are a new migration scripts
|
|
|
|
if (actualVersion < constants.LAST_MIGRATION_VERSION) {
|
2016-09-26 16:36:36 -04:00
|
|
|
logger.info('Begin migrations.')
|
|
|
|
|
2016-12-25 03:44:57 -05:00
|
|
|
getMigrationScripts(function (err, migrationScripts) {
|
|
|
|
if (err) return callback(err)
|
2016-09-26 16:36:36 -04:00
|
|
|
|
2016-12-25 03:44:57 -05:00
|
|
|
eachSeries(migrationScripts, function (entity, callbackEach) {
|
|
|
|
executeMigration(actualVersion, entity, callbackEach)
|
|
|
|
}, function (err) {
|
|
|
|
if (err) return callback(err)
|
2016-09-26 16:36:36 -04:00
|
|
|
|
2016-12-25 03:44:57 -05:00
|
|
|
logger.info('Migrations finished. New migration version schema: %s', constants.LAST_MIGRATION_VERSION)
|
|
|
|
return callback(null)
|
2016-09-26 16:36:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return callback(null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = migrator
|
|
|
|
|
2016-12-25 03:44:57 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function getMigrationScripts (callback) {
|
|
|
|
fs.readdir(path.join(__dirname, 'migrations'), function (err, files) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
const filesToMigrate = []
|
|
|
|
|
|
|
|
files.forEach(function (file) {
|
|
|
|
// Filename is something like 'version-blabla.js'
|
|
|
|
const version = file.split('-')[0]
|
|
|
|
filesToMigrate.push({
|
|
|
|
version,
|
|
|
|
script: file
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return callback(err, filesToMigrate)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function executeMigration (actualVersion, entity, callback) {
|
|
|
|
const versionScript = entity.version
|
|
|
|
|
|
|
|
// Do not execute old migration scripts
|
|
|
|
if (versionScript <= actualVersion) return callback(null)
|
|
|
|
|
|
|
|
// Load the migration module and run it
|
|
|
|
const migrationScriptName = entity.script
|
|
|
|
logger.info('Executing %s migration script.', migrationScriptName)
|
|
|
|
|
|
|
|
const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName))
|
|
|
|
|
|
|
|
db.sequelize.transaction().asCallback(function (err, t) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
migrationScript.up({ transaction: t }, function (err) {
|
|
|
|
if (err) {
|
|
|
|
t.rollback()
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the new migration version
|
|
|
|
db.Application.updateMigrationVersion(versionScript, t, function (err) {
|
|
|
|
if (err) {
|
|
|
|
t.rollback()
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
2017-01-11 10:22:50 -05:00
|
|
|
t.commit().asCallback(callback)
|
2016-12-25 03:44:57 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|