X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Finitializers%2Fmigrator.ts;h=77203ae24139ac9f40cceb7a86f1927af545687f;hb=1fa23d6f5e487f3c149e1f0001beadd919ee82fc;hp=9ebc57f0764d3caf631b3433af60f315ae68c536;hpb=2ccaeeb341ffe8c2609039bf4c6d8835b4650316;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/initializers/migrator.ts b/server/initializers/migrator.ts index 9ebc57f07..77203ae24 100644 --- a/server/initializers/migrator.ts +++ b/server/initializers/migrator.ts @@ -1,8 +1,9 @@ import * as path from 'path' -import { readdirPromise } from '../helpers/core-utils' import { logger } from '../helpers/logger' import { LAST_MIGRATION_VERSION } from './constants' import { sequelizeTypescript } from './database' +import { readdir } from 'fs-extra' +import { QueryTypes } from 'sequelize' async function migrate () { const tables = await sequelizeTypescript.getQueryInterface().showAllTables() @@ -11,19 +12,16 @@ async function migrate () { // The installer will do that if (tables.length === 0) return - let actualVersion: number = null + let actualVersion: number | null = null - // Search in "Applications" or "application" tables - try { - const [ rows ] = await sequelizeTypescript.query('SELECT "migrationVersion" FROM "Applications"') - if (rows && rows[ 0 ] && rows[ 0 ].migrationVersion) { - actualVersion = rows[ 0 ].migrationVersion - } - } catch { - const [ rows ] = await sequelizeTypescript.query('SELECT "migrationVersion" FROM "application"') - if (rows && rows[0] && rows[0].migrationVersion) { - actualVersion = rows[0].migrationVersion - } + const query = 'SELECT "migrationVersion" FROM "application"' + const options = { + type: QueryTypes.SELECT as QueryTypes.SELECT + } + + const rows = await sequelizeTypescript.query<{ migrationVersion: number }>(query, options) + if (rows?.[0]?.migrationVersion) { + actualVersion = rows[0].migrationVersion } if (actualVersion === null) { @@ -43,7 +41,7 @@ async function migrate () { try { await executeMigration(actualVersion, migrationScript) } catch (err) { - logger.error('Cannot execute migration %s.', migrationScript.version, err) + logger.error('Cannot execute migration %s.', migrationScript.version, { err }) process.exit(-1) } } @@ -60,9 +58,9 @@ export { // --------------------------------------------------------------------------- async function getMigrationScripts () { - const files = await readdirPromise(path.join(__dirname, 'migrations')) + const files = await readdir(path.join(__dirname, 'migrations')) const filesToMigrate: { - version: string, + version: string script: string }[] = []