Fix webfinger validator
[oweals/peertube.git] / server / initializers / migrator.ts
index 187c9be6e5bb3abb305a886434826d2fadb02425..bb2539fc88e3297b8f1af44df4b7acf027b05317 100644 (file)
@@ -1,19 +1,32 @@
 import * as path from 'path'
-
-import { database as db } from './database'
-import { LAST_MIGRATION_VERSION } from './constants'
 import { logger, readdirPromise } from '../helpers'
+import { LAST_MIGRATION_VERSION } from './constants'
+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
+
+  // 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
+    }
+  }
+
   if (actualVersion === null) {
-    await db.Application.create({ migrationVersion: 0 })
+    await sequelizeTypescript.query('INSERT INTO "application" ("migrationVersion") VALUES (0)')
     actualVersion = 0
   }
 
@@ -78,17 +91,16 @@ async function executeMigration (actualVersion: number, entity: { version: strin
 
   const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName))
 
-  await db.sequelize.transaction(async t => {
+  await 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 })
   })
 }