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