aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrator.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/initializers/migrator.ts')
-rw-r--r--server/initializers/migrator.ts107
1 files changed, 44 insertions, 63 deletions
diff --git a/server/initializers/migrator.ts b/server/initializers/migrator.ts
index 7b535aea9..4fbe1cf5b 100644
--- a/server/initializers/migrator.ts
+++ b/server/initializers/migrator.ts
@@ -1,52 +1,35 @@
1import * as path from 'path' 1import * as path from 'path'
2import * as Promise from 'bluebird'
3 2
4import { database as db } from './database' 3import { database as db } from './database'
5import { LAST_MIGRATION_VERSION } from './constants' 4import { LAST_MIGRATION_VERSION } from './constants'
6import { logger, readdirPromise } from '../helpers' 5import { logger, readdirPromise } from '../helpers'
7 6
8function migrate () { 7async function migrate () {
9 const p = db.sequelize.getQueryInterface().showAllTables() 8 const tables = await db.sequelize.getQueryInterface().showAllTables()
10 .then(tables => {
11 // No tables, we don't need to migrate anything
12 // The installer will do that
13 if (tables.length === 0) throw null
14 })
15 .then(() => {
16 return db.Application.loadMigrationVersion()
17 })
18 .then(actualVersion => {
19 if (actualVersion === null) {
20 return db.Application.create({ migrationVersion: 0 }).then(() => 0)
21 }
22 9
23 return actualVersion 10 // No tables, we don't need to migrate anything
24 }) 11 // The installer will do that
25 .then(actualVersion => { 12 if (tables.length === 0) return
26 // No need migrations, abort
27 if (actualVersion >= LAST_MIGRATION_VERSION) throw null
28 13
29 return actualVersion 14 let actualVersion = await db.Application.loadMigrationVersion()
30 }) 15 if (actualVersion === null) {
31 .then(actualVersion => { 16 await db.Application.create({ migrationVersion: 0 })
32 // If there are a new migration scripts 17 actualVersion = 0
33 logger.info('Begin migrations.') 18 }
34 19
35 return getMigrationScripts().then(migrationScripts => ({ actualVersion, migrationScripts })) 20 // No need migrations, abort
36 }) 21 if (actualVersion >= LAST_MIGRATION_VERSION) return
37 .then(({ actualVersion, migrationScripts }) => {
38 return Promise.each(migrationScripts, entity => executeMigration(actualVersion, entity))
39 })
40 .then(() => {
41 logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)
42 })
43 .catch(err => {
44 if (err === null) return undefined
45 22
46 throw err 23 // If there are a new migration scripts
47 }) 24 logger.info('Begin migrations.')
25
26 const migrationScripts = await getMigrationScripts()
48 27
49 return p 28 for (const migrationScript of migrationScripts) {
29 await executeMigration(actualVersion, migrationScript)
30 }
31
32 logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)
50} 33}
51 34
52// --------------------------------------------------------------------------- 35// ---------------------------------------------------------------------------
@@ -57,29 +40,28 @@ export {
57 40
58// --------------------------------------------------------------------------- 41// ---------------------------------------------------------------------------
59 42
60function getMigrationScripts () { 43async function getMigrationScripts () {
61 return readdirPromise(path.join(__dirname, 'migrations')).then(files => { 44 const files = await readdirPromise(path.join(__dirname, 'migrations'))
62 const filesToMigrate: { 45 const filesToMigrate: {
63 version: string, 46 version: string,
64 script: string 47 script: string
65 }[] = [] 48 }[] = []
66 49
67 files 50 files
68 .filter(file => file.endsWith('.js.map') === false) 51 .filter(file => file.endsWith('.js.map') === false)
69 .forEach(file => { 52 .forEach(file => {
70 // Filename is something like 'version-blabla.js' 53 // Filename is something like 'version-blabla.js'
71 const version = file.split('-')[0] 54 const version = file.split('-')[0]
72 filesToMigrate.push({ 55 filesToMigrate.push({
73 version, 56 version,
74 script: file 57 script: file
75 })
76 }) 58 })
59 })
77 60
78 return filesToMigrate 61 return filesToMigrate
79 })
80} 62}
81 63
82function executeMigration (actualVersion: number, entity: { version: string, script: string }) { 64async function executeMigration (actualVersion: number, entity: { version: string, script: string }) {
83 const versionScript = parseInt(entity.version, 10) 65 const versionScript = parseInt(entity.version, 10)
84 66
85 // Do not execute old migration scripts 67 // Do not execute old migration scripts
@@ -91,7 +73,7 @@ function executeMigration (actualVersion: number, entity: { version: string, scr
91 73
92 const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) 74 const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName))
93 75
94 return db.sequelize.transaction(t => { 76 await db.sequelize.transaction(async t => {
95 const options = { 77 const options = {
96 transaction: t, 78 transaction: t,
97 queryInterface: db.sequelize.getQueryInterface(), 79 queryInterface: db.sequelize.getQueryInterface(),
@@ -99,10 +81,9 @@ function executeMigration (actualVersion: number, entity: { version: string, scr
99 db 81 db
100 } 82 }
101 83
102 return migrationScript.up(options) 84 await migrationScript.up(options)
103 .then(() => { 85
104 // Update the new migration version 86 // Update the new migration version
105 return db.Application.updateMigrationVersion(versionScript, t) 87 await db.Application.updateMigrationVersion(versionScript, t)
106 })
107 }) 88 })
108} 89}