X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Finitializers%2Fmigrator.ts;h=4663697299ff830f2825e7e5096f53f42d051deb;hb=2243730c8edf210c0a3ffc161bac89785f6a52f0;hp=4fbe1cf5b9c0c3c7aa93dd0c1bd06a606257fe7b;hpb=f5028693a896a3076dd286ac0030e3d8f78f5ebf;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/initializers/migrator.ts b/server/initializers/migrator.ts index 4fbe1cf5b..466369729 100644 --- a/server/initializers/migrator.ts +++ b/server/initializers/migrator.ts @@ -1,19 +1,25 @@ import * as path from 'path' - -import { database as db } from './database' +import { readdirPromise } from '../helpers/core-utils' +import { logger } from '../helpers/logger' import { LAST_MIGRATION_VERSION } from './constants' -import { logger, readdirPromise } from '../helpers' +import { sequelizeTypescript } from './database' async function migrate () { - const tables = await db.sequelize.getQueryInterface().showAllTables() + 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 - let actualVersion = await db.Application.loadMigrationVersion() + let actualVersion: number = null + + const [ rows ] = await sequelizeTypescript.query('SELECT "migrationVersion" FROM "application"') + if (rows && rows[0] && rows[0].migrationVersion) { + actualVersion = rows[0].migrationVersion + } + if (actualVersion === null) { - await db.Application.create({ migrationVersion: 0 }) + await sequelizeTypescript.query('INSERT INTO "application" ("migrationVersion") VALUES (0)') actualVersion = 0 } @@ -26,7 +32,12 @@ async function migrate () { const migrationScripts = await getMigrationScripts() for (const migrationScript of migrationScripts) { - await executeMigration(actualVersion, migrationScript) + try { + await executeMigration(actualVersion, migrationScript) + } catch (err) { + logger.error('Cannot execute migration %s.', migrationScript.version, { err }) + process.exit(-1) + } } logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION) @@ -73,17 +84,16 @@ async function executeMigration (actualVersion: number, entity: { version: strin const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) - await db.sequelize.transaction(async t => { + return sequelizeTypescript.transaction(async t => { const options = { transaction: t, - queryInterface: db.sequelize.getQueryInterface(), - sequelize: db.sequelize, - db + queryInterface: sequelizeTypescript.getQueryInterface(), + sequelize: sequelizeTypescript } await migrationScript.up(options) // Update the new migration version - await db.Application.updateMigrationVersion(versionScript, t) + await sequelizeTypescript.query('UPDATE "application" SET "migrationVersion" = ' + versionScript, { transaction: t }) }) }