Files
TREK/server/src/db/orm.ts
T
Maurice 67049c1065 fix(backup): a restore that migrates no longer breaks the request it runs in
Restoring a backup whose database was behind the running version migrated it inside the admin's HTTP request. A MikroORM migration's getEntityManager() pins the migration's transaction on the EntityManager of the context it runs in, which was the request's. Once the migrations committed, every later read on that request failed with "Transaction is already committed": the storage registry reload kept the pre-restore config (uploads, including the archive's own, went to the old backend), and the restore's audit row was lost.

Both engines now run their migrations in a request context of their own (runSchemaBootstrap on SQLite, bootstrapPostgresSchema on Postgres), so the transaction stays on a throwaway EntityManager, at boot and on restore alike. RESTORE-002 restores a file six migrations behind head inside a request and reads on through that request's EntityManager.

Closes #3264
2026-11-29 12:00:08 +01:00

94 lines
4.2 KiB
TypeScript

import { withRequestContext } from '../nest/database/request-context';
import { runDemoSeed } from './database';
import { ensureEmailCaseIndex } from './email-case-index';
import { migrateToHead, NO_SAFETY_NET } from './legacy-baseline';
import { preMigrateSnapshot } from './pre-migrate-snapshot';
import { warnAboutSchemaGaps } from './schema-gaps';
import type { AnyEntity, EntityClass, EntityManager, EntitySchema, IDatabaseDriver, MikroORM } from '@mikro-orm/core';
import type { Migrator } from '@mikro-orm/migrations';
/**
* Schema ownership at startup. The migrator is async, so the schema is brought
* to head from `DatabaseLifecycle.open()`, which `buildApp()` calls before
* anything reads the database. Nothing runs at module load.
*/
/**
* Whatever MikroORM instance this process holds.
*
* Widened on the entity list because Nest's `MikroORM` token resolves to the
* class's own `readonly` form while a direct `init()` yields the mutable one;
* nothing here reads that parameter.
*/
export type AnyOrm = MikroORM<
IDatabaseDriver,
EntityManager<IDatabaseDriver>,
readonly (string | EntityClass<AnyEntity> | EntitySchema)[]
>;
/**
* 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 reopen after a restore 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 }: { 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.
//
// In a request context of its own (#3264): a migration's getEntityManager()
// pins the migration's transaction on the EntityManager of the context it
// runs in. A restore migrates inside the admin's HTTP request, so that was
// the request's, which then failed every later read with "Transaction is
// already committed" (the storage registry reload, the audit row).
await withRequestContext(orm, () =>
migrateToHead(
connection,
migrator,
undefined,
undefined,
snapshot ? preMigrateSnapshot(connection) : NO_SAFETY_NET,
),
);
await finishSchemaBootstrap(orm);
}
/**
* What every engine does once its schema is at head: the deferred index, the
* check for tables and columns the history left out, the seeders, the demo
* seed. Idempotent, like the migration step before it.
*/
export async function finishSchemaBootstrap(orm: AnyOrm): Promise<void> {
// The one index a migration may have had to defer (see email-case-index.ts).
await ensureEmailCaseIndex(orm.em.getConnection());
// Names what the entities map and the database lacks (see schema-gaps.ts).
await warnAboutSchemaGaps(orm);
const defaultSeeder = orm.config.get('seeder').defaultSeeder;
if (defaultSeeder) await orm.seeder.seedString(defaultSeeder);
// Demo data depends on the seeded categories (hard-coded ids under an FK), so
// it can only run once the seeders above have.
//
// Plan 3c Task 0b (R9): wrapping the call HERE, the one place
// `runSchemaBootstrap` already has `orm` in scope, covers BOTH of
// `runDemoSeed`'s real call sites at once (`DatabaseLifecycle.open()` at
// boot and its restore hook in `reopen()`, both of which only ever reach
// `runDemoSeed` through this function) with no second wrapper to keep in
// sync.
//
// Plan 3i Task 3: `runDemoSeed` (and `seedDemoData` beneath it, converted
// onto `DemoRepository`) is `async` now — this call is awaited so a
// rejection surfaces here rather than becoming an unhandled promise
// rejection, and so the seed genuinely completes before
// `runSchemaBootstrap` (and therefore `buildApp()`/this hook's own
// restore path) returns to its caller, the same completion guarantee the
// fully-synchronous version had by construction.
await withRequestContext(orm, () => runDemoSeed());
}