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