aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrator.ts
blob: 4b3be6d166fb901ded0be147491b127fb3320e4f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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)
      }

      return actualVersion
    })
    .then(actualVersion => {
      // No need migrations, abort
      if (actualVersion >= LAST_MIGRATION_VERSION) throw null

      return actualVersion
    })
    .then(actualVersion => {
      // If there are a new migration scripts
      logger.info('Begin migrations.')

      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

      throw err
    })

  return p
}

// ---------------------------------------------------------------------------

export {
  migrate
}

// ---------------------------------------------------------------------------

function getMigrationScripts () {
  return readdirPromise(path.join(__dirname, 'migrations')).then(files => {
    const filesToMigrate: {
      version: string,
      script: string
    }[] = []

    files.forEach(function (file) {
      // Filename is something like 'version-blabla.js'
      const version = file.split('-')[0]
      filesToMigrate.push({
        version,
        script: file
      })
    })

    return filesToMigrate
  })
}

function executeMigration (actualVersion: number, entity: { version: string, script: string }) {
  const versionScript = parseInt(entity.version, 10)

  // Do not execute old migration scripts
  if (versionScript <= actualVersion) return undefined

  // Load the migration module and run it
  const migrationScriptName = entity.script
  logger.info('Executing %s migration script.', migrationScriptName)

  const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName))

  return db.sequelize.transaction(t => {
    const options = {
      transaction: t,
      queryInterface: db.sequelize.getQueryInterface(),
      sequelize: db.sequelize
    }

    return migrationScript.up(options)
      .then(() => {
        // Update the new migration version
        return db.Application.updateMigrationVersion(versionScript, t)
      })
  })
}