]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/initializers/migrator.ts
Generate application keys too
[github/Chocobozzz/PeerTube.git] / server / initializers / migrator.ts
index 4b3be6d166fb901ded0be147491b127fb3320e4f..4fbe1cf5b9c0c3c7aa93dd0c1bd06a606257fe7b 100644 (file)
@@ -1,52 +1,35 @@
 import * as path from 'path'
-import * as Promise from 'bluebird'
 
 import { database as db } from './database'
 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)
-      }
+async function migrate () {
+  const tables = await db.sequelize.getQueryInterface().showAllTables()
 
-      return actualVersion
-    })
-    .then(actualVersion => {
-      // No need migrations, abort
-      if (actualVersion >= LAST_MIGRATION_VERSION) throw null
+  // No tables, we don't need to migrate anything
+  // The installer will do that
+  if (tables.length === 0) return
 
-      return actualVersion
-    })
-    .then(actualVersion => {
-      // If there are a new migration scripts
-      logger.info('Begin migrations.')
+  let actualVersion = await db.Application.loadMigrationVersion()
+  if (actualVersion === null) {
+    await db.Application.create({ migrationVersion: 0 })
+    actualVersion = 0
+  }
 
-      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
+  // No need migrations, abort
+  if (actualVersion >= LAST_MIGRATION_VERSION) return
 
-      throw err
-    })
+  // If there are a new migration scripts
+  logger.info('Begin migrations.')
+
+  const migrationScripts = await getMigrationScripts()
+
+  for (const migrationScript of migrationScripts) {
+    await executeMigration(actualVersion, migrationScript)
+  }
 
-  return p
+  logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)
 }
 
 // ---------------------------------------------------------------------------
@@ -57,14 +40,16 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function getMigrationScripts () {
-  return readdirPromise(path.join(__dirname, 'migrations')).then(files => {
-    const filesToMigrate: {
-      version: string,
-      script: string
-    }[] = []
+async function getMigrationScripts () {
+  const files = await readdirPromise(path.join(__dirname, 'migrations'))
+  const filesToMigrate: {
+    version: string,
+    script: string
+  }[] = []
 
-    files.forEach(function (file) {
+  files
+    .filter(file => file.endsWith('.js.map') === false)
+    .forEach(file => {
       // Filename is something like 'version-blabla.js'
       const version = file.split('-')[0]
       filesToMigrate.push({
@@ -73,11 +58,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
@@ -89,17 +73,17 @@ function executeMigration (actualVersion: number, entity: { version: string, scr
 
   const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName))
 
-  return db.sequelize.transaction(t => {
+  await db.sequelize.transaction(async t => {
     const options = {
       transaction: t,
       queryInterface: db.sequelize.getQueryInterface(),
-      sequelize: db.sequelize
+      sequelize: db.sequelize,
+      db
     }
 
-    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 db.Application.updateMigrationVersion(versionScript, t)
   })
 }