2017-06-05 21:53:49 +02:00
|
|
|
import * as path from 'path'
|
2017-07-05 13:26:25 +02:00
|
|
|
import * as Promise from 'bluebird'
|
2016-09-26 22:36:36 +02:00
|
|
|
|
2017-05-22 20:58:25 +02:00
|
|
|
import { database as db } from './database'
|
2017-05-15 22:22:03 +02:00
|
|
|
import { LAST_MIGRATION_VERSION } from './constants'
|
2017-07-05 13:26:25 +02:00
|
|
|
import { logger, readdirPromise } from '../helpers'
|
|
|
|
|
|
|
|
function migrate () {
|
|
|
|
const p = db.sequelize.getQueryInterface().showAllTables()
|
|
|
|
.then(tables => {
|
|
|
|
// No tables, we don't need to migrate anything
|
|
|
|
// The installer will do that
|
|
|
|
if (tables.length === 0) throw null
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return db.Application.loadMigrationVersion()
|
|
|
|
})
|
|
|
|
.then(actualVersion => {
|
2017-03-04 10:40:09 +01:00
|
|
|
if (actualVersion === null) {
|
2017-07-05 13:26:25 +02:00
|
|
|
return db.Application.create({ migrationVersion: 0 }).then(() => 0)
|
2017-03-04 10:40:09 +01:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
return actualVersion
|
|
|
|
})
|
|
|
|
.then(actualVersion => {
|
|
|
|
// No need migrations, abort
|
|
|
|
if (actualVersion >= LAST_MIGRATION_VERSION) throw null
|
2017-02-18 11:56:28 +01:00
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
return actualVersion
|
|
|
|
})
|
|
|
|
.then(actualVersion => {
|
2017-02-18 11:56:28 +01:00
|
|
|
// If there are a new migration scripts
|
2016-09-26 22:36:36 +02:00
|
|
|
logger.info('Begin migrations.')
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
return getMigrationScripts().then(migrationScripts => ({ actualVersion, migrationScripts }))
|
|
|
|
})
|
|
|
|
.then(({ actualVersion, migrationScripts }) => {
|
|
|
|
return Promise.mapSeries(migrationScripts, entity => {
|
|
|
|
return executeMigration(actualVersion, entity)
|
2017-02-18 11:56:28 +01:00
|
|
|
})
|
2017-07-05 13:26:25 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
if (err === null) return undefined
|
2016-09-26 22:36:36 +02:00
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
throw err
|
|
|
|
})
|
2016-09-26 22:36:36 +02:00
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
return p
|
2016-09-26 22:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
|
|
|
migrate
|
|
|
|
}
|
2016-09-26 22:36:36 +02:00
|
|
|
|
2016-12-25 09:44:57 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
function getMigrationScripts () {
|
|
|
|
return readdirPromise(path.join(__dirname, 'migrations')).then(files => {
|
|
|
|
const filesToMigrate: {
|
|
|
|
version: string,
|
|
|
|
script: string
|
|
|
|
}[] = []
|
2016-12-25 09:44:57 +01:00
|
|
|
|
|
|
|
files.forEach(function (file) {
|
|
|
|
// Filename is something like 'version-blabla.js'
|
|
|
|
const version = file.split('-')[0]
|
|
|
|
filesToMigrate.push({
|
|
|
|
version,
|
|
|
|
script: file
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
return filesToMigrate
|
2016-12-25 09:44:57 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
function executeMigration (actualVersion: number, entity: { version: string, script: string }) {
|
2017-05-22 20:58:25 +02:00
|
|
|
const versionScript = parseInt(entity.version, 10)
|
2016-12-25 09:44:57 +01:00
|
|
|
|
|
|
|
// Do not execute old migration scripts
|
2017-07-05 13:26:25 +02:00
|
|
|
if (versionScript <= actualVersion) return undefined
|
2016-12-25 09:44:57 +01: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-07-05 13:26:25 +02:00
|
|
|
return db.sequelize.transaction(t => {
|
2017-02-16 19:19:56 +01:00
|
|
|
const options = {
|
|
|
|
transaction: t,
|
|
|
|
queryInterface: db.sequelize.getQueryInterface(),
|
2017-07-05 13:26:25 +02:00
|
|
|
sequelize: db.sequelize
|
2017-02-16 19:19:56 +01:00
|
|
|
}
|
2016-12-25 09:44:57 +01:00
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
migrationScript.up(options)
|
|
|
|
.then(() => {
|
|
|
|
// Update the new migration version
|
|
|
|
db.Application.updateMigrationVersion(versionScript, t)
|
2016-12-25 09:44:57 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|