2016-09-26 15:36:36 -05:00
|
|
|
'use strict'
|
|
|
|
|
2017-02-18 04:56:28 -06:00
|
|
|
const waterfall = require('async/waterfall')
|
2016-09-26 15:36:36 -05:00
|
|
|
const eachSeries = require('async/eachSeries')
|
2016-12-25 02:44:57 -06:00
|
|
|
const fs = require('fs')
|
2016-09-26 15:36:36 -05:00
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
const constants = require('./constants')
|
2016-12-11 14:50:51 -06:00
|
|
|
const db = require('./database')
|
2016-09-26 15:36:36 -05:00
|
|
|
const logger = require('../helpers/logger')
|
|
|
|
|
|
|
|
const migrator = {
|
|
|
|
migrate: migrate
|
|
|
|
}
|
|
|
|
|
2017-02-18 04:56:28 -06:00
|
|
|
function migrate (finalCallback) {
|
|
|
|
waterfall([
|
|
|
|
|
|
|
|
function checkApplicationTableExists (callback) {
|
|
|
|
db.sequelize.getQueryInterface().showAllTables().asCallback(function (err, tables) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
// No tables, we don't need to migrate anything
|
|
|
|
// The installer will do that
|
|
|
|
if (tables.length === 0) return finalCallback(null)
|
|
|
|
|
|
|
|
return callback(null)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
function loadMigrationVersion (callback) {
|
|
|
|
db.Application.loadMigrationVersion(callback)
|
|
|
|
},
|
2016-09-26 15:36:36 -05:00
|
|
|
|
2017-02-18 04:56:28 -06:00
|
|
|
function abortMigrationIfNotNeeded (actualVersion, callback) {
|
|
|
|
// No need migrations
|
|
|
|
if (actualVersion >= constants.LAST_MIGRATION_VERSION) return finalCallback(null)
|
|
|
|
|
|
|
|
return callback(null, actualVersion)
|
|
|
|
},
|
|
|
|
|
|
|
|
function getMigrations (actualVersion, callback) {
|
|
|
|
// If there are a new migration scripts
|
2016-09-26 15:36:36 -05:00
|
|
|
logger.info('Begin migrations.')
|
|
|
|
|
2016-12-25 02:44:57 -06:00
|
|
|
getMigrationScripts(function (err, migrationScripts) {
|
2017-02-18 04:56:28 -06:00
|
|
|
return callback(err, actualVersion, migrationScripts)
|
|
|
|
})
|
|
|
|
},
|
2016-09-26 15:36:36 -05:00
|
|
|
|
2017-02-18 04:56:28 -06:00
|
|
|
function doMigrations (actualVersion, migrationScripts, callback) {
|
|
|
|
eachSeries(migrationScripts, function (entity, callbackEach) {
|
|
|
|
executeMigration(actualVersion, entity, callbackEach)
|
|
|
|
}, function (err) {
|
|
|
|
if (err) return callback(err)
|
2016-09-26 15:36:36 -05:00
|
|
|
|
2017-02-18 04:56:28 -06:00
|
|
|
logger.info('Migrations finished. New migration version schema: %s', constants.LAST_MIGRATION_VERSION)
|
|
|
|
return callback(null)
|
2016-09-26 15:36:36 -05:00
|
|
|
})
|
|
|
|
}
|
2017-02-18 04:56:28 -06:00
|
|
|
], finalCallback)
|
2016-09-26 15:36:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = migrator
|
|
|
|
|
2016-12-25 02:44:57 -06: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) {
|
2017-02-16 12:19:56 -06:00
|
|
|
const versionScript = parseInt(entity.version)
|
2016-12-25 02:44:57 -06:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2017-02-16 12:19:56 -06:00
|
|
|
const options = {
|
|
|
|
transaction: t,
|
|
|
|
queryInterface: db.sequelize.getQueryInterface(),
|
2017-02-18 04:56:28 -06:00
|
|
|
sequelize: db.sequelize,
|
2017-02-16 12:19:56 -06:00
|
|
|
Sequelize: db.Sequelize
|
|
|
|
}
|
|
|
|
migrationScript.up(options, function (err) {
|
2016-12-25 02:44:57 -06:00
|
|
|
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 09:22:50 -06:00
|
|
|
t.commit().asCallback(callback)
|
2016-12-25 02:44:57 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|