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