]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as path from 'path' |
da854ddd C |
2 | import { readdirPromise } from '../helpers/core-utils' |
3 | import { logger } from '../helpers/logger' | |
3fd3ab2d C |
4 | import { LAST_MIGRATION_VERSION } from './constants' |
5 | import { sequelizeTypescript } from './database' | |
6fcd19ba | 6 | |
f5028693 | 7 | async function migrate () { |
3fd3ab2d | 8 | const tables = await sequelizeTypescript.getQueryInterface().showAllTables() |
ed708d62 | 9 | |
f5028693 C |
10 | // No tables, we don't need to migrate anything |
11 | // The installer will do that | |
12 | if (tables.length === 0) return | |
5804c0db | 13 | |
91fea9fc C |
14 | let actualVersion: number = null |
15 | ||
3660d6c3 C |
16 | const [ rows ] = await sequelizeTypescript.query('SELECT "migrationVersion" FROM "application"') |
17 | if (rows && rows[0] && rows[0].migrationVersion) { | |
18 | actualVersion = rows[0].migrationVersion | |
91fea9fc C |
19 | } |
20 | ||
f5028693 | 21 | if (actualVersion === null) { |
91fea9fc | 22 | await sequelizeTypescript.query('INSERT INTO "application" ("migrationVersion") VALUES (0)') |
f5028693 C |
23 | actualVersion = 0 |
24 | } | |
00d6b0dd | 25 | |
f5028693 C |
26 | // No need migrations, abort |
27 | if (actualVersion >= LAST_MIGRATION_VERSION) return | |
00d6b0dd | 28 | |
f5028693 C |
29 | // If there are a new migration scripts |
30 | logger.info('Begin migrations.') | |
31 | ||
32 | const migrationScripts = await getMigrationScripts() | |
00d6b0dd | 33 | |
f5028693 | 34 | for (const migrationScript of migrationScripts) { |
74bb2cb8 C |
35 | try { |
36 | await executeMigration(actualVersion, migrationScript) | |
37 | } catch (err) { | |
38 | logger.error('Cannot execute migration %s.', migrationScript.version, err) | |
2ccaeeb3 | 39 | process.exit(-1) |
74bb2cb8 | 40 | } |
f5028693 C |
41 | } |
42 | ||
43 | logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION) | |
00d6b0dd C |
44 | } |
45 | ||
46 | // --------------------------------------------------------------------------- | |
47 | ||
65fcc311 C |
48 | export { |
49 | migrate | |
50 | } | |
00d6b0dd | 51 | |
b769007f C |
52 | // --------------------------------------------------------------------------- |
53 | ||
f5028693 C |
54 | async function getMigrationScripts () { |
55 | const files = await readdirPromise(path.join(__dirname, 'migrations')) | |
56 | const filesToMigrate: { | |
57 | version: string, | |
58 | script: string | |
59 | }[] = [] | |
60 | ||
61 | files | |
62 | .filter(file => file.endsWith('.js.map') === false) | |
63 | .forEach(file => { | |
64 | // Filename is something like 'version-blabla.js' | |
65 | const version = file.split('-')[0] | |
66 | filesToMigrate.push({ | |
67 | version, | |
68 | script: file | |
b769007f | 69 | }) |
f5028693 | 70 | }) |
b769007f | 71 | |
f5028693 | 72 | return filesToMigrate |
b769007f C |
73 | } |
74 | ||
f5028693 | 75 | async function executeMigration (actualVersion: number, entity: { version: string, script: string }) { |
e02643f3 | 76 | const versionScript = parseInt(entity.version, 10) |
b769007f C |
77 | |
78 | // Do not execute old migration scripts | |
6fcd19ba | 79 | if (versionScript <= actualVersion) return undefined |
b769007f C |
80 | |
81 | // Load the migration module and run it | |
82 | const migrationScriptName = entity.script | |
83 | logger.info('Executing %s migration script.', migrationScriptName) | |
84 | ||
85 | const migrationScript = require(path.join(__dirname, 'migrations', migrationScriptName)) | |
86 | ||
2ccaeeb3 | 87 | return sequelizeTypescript.transaction(async t => { |
4793c343 C |
88 | const options = { |
89 | transaction: t, | |
3fd3ab2d C |
90 | queryInterface: sequelizeTypescript.getQueryInterface(), |
91 | sequelize: sequelizeTypescript | |
4793c343 | 92 | } |
b769007f | 93 | |
f5028693 C |
94 | await migrationScript.up(options) |
95 | ||
96 | // Update the new migration version | |
91fea9fc | 97 | await sequelizeTypescript.query('UPDATE "application" SET "migrationVersion" = ' + versionScript, { transaction: t }) |
b769007f C |
98 | }) |
99 | } |