feat(migrations): apply unapplied migrations on init

This commit is contained in:
zkldi
2022-04-21 17:28:59 +01:00
parent c1900e1c93
commit 9fe155bf71
2 changed files with 25 additions and 0 deletions
+22
View File
@@ -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.
+3
View File
@@ -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) {