]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/initializers/migrator.ts
Add configuration for prometheus exporter hostname
[github/Chocobozzz/PeerTube.git] / server / initializers / migrator.ts
index 71a656c59b74d65d6127d018907c9c52365e8584..7ac20127e866a0deee77d6179b0ea8c1706fb5e5 100644 (file)
@@ -1,52 +1,52 @@
-import * as path from 'path'
-import * as Promise from 'bluebird'
-
-import { database as db } from './database'
+import { readdir } from 'fs-extra'
+import { join } from 'path'
+import { QueryTypes } from 'sequelize'
+import { logger } from '../helpers/logger'
 import { LAST_MIGRATION_VERSION } from './constants'
-import { logger, readdirPromise } from '../helpers'
-
-function migrate () {
-  const p = db.sequelize.getQueryInterface().showAllTables()
-    .then(tables => {
-      // No tables, we don't need to migrate anything
-      // The installer will do that
-      if (tables.length === 0) throw null
-    })
-    .then(() => {
-      return db.Application.loadMigrationVersion()
-    })
-    .then(actualVersion => {
-      if (actualVersion === null) {
-        return db.Application.create({ migrationVersion: 0 }).then(() => 0)
-      }
+import { sequelizeTypescript } from './database'
 
-      return actualVersion
-    })
-    .then(actualVersion => {
-      // No need migrations, abort
-      if (actualVersion >= LAST_MIGRATION_VERSION) throw null
+async function migrate () {
+  const tables = await sequelizeTypescript.getQueryInterface().showAllTables()
 
-      return actualVersion
-    })
-    .then(actualVersion => {
-      // If there are a new migration scripts
-      logger.info('Begin migrations.')
+  // No tables, we don't need to migrate anything
+  // The installer will do that
+  if (tables.length === 0) return
 
-      return getMigrationScripts().then(migrationScripts => ({ actualVersion, migrationScripts }))
-    })
-    .then(({ actualVersion, migrationScripts }) => {
-      return Promise.each(migrationScripts, entity => executeMigration(actualVersion, entity))
-    })
-    .then(() => {
-      logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)
-    })
-    .catch(err => {
-      if (err === null) return undefined
+  let actualVersion: number | null = null
 
-      throw err
-    })
+  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
+  }
 
-  return p
+  if (actualVersion === null) {
+    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) {
+    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)
 }
 
 // ---------------------------------------------------------------------------
@@ -57,14 +57,16 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function getMigrationScripts () {
-  return readdirPromise(path.join(__dirname, 'migrations')).then(files => {
-    const filesToMigrate: {
-      version: string,
-      script: string
-    }[] = []
+async function getMigrationScripts () {
+  const files = await readdir(join(__dirname, 'migrations'))
+  const filesToMigrate: {
+    version: string
+    script: string
+  }[] = []
 
-    files.forEach(file => {
+  files
+    .filter(file => file.endsWith('.js'))
+    .forEach(file => {
       // Filename is something like 'version-blabla.js'
       const version = file.split('-')[0]
       filesToMigrate.push({
@@ -73,11 +75,10 @@ function getMigrationScripts () {
       })
     })
 
-    return filesToMigrate
-  })
+  return filesToMigrate
 }
 
-function executeMigration (actualVersion: number, entity: { version: string, script: string }) {
+async function executeMigration (actualVersion: number, entity: { version: string, script: string }) {
   const versionScript = parseInt(entity.version, 10)
 
   // Do not execute old migration scripts
@@ -87,19 +88,18 @@ function executeMigration (actualVersion: number, entity: { version: string, scr
   const migrationScriptName = entity.script
   logger.info('Executing %s migration script.', migrationScriptName)
 
-  const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName))
+  const migrationScript = require(join(__dirname, 'migrations', migrationScriptName))
 
-  return db.sequelize.transaction(t => {
+  return sequelizeTypescript.transaction(async t => {
     const options = {
       transaction: t,
-      queryInterface: db.sequelize.getQueryInterface(),
-      sequelize: db.sequelize
+      queryInterface: sequelizeTypescript.getQueryInterface(),
+      sequelize: sequelizeTypescript
     }
 
-    return migrationScript.up(options)
-      .then(() => {
-        // Update the new migration version
-        return db.Application.updateMigrationVersion(versionScript, t)
-      })
+    await migrationScript.up(options)
+
+    // Update the new migration version
+    await sequelizeTypescript.query('UPDATE "application" SET "migrationVersion" = ' + versionScript, { transaction: t })
   })
 }