feat(db): create and migrate the schema on a Postgres database

With TREK_DB_URL set, TREK now boots on Postgres; without it nothing changes.

- The database engine contract gains open, close, reopen and the schema
  bootstrap; DatabaseLifecycle delegates to the engine resolveEngine() picks.
- An empty Postgres database gets server/assets/postgres/baseline.sql
  (generated from the entities by gen:pg-baseline, with the reference rows
  the migrations up to its cut insert: tour types, provider catalogues,
  addons, app settings) and every migration up to its cut recorded as
  executed, in one transaction. A database holding tables
  without TREK's history is refused before anything is written; a migrated one
  runs the newer-database refusal and its pending migrations, which refuse
  without a copy unless TREK_DB_PRE_MIGRATE_SNAPSHOT=false.
- INTEGER columns are bigint on Postgres (SQLite's INTEGER is 64-bit: an
  epoch-millisecond column overflowed int4), and the engine's driver reads
  bigint and numeric back as numbers, refusing values past 2^53.
- The two SQLite triggers are ported to PL/pgSQL; check:pg-baseline fails
  while a SQLite trigger has no port, and while the baseline drifts from the
  entities before a later migration freezes it.
- Backups and restores on Postgres refuse until they can dump and load one.
- Migration helpers read the catalog per engine (dialect/catalog.ts), and
  the email case index no longer needs group_concat.
- CI runs check:pg-baseline and boots the image on postgres:17 twice.
This commit is contained in:
Isaias Tavares
2026-11-02 14:25:34 -03:00
parent 25f5e34558
commit a77705984e
40 changed files with 2655 additions and 229 deletions
+24 -2
View File
@@ -1,15 +1,18 @@
import { closeDb, getRawConnection, registerReinitializeHook, reinitialize } from '../../database';
import { resolveDbPath } from '../../db-path';
import { runSchemaBootstrap, type AnyOrm } from '../../orm';
import { SharedSqliteDriver } from '../../orm-driver';
import type { DatabaseEngine } from '../engine';
/**
* The SQLite file engine, TREK's default: one better-sqlite3 handle that
* `db/database.ts` owns and the ORM's bound driver shares (`db/orm-driver.ts`),
* on `TREK_DB_FILE` or `data/travel.db` (`:memory:` under test).
* on `TREK_DB_FILE` or `data/travel.db` (`:memory:` under test). A restore
* swaps the file and reopens the handle; the schema comes from the migration
* chain (`db/orm.ts`).
*/
export class SqliteEngine implements DatabaseEngine {
readonly id = 'sqlite';
readonly ready = true;
describe(): string {
return `SQLite file ${resolveDbPath()}`;
@@ -18,4 +21,23 @@ export class SqliteEngine implements DatabaseEngine {
ormOptions() {
return { driver: SharedSqliteDriver, dbName: resolveDbPath() };
}
open(onReopened: () => Promise<void>): void {
// Normally a no-op: the ORM's first connect inside NestFactory.create()
// already opened it. Said here so the boot does not depend on that.
getRawConnection();
registerReinitializeHook(onReopened);
}
close(): void {
closeDb();
}
reopen(): Promise<void> {
return reinitialize();
}
schemaBootstrap(orm: AnyOrm, options: { snapshot: boolean }): Promise<void> {
return runSchemaBootstrap(orm, options);
}
}