feat(db): copy the database before a boot migrates it

Migrations only go forward, and since #2610 an older image refuses a
database a newer one migrated. Going back after a bad update therefore
needs a copy from before it, and the only copies were backups the admin
takes by hand or auto-backup, which is off by default. The first boot
on 4.4 migrates every 4.3.x install through the legacy baseline and the
new migrations without either.

Before applying pending migrations to a file database that holds
anything, the boot now writes a VACUUM INTO copy next to it, named
pre-migrate-<schema>-<time>.db (legacy-<schema_version> for an install
the positional runner left, otherwise the last recorded migration), and
keeps the newest TREK_DB_PRE_MIGRATE_SNAPSHOT_KEEP (3). The copy is
written under a temporary name and renamed, so a crash never leaves a
truncated file that looks good. If it cannot be written the boot refuses
before anything is migrated; TREK_DB_PRE_MIGRATE_SNAPSHOT=false is the
documented way past that. The newer-database refusal names the copy
taken right before the newer release migrated past this one.

A fresh install, a restart with nothing pending, an in-memory database
and a restore's re-bootstrap (the archive is the copy) take none, and
NODE_ENV=test leaves it off unless a suite sets the switch.
This commit is contained in:
Maurice
2026-10-08 11:35:07 +02:00
parent 395a7f41fe
commit ff3615fdf0
14 changed files with 486 additions and 10 deletions
+13 -5
View File
@@ -1,6 +1,7 @@
import mikroOrmConfig from '../mikro-orm.config';
import { registerReinitializeHook, runDemoSeed } from './database';
import { migrateToHead } from './legacy-baseline';
import { migrateToHead, NO_SAFETY_NET } from './legacy-baseline';
import { preMigrateSnapshot } from './pre-migrate-snapshot';
import { withRequestContext } from '../nest/database/request-context';
import type { AnyEntity, EntityClass, EntityManager, EntitySchema, IDatabaseDriver, MikroORM } from '@mikro-orm/core';
import type { Migrator } from '@mikro-orm/migrations';
@@ -29,14 +30,21 @@ type AnyOrm = MikroORM<
readonly (string | EntityClass<AnyEntity> | EntitySchema)[]
>;
/** Migrate to head, then seed. Safe to call again — both halves are idempotent. */
export async function runSchemaBootstrap(orm: AnyOrm): Promise<void> {
/**
* Migrate to head, then seed. Safe to call again, both halves are idempotent.
*
* `snapshot: false` skips the pre-migration copy (`pre-migrate-snapshot.ts`).
* Only the restore hook passes it: the file it migrates was just unpacked from
* a backup archive, which is still there to go back to.
*/
export async function runSchemaBootstrap(orm: AnyOrm, { snapshot = true }: { snapshot?: boolean } = {}): Promise<void> {
const migrator = orm.config.getExtension('@mikro-orm/migrator') as Migrator;
const connection = orm.em.getConnection();
// A database the retired positional runner migrated has no migrator rows yet;
// what it already has is recorded in the same transaction as the run, or the
// run would replay the whole history over it (see legacy-baseline.ts). Every
// other database just gets its pending migrations.
await migrateToHead(orm.em.getConnection(), migrator);
await migrateToHead(connection, migrator, undefined, undefined, snapshot ? preMigrateSnapshot(connection) : NO_SAFETY_NET);
const defaultSeeder = orm.config.get('seeder').defaultSeeder;
if (defaultSeeder) await orm.seeder.seedString(defaultSeeder);
@@ -77,7 +85,7 @@ export function attachOrm(orm: AnyOrm): void {
// `destroy()` is a no-op (see `orm-driver.ts`), so this close never reaches it.
await connection.close(true);
await connection.connect();
await runSchemaBootstrap(orm);
await runSchemaBootstrap(orm, { snapshot: false });
});
}