]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as path from 'path' |
00d6b0dd | 2 | |
e02643f3 | 3 | import { database as db } from './database' |
65fcc311 | 4 | import { LAST_MIGRATION_VERSION } from './constants' |
6fcd19ba C |
5 | import { logger, readdirPromise } from '../helpers' |
6 | ||
f5028693 C |
7 | async function migrate () { |
8 | const tables = await db.sequelize.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 | |
f5028693 C |
14 | let actualVersion = await db.Application.loadMigrationVersion() |
15 | if (actualVersion === null) { | |
16 | await db.Application.create({ migrationVersion: 0 }) | |
17 | actualVersion = 0 | |
18 | } | |
00d6b0dd | 19 | |
f5028693 C |
20 | // No need migrations, abort |
21 | if (actualVersion >= LAST_MIGRATION_VERSION) return | |
00d6b0dd | 22 | |
f5028693 C |
23 | // If there are a new migration scripts |
24 | logger.info('Begin migrations.') | |
25 | ||
26 | const migrationScripts = await getMigrationScripts() | |
00d6b0dd | 27 | |
f5028693 | 28 | for (const migrationScript of migrationScripts) { |
74bb2cb8 C |
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 | } | |
f5028693 C |
35 | } |
36 | ||
37 | logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION) | |
00d6b0dd C |
38 | } |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | ||
65fcc311 C |
42 | export { |
43 | migrate | |
44 | } | |
00d6b0dd | 45 | |
b769007f C |
46 | // --------------------------------------------------------------------------- |
47 | ||
f5028693 C |
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 | |
b769007f | 63 | }) |
f5028693 | 64 | }) |
b769007f | 65 | |
f5028693 | 66 | return filesToMigrate |
b769007f C |
67 | } |
68 | ||
f5028693 | 69 | async function executeMigration (actualVersion: number, entity: { version: string, script: string }) { |
e02643f3 | 70 | const versionScript = parseInt(entity.version, 10) |
b769007f C |
71 | |
72 | // Do not execute old migration scripts | |
6fcd19ba | 73 | if (versionScript <= actualVersion) return undefined |
b769007f C |
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 | ||
f5028693 | 81 | await db.sequelize.transaction(async t => { |
4793c343 C |
82 | const options = { |
83 | transaction: t, | |
84 | queryInterface: db.sequelize.getQueryInterface(), | |
93e1258c C |
85 | sequelize: db.sequelize, |
86 | db | |
4793c343 | 87 | } |
b769007f | 88 | |
f5028693 C |
89 | await migrationScript.up(options) |
90 | ||
91 | // Update the new migration version | |
92 | await db.Application.updateMigrationVersion(versionScript, t) | |
b769007f C |
93 | }) |
94 | } |