pg-tools.ts held six concerns in 366 lines: tool resolution and process
running, the libpq connection environment, the CVE-2025-8714 version policy,
the CLEAR-SCHEMA SQL and the operations themselves. Each now has its own
module, and pg-tools.ts keeps only the operations the backup and the boot
restore call, so their import paths do not change.
- pg-process.ts: PgTool, resolvePgTool, pgConnectionEnv, PgToolMissingError
and run (stderr is summarized there, as before).
- restore-safety.ts: hasRestrictFix and the refusal. The minimum versions in
the error are built from the RESTRICT_SINCE table, so the message cannot
drift from the policy.
- clear-schema.ts: the SQL that empties the schema before a load, unchanged.
The version floor also no longer keeps its own OLDEST_MAJOR and
LAST_MAJOR_WITHOUT_IT constants: both are read from the table. Behaviour is
unchanged. The literal /dev/null is os.devNull, and the COPY parsing of a
dump's migration history is its own function.
pgRestore read the whole pg_restore --file script into one string to find
TREK's migration table before clearing the schema. The script carries every
COPY row of the dump, so a large database made the read exceed V8's maximum
string length before the check ran, and the restore failed with an
unhelpful error instead of loading or refusing.
The check now streams the script with readline and stops at the first
matching line. The match itself is unchanged: the same CREATE TABLE line for
the same quoted schema, so a dump without TREK's history is still refused
before anything is cleared. The regex's escaping is a named helper.
PGTOOLS-005b pins that the script is never read whole.
pg_dump copied every schema of the database, so on a hosted Postgres such
as Supabase a backup carried its auth and storage schemas too, and a
restore failed on them. A dump now holds the current schema only, a
restore loads only that schema, and a dump without TREK's tables in it is
refused before anything is replaced.
On Postgres the backup port dumps with pg_dump and restores by turning the
dump into a script with pg_restore and having psql clear the current schema
and run it in one transaction. A dump only recreates what it holds, so a
plain pg_restore --clean would leave a later migration's tables behind and
break the migrations the reopen runs; a restore that fails leaves the
database as it was. A restore refuses a pg_restore or psql older than
17.6/16.10/15.14/14.19, whose script would lack the \restrict line that
keeps a psql meta-command smuggled into an uploaded dump from running
(CVE-2025-8714). A restore
refuses a SQLite archive, a dump without TREK's migration history or one a
newer TREK wrote before anything is replaced, and keeps a dump of the
database it replaces.
RESTORE_FROM_BACKUP loads the archive's dump the same way into a database
that holds no table yet, and is ignored with a warning once it holds one.
Before pending migrations the boot now takes a pg_dump copy to
data/pre-migrate-<last migration>-<time>.pgdump under the same
TREK_DB_PRE_MIGRATE_SNAPSHOT(_KEEP) policy as the SQLite file, instead of
refusing; a copy that cannot be written refuses the boot.
The tools run by absolute path (TREK_DB_PG_BIN_DIR, or the usual
directories), never through PATH, with the password in PGPASSWORD; an
sslmode that asks for SSL verifies the certificate as the app pool does.
Dumps are written beside their target and renamed into place, readable by
their owner only, and a tool's stderr reaches errors without the lines that
quote SQL or rows. The image installs postgresql-client-17 from Debian 13.
- UnitOfWork transactions take one advisory lock for the database on
Postgres (DatabaseEngine.serializeWrites, pg_advisory_xact_lock), so a
MAX(...) + 1 read inside one cannot race another, as with SQLite's single
writer. SQLite needs nothing.
- ilike(platform, refs) builds the case-insensitive search condition: LIKE
on SQLite, ILIKE on Postgres, where LIKE is exact. The place searches use it.
- Plugin data erasure runs each statement in its own savepoint: on Postgres a
failed statement would abort the user's whole erasure transaction.
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.
A port for the engine TREK runs on, without changing what it runs on.
src/db/engine/ names the engine (SqliteEngine, PostgresEngine) and builds
the MikroORM config from it; src/mikro-orm.config.ts exports that config for
the CLI and the Nest container alike. Without TREK_DB_URL the config is the
one it was: the shared SQLite driver on TREK_DB_FILE or data/travel.db.
- TREK_DB_URL takes a postgres:// or postgresql:// URL and is masked in the
boot report; TREK_DB_POOL_MAX sizes the pool (1 to 100, default 10).
TREK_DB_URL beside TREK_DB_FILE, TREK_DB_JOURNAL_MODE or
TREK_DB_SYNCHRONOUS refuses the boot as an ambiguous configuration.
- MikroORM builds the pg pool from the URL's host, port, user, password and
database and drops the rest of the query, so the engine reads sslmode and
sslrootcert itself. require, verify-ca and verify-full all verify the
server's certificate, as the pg driver reads them; a mode that would not
verify, or a misspelt one, refuses.
- Postgres cannot run TREK yet, so a boot with TREK_DB_URL is refused with
that reason, in the boot check and again where the config is built, before
anything connects.
- @mikro-orm/postgresql moves to dependencies; pg is plain JavaScript.
- lint:engine-dispatch (CI) holds the engine checks outside src/db/dialect/
and src/db/engine/ (isSqlite(, isPostgres(, an instanceof on a platform, a
comparison on the engine's name) to a baseline that starts and stays empty.
The probe's baseline is empty now: no repository method Postgres refuses,
none it cannot measure.
- reservations.accommodation_id (TEXT) is cast where it is compared with
day_accommodations.id (INTEGER), in the four reservation reads and the two
stay reads; SQLite coerced the bare comparison, Postgres refuses it. The
joined RS18/RR1 read moves to reservation-join.ts beside the repository.
- The usage counters qualify the target row's column in DO UPDATE SET, which
Postgres reads as ambiguous with `excluded` otherwise.
- ReservationDayPositions.upsertScoped upserts on its primary key instead of
INSERT OR REPLACE; position is the only other column and nothing references
the table, so the row left behind is the same.
- PRAGMA wal_checkpoint and VACUUM INTO move from MaintenanceRepository to
SqliteMaintenance under src/db/engine/sqlite/, the SQLite engine's folder,
where lint:dialect allows PRAGMA.
- The places list takes category and tag ids as numbers: the service converts
the query text with toRowId and answers an empty list for one that is no
id, instead of binding the text to an integer column.
- The probe samples the `present` flag of a partial update's pair as true, so
the four update methods that sent no SQL are measured, and it now holds
SQLSTATE class 22 too: a value of the wrong type reaching a column is the
code's, since the sampler sends what each parameter declares.