diff --git a/server/src/lib/migration/migrations.ts b/server/src/lib/migration/migrations.ts index a91c60a0c..ba0570576 100644 --- a/server/src/lib/migration/migrations.ts +++ b/server/src/lib/migration/migrations.ts @@ -57,6 +57,28 @@ export async function FindUnappliedMigrations() { return notApplied; } +export async function ApplyUnappliedMigrations() { + const unapplied = await FindUnappliedMigrations(); + + if (unapplied.length === 0) { + logger.verbose(`Found no migrations to apply. Not bothering.`); + return; + } + + logger.info( + `Found ${ + unapplied.length + } migration(s) that have not been applied yet. These are '${unapplied.join("', '")}'.`, + { unapplied } + ); + + for (const migrationID of unapplied) { + // These things need to apply in lockstep. + // eslint-disable-next-line no-await-in-loop + await ApplyMigration(migrationID); + } +} + /** * Applies a migration. Will **EXIT TACHI** if a migration fails to apply, as this implies * database level inconsistencies which could cause severe problems. diff --git a/server/src/main.ts b/server/src/main.ts index f600ab784..a3de83609 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -13,6 +13,7 @@ import { LoadDefaultClients } from "lib/builtin-clients/builtin-clients"; import { VERSION_PRETTY } from "lib/constants/version"; import { HandleSIGTERMGracefully } from "lib/handlers/sigterm"; import CreateLogCtx from "lib/logger/logger"; +import { ApplyUnappliedMigrations } from "lib/migration/migrations"; import { Environment, ServerConfig, TachiConfig } from "lib/setup/config"; import path from "path"; import server from "server/server"; @@ -32,6 +33,8 @@ async function RunOnInit() { await InitSequenceDocs(); await SetIndexesIfNoneSet(); + await ApplyUnappliedMigrations(); + await db["folder-chart-lookup"].findOne().then((r) => { // If there are no folder chart lookups, initialise them. if (!r) {