PeerTube/server/initializers/migrator.ts

137 lines
3.8 KiB
TypeScript
Raw Normal View History

2017-05-15 22:22:03 +02:00
import { waterfall, eachSeries } from 'async'
2017-06-05 21:53:49 +02:00
import * as fs from 'fs'
import * as path from 'path'
2017-05-22 20:58:25 +02:00
import * as Sequelize from 'sequelize'
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'
import { logger } from '../helpers'
2017-05-22 20:58:25 +02:00
import { ApplicationInstance } from '../models'
2017-02-18 11:56:28 +01: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)
},
function createMigrationRowIfNotExists (actualVersion, callback) {
if (actualVersion === null) {
db.Application.create({
migrationVersion: 0
}, function (err) {
2017-03-04 10:41:42 +01:00
return callback(err, 0)
})
}
return callback(null, actualVersion)
},
2017-02-18 11:56:28 +01:00
function abortMigrationIfNotNeeded (actualVersion, callback) {
// No need migrations
2017-05-15 22:22:03 +02:00
if (actualVersion >= LAST_MIGRATION_VERSION) return finalCallback(null)
2017-02-18 11:56:28 +01:00
return callback(null, actualVersion)
},
function getMigrations (actualVersion, callback) {
// If there are a new migration scripts
logger.info('Begin migrations.')
2016-12-25 09:44:57 +01:00
getMigrationScripts(function (err, migrationScripts) {
2017-02-18 11:56:28 +01:00
return callback(err, actualVersion, migrationScripts)
})
},
2017-02-18 11:56:28 +01:00
function doMigrations (actualVersion, migrationScripts, callback) {
eachSeries(migrationScripts, function (entity, callbackEach) {
executeMigration(actualVersion, entity, callbackEach)
}, function (err) {
if (err) return callback(err)
2017-05-15 22:22:03 +02:00
logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)
2017-02-18 11:56:28 +01:00
return callback(null)
})
}
2017-02-18 11:56:28 +01:00
], finalCallback)
}
// ---------------------------------------------------------------------------
2017-05-15 22:22:03 +02:00
export {
migrate
}
2016-12-25 09:44:57 +01: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-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
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 19:19:56 +01:00
const options = {
transaction: t,
queryInterface: db.sequelize.getQueryInterface(),
2017-02-18 11:56:28 +01:00
sequelize: db.sequelize,
2017-05-22 20:58:25 +02:00
Sequelize: Sequelize
2017-02-16 19:19:56 +01:00
}
migrationScript.up(options, function (err) {
2016-12-25 09:44:57 +01: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 16:22:50 +01:00
t.commit().asCallback(callback)
2016-12-25 09:44:57 +01:00
})
})
})
}