2023-07-31 08:34:36 -04:00
|
|
|
import { readdir } from 'fs/promises'
|
2021-08-27 08:32:44 -04:00
|
|
|
import { join } from 'path'
|
|
|
|
import { QueryTypes } from 'sequelize'
|
2023-07-31 08:34:36 -04:00
|
|
|
import { currentDir } from '@peertube/peertube-node-utils'
|
|
|
|
import { logger } from '../helpers/logger.js'
|
|
|
|
import { LAST_MIGRATION_VERSION } from './constants.js'
|
|
|
|
import { sequelizeTypescript } from './database.js'
|
2017-07-05 07:26:25 -04:00
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
async function migrate () {
|
2017-12-12 11:53:50 -05:00
|
|
|
const tables = await sequelizeTypescript.getQueryInterface().showAllTables()
|
2017-03-04 04:40:09 -05:00
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
// No tables, we don't need to migrate anything
|
|
|
|
// The installer will do that
|
|
|
|
if (tables.length === 0) return
|
2017-02-18 05:56:28 -05:00
|
|
|
|
2018-07-25 16:01:25 -04:00
|
|
|
let actualVersion: number | null = null
|
2017-12-13 11:46:23 -05:00
|
|
|
|
2019-04-18 05:28:17 -04:00
|
|
|
const query = 'SELECT "migrationVersion" FROM "application"'
|
|
|
|
const options = {
|
|
|
|
type: QueryTypes.SELECT as QueryTypes.SELECT
|
|
|
|
}
|
|
|
|
|
|
|
|
const rows = await sequelizeTypescript.query<{ migrationVersion: number }>(query, options)
|
2020-01-31 10:56:52 -05:00
|
|
|
if (rows?.[0]?.migrationVersion) {
|
2018-03-23 04:16:21 -04:00
|
|
|
actualVersion = rows[0].migrationVersion
|
2017-12-13 11:46:23 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
if (actualVersion === null) {
|
2017-12-13 11:46:23 -05:00
|
|
|
await sequelizeTypescript.query('INSERT INTO "application" ("migrationVersion") VALUES (0)')
|
2017-10-25 10:03:33 -04:00
|
|
|
actualVersion = 0
|
|
|
|
}
|
2016-09-26 16:36:36 -04:00
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
// No need migrations, abort
|
|
|
|
if (actualVersion >= LAST_MIGRATION_VERSION) return
|
2016-09-26 16:36:36 -04:00
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
// If there are a new migration scripts
|
|
|
|
logger.info('Begin migrations.')
|
|
|
|
|
|
|
|
const migrationScripts = await getMigrationScripts()
|
2016-09-26 16:36:36 -04:00
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
for (const migrationScript of migrationScripts) {
|
2017-11-27 03:47:21 -05:00
|
|
|
try {
|
|
|
|
await executeMigration(actualVersion, migrationScript)
|
|
|
|
} catch (err) {
|
2018-03-26 09:54:13 -04:00
|
|
|
logger.error('Cannot execute migration %s.', migrationScript.version, { err })
|
2018-01-10 11:18:12 -05:00
|
|
|
process.exit(-1)
|
2017-11-27 03:47:21 -05:00
|
|
|
}
|
2017-10-25 10:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)
|
2016-09-26 16:36:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
|
|
|
migrate
|
|
|
|
}
|
2016-09-26 16:36:36 -04:00
|
|
|
|
2016-12-25 03:44:57 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
async function getMigrationScripts () {
|
2023-07-31 08:34:36 -04:00
|
|
|
const files = await readdir(join(currentDir(import.meta.url), 'migrations'))
|
2017-10-25 10:03:33 -04:00
|
|
|
const filesToMigrate: {
|
2020-01-31 10:56:52 -05:00
|
|
|
version: string
|
2017-10-25 10:03:33 -04:00
|
|
|
script: string
|
|
|
|
}[] = []
|
|
|
|
|
|
|
|
files
|
2021-12-17 08:20:43 -05:00
|
|
|
.filter(file => file.endsWith('.js'))
|
2017-10-25 10:03:33 -04:00
|
|
|
.forEach(file => {
|
|
|
|
// Filename is something like 'version-blabla.js'
|
|
|
|
const version = file.split('-')[0]
|
|
|
|
filesToMigrate.push({
|
|
|
|
version,
|
|
|
|
script: file
|
2016-12-25 03:44:57 -05:00
|
|
|
})
|
2017-10-25 10:03:33 -04:00
|
|
|
})
|
2016-12-25 03:44:57 -05:00
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
return filesToMigrate
|
2016-12-25 03:44:57 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
async function executeMigration (actualVersion: number, entity: { version: string, script: string }) {
|
2017-05-22 14:58:25 -04:00
|
|
|
const versionScript = parseInt(entity.version, 10)
|
2016-12-25 03:44:57 -05:00
|
|
|
|
|
|
|
// Do not execute old migration scripts
|
2017-07-05 07:26:25 -04:00
|
|
|
if (versionScript <= actualVersion) return undefined
|
2016-12-25 03:44:57 -05:00
|
|
|
|
|
|
|
// Load the migration module and run it
|
|
|
|
const migrationScriptName = entity.script
|
|
|
|
logger.info('Executing %s migration script.', migrationScriptName)
|
|
|
|
|
2023-07-31 08:34:36 -04:00
|
|
|
const migrationScript = await import(join(currentDir(import.meta.url), 'migrations', migrationScriptName))
|
2016-12-25 03:44:57 -05:00
|
|
|
|
2018-01-10 11:18:12 -05:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
2017-02-16 13:19:56 -05:00
|
|
|
const options = {
|
|
|
|
transaction: t,
|
2017-12-12 11:53:50 -05:00
|
|
|
queryInterface: sequelizeTypescript.getQueryInterface(),
|
|
|
|
sequelize: sequelizeTypescript
|
2017-02-16 13:19:56 -05:00
|
|
|
}
|
2016-12-25 03:44:57 -05:00
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
await migrationScript.up(options)
|
|
|
|
|
|
|
|
// Update the new migration version
|
2017-12-13 11:46:23 -05:00
|
|
|
await sequelizeTypescript.query('UPDATE "application" SET "migrationVersion" = ' + versionScript, { transaction: t })
|
2016-12-25 03:44:57 -05:00
|
|
|
})
|
|
|
|
}
|