From 0aa5b36661ef417745723e8896c434c418fc53d4 Mon Sep 17 00:00:00 2001 From: Maurice Date: Thu, 8 Oct 2026 15:25:52 +0200 Subject: [PATCH] fix(backup): back up and restore through an injected database port Backup, restore and the demo reset each handled the SQLite file themselves: backup.impl closed and reinitialized the connection, built MaintenanceRepository with new, and assumed the database was data/travel.db. That tied the whole chain to one local file and hid two bugs. They now go through DATABASE_BACKUP, a port declared in the shared database kernel (snapshot, verify, keep a copy before a restore, replace). SqliteDatabaseBackup implements it with what the code did before: VACUUM INTO for a consistent copy, and a close, copy, sidecar drop, rename and reopen through DatabaseLifecycle for a restore. BackupService injects it and hands it to backup.impl. The demo reset and the baseline save use the same port; the first baseline is saved by DemoResetJob once the app is up, because the boot-time seed runs before anything can be injected. Another engine can plug in a logical dump behind the port. Fixes on the way: - The backup and the restore ignored TREK_DB_FILE. The backup looked for data/travel.db and, with the database elsewhere, silently left it out of the archive; the restore wrote data/travel.db and never replaced the live database. Both now use DatabaseLifecycle.file, the path the connection runs on, and the pre-restore copy sits next to it. - A failed VACUUM INTO fell back to archiving the live WAL-mode file without a word, the torn copy the snapshot exists to prevent. The backup now fails: nothing is archived or committed, the existing backups are untouched, a partial snapshot is removed, and the log says why. A missing database file and a failed checkpoint are logged too. --- server/CLAUDE.md | 2 +- .../db/repositories/MaintenanceRepository.ts | 29 +- server/src/demo/demo-reset.ts | 94 +++--- server/src/demo/demo-seed.ts | 9 +- server/src/nest/README.md | 11 +- server/src/nest/admin/admin.service.ts | 8 +- server/src/nest/admin/demo-reset.job.ts | 44 ++- server/src/nest/backup/backup.impl.ts | 184 ++++-------- server/src/nest/backup/backup.module.ts | 4 + server/src/nest/backup/backup.service.ts | 29 +- .../src/nest/backup/database-backup.module.ts | 22 ++ .../src/nest/backup/sqlite-database-backup.ts | 111 +++++++ .../database/database-backup.interface.ts | 76 +++++ .../database/database-lifecycle.service.ts | 15 +- server/tests/e2e/admin.e2e.test.ts | 6 +- .../integration/database-lifecycle.test.ts | 15 +- server/tests/unit/auto-backup.test.ts | 108 +++---- .../tests/unit/demo/demo-reset-path.test.ts | 126 +++++--- server/tests/unit/nest/admin-jobs.test.ts | 55 +++- server/tests/unit/nest/admin.service.test.ts | 30 +- .../nest/admin.version-notification.test.ts | 4 + .../tests/unit/nest/backup.controller.test.ts | 13 +- server/tests/unit/nest/backup.impl.test.ts | 280 +++++++++++------- .../unit/nest/sqlite-database-backup.test.ts | 217 ++++++++++++++ wiki/Backups.md | 6 +- wiki/Demo-Mode.md | 4 +- 26 files changed, 1029 insertions(+), 473 deletions(-) create mode 100644 server/src/nest/backup/database-backup.module.ts create mode 100644 server/src/nest/backup/sqlite-database-backup.ts create mode 100644 server/src/nest/database/database-backup.interface.ts create mode 100644 server/tests/unit/nest/sqlite-database-backup.test.ts diff --git a/server/CLAUDE.md b/server/CLAUDE.md index 0259f516e..e82f7e307 100644 --- a/server/CLAUDE.md +++ b/server/CLAUDE.md @@ -42,7 +42,7 @@ Nest owns everything. Every domain is a DI module under `src/nest//` (`c - **Scheduling**: `src/nest/scheduling/` (`CronRegistrarService`) is the one way to schedule a cron. Jobs are `*.job.ts` providers in their owning domain. The registrar refuses to schedule under `NODE_ENV=test` (an integration test pins it). **Never use a `@Cron` decorator** — it bypasses that gate. - **Plugins** (`src/nest/plugins/`): the child-process runtime (`supervisor/`, `runtime/plugin-host-entry.ts`, `protocol/envelope.ts`) is **intentionally not Nest** — it is the security boundary. `host/rpc-host.ts` registers an RPC method at spawn only if the plugin holds the unlocking permission (registration = authorization). Install-time gates live in `install/`; the manifest's `trek` version range fails closed, and `TREK_PLUGINS_IGNORE_TREK_RANGE` downgrades that to a warning. The protocol tables in `protocol/envelope.ts` (+ `src/plugin-event-sink.ts`) are **generated** into `plugin-sdk/` and `shared/` — run `gen:plugin-facts` after touching them and commit the output. `runtime/egress-policy.ts` is a hand-kept mirror guarded by a parity test in `plugin-sdk/`; change both sides together. - **Storage**: `src/nest/storage/` is the upload driver abstraction (local / S3 / mirror behind one interface, with an admin surface and a usage job). Domain services never touch the filesystem or the S3 SDK directly. -- **DB** (`src/db/`): `better-sqlite3`, WAL, FK on. `database.ts` only holds the connection and applies pragmas. It does not build a schema, and importing it opens nothing (the first reader does). The connection's lifecycle belongs to the `DatabaseLifecycle` provider (`nest/database/database-lifecycle.service.ts`): `buildApp()` opens it through `open()` (which binds the ORM to later swaps and runs the schema bootstrap below), the shutdown closes it through `close()`, and `reopen()` swaps in a new handle and migrates it. `DatabaseLifecycle.file` is the one resolved path (`TREK_DB_FILE` or `data/travel.db`). MikroORM's migrations (`db/migrations/`) are the only schema source, **including for tests**: the hand-kept `schema.ts`/`migrations.ts`/`seeds.ts` builders and the parity test that guarded them are gone. Seeding is the MikroORM seeders in `db/seeders/`, applied by `db/orm.ts` from `buildApp()` before anything reads. An install the retired positional runner migrated (a `schema_version` row, no `mikro_orm_migrations` rows) is baselined by `db/legacy-baseline.ts`: the migrations whose `Legacy migration step N` docstring is ≤ its version are recorded as executed (never replayed) in the same transaction as the migrator run, while the baseline migration itself still runs first, as the legacy runner's `createTables()` did on every boot; anything it cannot map refuses the boot. Before a boot applies pending migrations to a file database that holds anything, `db/pre-migrate-snapshot.ts` writes a `VACUUM INTO` copy next to it (`pre-migrate--