2017-05-15 15:22:03 -05:00
|
|
|
import { waterfall, eachSeries } from 'async'
|
2017-06-05 14:53:49 -05:00
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as path from 'path'
|
2017-05-22 13:58:25 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
2016-09-26 15:36:36 -05:00
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from './database'
|
2017-05-15 15:22:03 -05:00
|
|
|
import { LAST_MIGRATION_VERSION } from './constants'
|
|
|
|
import { logger } from '../helpers'
|
2016-09-26 15:36:36 -05:00
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function migrate (finalCallback: (err: Error) => void) {
|
2017-02-18 04:56:28 -06:00
|
|
|
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-03-04 03:40:09 -06:00
|
|
|
function createMigrationRowIfNotExists (actualVersion, callback) {
|
|
|
|
if (actualVersion === null) {
|
|
|
|
db.Application.create({
|
|
|
|
migrationVersion: 0
|
|
|
|
}, function (err) {
|
2017-03-04 03:41:42 -06:00
|
|
|
return callback(err, 0)
|
2017-03-04 03:40:09 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, actualVersion)
|
|
|
|
},
|
|
|
|
|
2017-02-18 04:56:28 -06:00
|
|
|
function abortMigrationIfNotNeeded (actualVersion, callback) {
|
|
|
|
// No need migrations
|
2017-05-15 15:22:03 -05:00
|
|
|
if (actualVersion >= LAST_MIGRATION_VERSION) return finalCallback(null)
|
2017-02-18 04:56:28 -06:00
|
|
|
|
|
|
|
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) {
|
2017-06-10 15:15:25 -05:00
|
|
|
eachSeries(migrationScripts, function (entity: any, callbackEach) {
|
2017-02-18 04:56:28 -06:00
|
|
|
executeMigration(actualVersion, entity, callbackEach)
|
|
|
|
}, function (err) {
|
|
|
|
if (err) return callback(err)
|
2016-09-26 15:36:36 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)
|
2017-02-18 04:56:28 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
migrate
|
|
|
|
}
|
2016-09-26 15:36:36 -05:00
|
|
|
|
2016-12-25 02:44:57 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
type GetMigrationScriptsCallback = (err: Error, filesToMigrate?: { version: string, script: string }[]) => void
|
|
|
|
function getMigrationScripts (callback: GetMigrationScriptsCallback) {
|
2016-12-25 02:44:57 -06:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function executeMigration (actualVersion: number, entity: { version: string, script: string }, callback: (err: Error) => void) {
|
2017-05-22 13:58:25 -05:00
|
|
|
const versionScript = parseInt(entity.version, 10)
|
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-05-22 13:58:25 -05:00
|
|
|
Sequelize: Sequelize
|
2017-02-16 12:19:56 -06:00
|
|
|
}
|
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|