]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as path from 'path' |
6fcd19ba | 2 | import * as Promise from 'bluebird' |
00d6b0dd | 3 | |
e02643f3 | 4 | import { database as db } from './database' |
65fcc311 | 5 | import { LAST_MIGRATION_VERSION } from './constants' |
6fcd19ba C |
6 | import { logger, readdirPromise } from '../helpers' |
7 | ||
8 | function migrate () { | |
9 | const p = 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 => { | |
ed708d62 | 19 | if (actualVersion === null) { |
6fcd19ba | 20 | return db.Application.create({ migrationVersion: 0 }).then(() => 0) |
ed708d62 C |
21 | } |
22 | ||
6fcd19ba C |
23 | return actualVersion |
24 | }) | |
25 | .then(actualVersion => { | |
26 | // No need migrations, abort | |
27 | if (actualVersion >= LAST_MIGRATION_VERSION) throw null | |
5804c0db | 28 | |
6fcd19ba C |
29 | return actualVersion |
30 | }) | |
31 | .then(actualVersion => { | |
5804c0db | 32 | // If there are a new migration scripts |
00d6b0dd C |
33 | logger.info('Begin migrations.') |
34 | ||
6fcd19ba C |
35 | return getMigrationScripts().then(migrationScripts => ({ actualVersion, migrationScripts })) |
36 | }) | |
37 | .then(({ actualVersion, migrationScripts }) => { | |
709756b8 | 38 | return Promise.each(migrationScripts, entity => executeMigration(actualVersion, entity)) |
6fcd19ba C |
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 | |
00d6b0dd | 45 | |
6fcd19ba C |
46 | throw err |
47 | }) | |
00d6b0dd | 48 | |
6fcd19ba | 49 | return p |
00d6b0dd C |
50 | } |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | ||
65fcc311 C |
54 | export { |
55 | migrate | |
56 | } | |
00d6b0dd | 57 | |
b769007f C |
58 | // --------------------------------------------------------------------------- |
59 | ||
6fcd19ba C |
60 | function getMigrationScripts () { |
61 | return readdirPromise(path.join(__dirname, 'migrations')).then(files => { | |
62 | const filesToMigrate: { | |
63 | version: string, | |
64 | script: string | |
65 | }[] = [] | |
b769007f | 66 | |
93e1258c C |
67 | files |
68 | .filter(file => file.endsWith('.js.map') === false) | |
69 | .forEach(file => { | |
70 | // Filename is something like 'version-blabla.js' | |
71 | const version = file.split('-')[0] | |
72 | filesToMigrate.push({ | |
73 | version, | |
74 | script: file | |
75 | }) | |
b769007f | 76 | }) |
b769007f | 77 | |
6fcd19ba | 78 | return filesToMigrate |
b769007f C |
79 | }) |
80 | } | |
81 | ||
6fcd19ba | 82 | function executeMigration (actualVersion: number, entity: { version: string, script: string }) { |
e02643f3 | 83 | const versionScript = parseInt(entity.version, 10) |
b769007f C |
84 | |
85 | // Do not execute old migration scripts | |
6fcd19ba | 86 | if (versionScript <= actualVersion) return undefined |
b769007f C |
87 | |
88 | // Load the migration module and run it | |
89 | const migrationScriptName = entity.script | |
90 | logger.info('Executing %s migration script.', migrationScriptName) | |
91 | ||
92 | const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) | |
93 | ||
6fcd19ba | 94 | return db.sequelize.transaction(t => { |
4793c343 C |
95 | const options = { |
96 | transaction: t, | |
97 | queryInterface: db.sequelize.getQueryInterface(), | |
93e1258c C |
98 | sequelize: db.sequelize, |
99 | db | |
4793c343 | 100 | } |
b769007f | 101 | |
0a6658fd | 102 | return migrationScript.up(options) |
6fcd19ba C |
103 | .then(() => { |
104 | // Update the new migration version | |
0a6658fd | 105 | return db.Application.updateMigrationVersion(versionScript, t) |
b769007f | 106 | }) |
b769007f C |
107 | }) |
108 | } |