PeerTube/server/initializers/migrator.ts

100 lines
2.9 KiB
TypeScript
Raw Normal View History

2017-06-05 22:53:49 +03:00
import * as path from 'path'
2017-12-28 13:16:08 +03:00
import { logger } from '../helpers/logger'
2017-12-12 19:53:50 +03:00
import { LAST_MIGRATION_VERSION } from './constants'
import { sequelizeTypescript } from './database'
2018-08-27 17:23:34 +03:00
import { readdir } from 'fs-extra'
async function migrate () {
2017-12-12 19:53:50 +03:00
const tables = await sequelizeTypescript.getQueryInterface().showAllTables()
// No tables, we don't need to migrate anything
// The installer will do that
if (tables.length === 0) return
2017-02-18 13:56:28 +03:00
let actualVersion: number | null = null
2017-12-13 19:46:23 +03:00
const [ rows ] = await sequelizeTypescript.query('SELECT "migrationVersion" FROM "application"')
if (rows && rows[0] && rows[0].migrationVersion) {
actualVersion = rows[0].migrationVersion
2017-12-13 19:46:23 +03:00
}
if (actualVersion === null) {
2017-12-13 19:46:23 +03:00
await sequelizeTypescript.query('INSERT INTO "application" ("migrationVersion") VALUES (0)')
actualVersion = 0
}
// No need migrations, abort
if (actualVersion >= LAST_MIGRATION_VERSION) return
// If there are a new migration scripts
logger.info('Begin migrations.')
const migrationScripts = await getMigrationScripts()
for (const migrationScript of migrationScripts) {
2017-11-27 11:47:21 +03:00
try {
await executeMigration(actualVersion, migrationScript)
} catch (err) {
2018-03-26 16:54:13 +03:00
logger.error('Cannot execute migration %s.', migrationScript.version, { err })
2018-01-10 19:18:12 +03:00
process.exit(-1)
2017-11-27 11:47:21 +03:00
}
}
logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)
}
// ---------------------------------------------------------------------------
2017-05-15 23:22:03 +03:00
export {
migrate
}
2016-12-25 11:44:57 +03:00
// ---------------------------------------------------------------------------
async function getMigrationScripts () {
2018-08-27 17:23:34 +03:00
const files = await readdir(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 11:44:57 +03:00
})
})
2016-12-25 11:44:57 +03:00
return filesToMigrate
2016-12-25 11:44:57 +03:00
}
async function executeMigration (actualVersion: number, entity: { version: string, script: string }) {
2017-05-22 21:58:25 +03:00
const versionScript = parseInt(entity.version, 10)
2016-12-25 11:44:57 +03:00
// Do not execute old migration scripts
if (versionScript <= actualVersion) return undefined
2016-12-25 11:44:57 +03: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))
2018-01-10 19:18:12 +03:00
return sequelizeTypescript.transaction(async t => {
2017-02-16 21:19:56 +03:00
const options = {
transaction: t,
2017-12-12 19:53:50 +03:00
queryInterface: sequelizeTypescript.getQueryInterface(),
sequelize: sequelizeTypescript
2017-02-16 21:19:56 +03:00
}
2016-12-25 11:44:57 +03:00
await migrationScript.up(options)
// Update the new migration version
2017-12-13 19:46:23 +03:00
await sequelizeTypescript.query('UPDATE "application" SET "migrationVersion" = ' + versionScript, { transaction: t })
2016-12-25 11:44:57 +03:00
})
}