2017-06-05 14:53:49 -05:00
|
|
|
import * as path from 'path'
|
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'
|
2017-07-05 06:26:25 -05:00
|
|
|
import { logger, readdirPromise } from '../helpers'
|
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
async function migrate () {
|
|
|
|
const tables = await db.sequelize.getQueryInterface().showAllTables()
|
2017-03-04 03:40:09 -06:00
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
// No tables, we don't need to migrate anything
|
|
|
|
// The installer will do that
|
|
|
|
if (tables.length === 0) return
|
2017-02-18 04:56:28 -06:00
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
let actualVersion = await db.Application.loadMigrationVersion()
|
|
|
|
if (actualVersion === null) {
|
|
|
|
await db.Application.create({ migrationVersion: 0 })
|
|
|
|
actualVersion = 0
|
|
|
|
}
|
2016-09-26 15:36:36 -05:00
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
// No need migrations, abort
|
|
|
|
if (actualVersion >= LAST_MIGRATION_VERSION) return
|
2016-09-26 15:36:36 -05:00
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
// If there are a new migration scripts
|
|
|
|
logger.info('Begin migrations.')
|
|
|
|
|
|
|
|
const migrationScripts = await getMigrationScripts()
|
2016-09-26 15:36:36 -05:00
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
for (const migrationScript of migrationScripts) {
|
2017-11-27 02:47:21 -06:00
|
|
|
try {
|
|
|
|
await executeMigration(actualVersion, migrationScript)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot execute migration %s.', migrationScript.version, err)
|
|
|
|
process.exit(0)
|
|
|
|
}
|
2017-10-25 09:03:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)
|
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-10-25 09:03:33 -05:00
|
|
|
async function getMigrationScripts () {
|
|
|
|
const files = await readdirPromise(path.join(__dirname, 'migrations'))
|
|
|
|
const filesToMigrate: {
|
|
|
|
version: string,
|
|
|
|
script: string
|
|
|
|
}[] = []
|
|
|
|
|
|
|
|
files
|
|
|
|
.filter(file => file.endsWith('.js.map') === false)
|
|
|
|
.forEach(file => {
|
|
|
|
// Filename is something like 'version-blabla.js'
|
|
|
|
const version = file.split('-')[0]
|
|
|
|
filesToMigrate.push({
|
|
|
|
version,
|
|
|
|
script: file
|
2016-12-25 02:44:57 -06:00
|
|
|
})
|
2017-10-25 09:03:33 -05:00
|
|
|
})
|
2016-12-25 02:44:57 -06:00
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
return filesToMigrate
|
2016-12-25 02:44:57 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
async function executeMigration (actualVersion: number, entity: { version: string, script: string }) {
|
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
|
2017-07-05 06:26:25 -05:00
|
|
|
if (versionScript <= actualVersion) return undefined
|
2016-12-25 02:44:57 -06:00
|
|
|
|
|
|
|
// 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))
|
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
2017-02-16 12:19:56 -06:00
|
|
|
const options = {
|
|
|
|
transaction: t,
|
|
|
|
queryInterface: db.sequelize.getQueryInterface(),
|
2017-08-25 04:36:23 -05:00
|
|
|
sequelize: db.sequelize,
|
|
|
|
db
|
2017-02-16 12:19:56 -06:00
|
|
|
}
|
2016-12-25 02:44:57 -06:00
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
await migrationScript.up(options)
|
|
|
|
|
|
|
|
// Update the new migration version
|
|
|
|
await db.Application.updateMigrationVersion(versionScript, t)
|
2016-12-25 02:44:57 -06:00
|
|
|
})
|
|
|
|
}
|