]>
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 | |
075f16ca | 67 | files.forEach(file => { |
b769007f C |
68 | // Filename is something like 'version-blabla.js' |
69 | const version = file.split('-')[0] | |
70 | filesToMigrate.push({ | |
71 | version, | |
72 | script: file | |
73 | }) | |
74 | }) | |
75 | ||
6fcd19ba | 76 | return filesToMigrate |
b769007f C |
77 | }) |
78 | } | |
79 | ||
6fcd19ba | 80 | function executeMigration (actualVersion: number, entity: { version: string, script: string }) { |
e02643f3 | 81 | const versionScript = parseInt(entity.version, 10) |
b769007f C |
82 | |
83 | // Do not execute old migration scripts | |
6fcd19ba | 84 | if (versionScript <= actualVersion) return undefined |
b769007f C |
85 | |
86 | // Load the migration module and run it | |
87 | const migrationScriptName = entity.script | |
88 | logger.info('Executing %s migration script.', migrationScriptName) | |
89 | ||
90 | const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) | |
91 | ||
6fcd19ba | 92 | return db.sequelize.transaction(t => { |
4793c343 C |
93 | const options = { |
94 | transaction: t, | |
95 | queryInterface: db.sequelize.getQueryInterface(), | |
6fcd19ba | 96 | sequelize: db.sequelize |
4793c343 | 97 | } |
b769007f | 98 | |
0a6658fd | 99 | return migrationScript.up(options) |
6fcd19ba C |
100 | .then(() => { |
101 | // Update the new migration version | |
0a6658fd | 102 | return db.Application.updateMigrationVersion(versionScript, t) |
b769007f | 103 | }) |
b769007f C |
104 | }) |
105 | } |