Files
TREK/server/tests/build-schema-snapshot.ts
T
Isaias Tavares a77705984e 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.
2026-11-04 22:45:02 -03:00

43 lines
1.9 KiB
TypeScript

// Migrates one database and serialises it for the test run to reuse.
//
// Run as a child process under tsx by tests/global-setup.ts — not imported by
// it. vitest's globalSetup executes in the main process outside vitest's
// transform pipeline, where Node's native resolver rejects the extensionless
// imports these CommonJS-target modules use ('../migration-utils'). tsx resolves
// them, and it is already how `gen:plugin-facts` runs TypeScript directly.
import { schemaSnapshotPath, writeSchemaSnapshot } from '../src/db/schema-snapshot';
import fs from 'node:fs';
async function main(): Promise<void> {
// Delete first, and open the database only afterwards: in test mode `openDb()`
// opens a copy of this snapshot when one exists, so a leftover from the last
// run would have this process migrating an already-migrated database and
// seeding it twice. The modules are loaded dynamically below so nothing of
// them runs before the delete.
fs.rmSync(schemaSnapshotPath(), { force: true });
const { getRawConnection } = await import('../src/db/database');
const { runSchemaBootstrap } = await import('../src/db/orm');
const { createStandaloneOrm } = await import('../src/db/standalone-orm');
// The shared driver binds to the connection database.ts opens on the ORM's
// first connect, so this migrates that in-memory database and it can be
// serialised straight after.
const orm = await createStandaloneOrm();
try {
await runSchemaBootstrap(orm);
writeSchemaSnapshot(getRawConnection().serialize());
console.log(`[test-db] schema snapshot written to ${schemaSnapshotPath()}`);
} finally {
// Closing destroys Kysely's client, which closes the handle — after the
// snapshot has been taken.
await orm.close(true);
}
}
main().catch((err: unknown) => {
console.error('[test-db] failed to build the schema snapshot:', err);
process.exit(1);
});