fix(demo): save the first baseline after a reopen that seeded too

The demo seed runs in the schema bootstrap, at boot and again after every
reopen. Since the baseline save moved out of the seed into DemoResetJob,
only the boot read the seed's mark. A restore onto a demo instance with no
baseline, whose archive held no admin trips, seeded the example trips
during the reopen and then saved nothing, so the hourly reset kept logging
"No baseline found"; the mark also stayed set for the rest of the process.
Before the move, that path saved the baseline from inside the seed.

DatabaseLifecycle now runs listeners after a reopen's schema bootstrap,
and DemoResetJob registers one in demo mode that does the same check as at
boot: save through the backup port when the seed just ran and no baseline
exists. Each check clears the mark. A failed save is logged, never turned
into a failed reopen. AdminModule imports DatabaseLifecycleModule from the
shared database kernel for it.
This commit is contained in:
Maurice
2026-10-08 15:48:38 +02:00
parent 1dfc5dd8ea
commit 3e1234112d
9 changed files with 149 additions and 18 deletions
@@ -22,6 +22,8 @@ import { runSchemaBootstrap } from '../../db/orm';
*/
@Injectable()
export class DatabaseLifecycle {
private readonly reopenListeners: Array<() => Promise<void>> = [];
constructor(private readonly orm: MikroORM) {}
/**
@@ -61,6 +63,20 @@ export class DatabaseLifecycle {
await reinitialize();
}
/**
* Runs `listener` after every reopen, once the reopened file has been through
* the schema bootstrap (migrations, seeders, demo seed). For work that has to
* follow what that bootstrap did and needs the container, which the bootstrap
* itself has no access to: the demo reset job saves the first demo baseline
* here when a restore's re-bootstrap seeded the example trips.
*
* A listener contains its own failures. One that throws turns into the
* reopen's error, which a restore reports as "restart required".
*/
onReopened(listener: () => Promise<void>): void {
this.reopenListeners.push(listener);
}
/**
* Kysely caches whatever handle it was given, so without this the ORM would
* keep talking to the closed one. Closing and reconnecting sends the driver
@@ -75,5 +91,6 @@ export class DatabaseLifecycle {
// No pre-migrate snapshot: the file was just unpacked from an archive that
// is still there to go back to.
await runSchemaBootstrap(this.orm, { snapshot: false });
for (const listener of this.reopenListeners) await listener();
}
}