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