feat(db): run TREK on PostgreSQL

TREK runs on PostgreSQL when TREK_DB_URL is set, and on SQLite as before
otherwise: an engine port under src/db/engine, the dialect helpers under
src/db/dialect, a Postgres baseline for the migrations up to the cut,
pg_dump/pg_restore backups, a probe of every repository method against a
real server and a Postgres test lane in CI.
This commit is contained in:
Isaias Tavares
2026-11-05 01:15:21 -03:00
committed by Maurice
parent 17b47b38b9
commit 2e3762afe1
176 changed files with 6728 additions and 777 deletions
+17
View File
@@ -14,6 +14,7 @@
* call, which is what keeps the runtime env-mutation semantics the test suite
* depends on. Zod validation runs once at boot (env.schema.ts), never here.
*/
import { DB_POOL_MAX_CEILING, DB_POOL_MAX_DEFAULT, type DbEngineSettings } from './db-settings';
import { imageVersion } from './image-version';
import {
csvList,
@@ -348,9 +349,25 @@ export function deriveBackup(raw: RawEnv) {
};
}
/** The database settings `readEnv().db` answers with. */
export type DbSettings = ReturnType<typeof deriveDb>;
export function deriveDb(raw: RawEnv) {
const durability = resolveDurability(raw.TREK_DB_JOURNAL_MODE, raw.TREK_DB_SYNCHRONOUS);
const url = raw.TREK_DB_URL?.trim() || undefined;
const engine: DbEngineSettings = url
? {
engine: 'postgres',
// The connection URL carries the password, so it is never logged (db/engine/redact-url.ts).
url,
poolMax: Math.min(positiveIntOr(raw.TREK_DB_POOL_MAX, DB_POOL_MAX_DEFAULT), DB_POOL_MAX_CEILING),
}
: { engine: 'sqlite' };
return {
/** Which database this process runs on: Postgres when TREK_DB_URL names one, the SQLite file otherwise. */
...engine,
/** Where pg_dump and pg_restore are; unset, the usual install directories are tried (never PATH). */
pgBinDir: raw.TREK_DB_PG_BIN_DIR?.trim() || undefined,
trekDbFile: raw.TREK_DB_FILE,
/** Resolved journal_mode — WAL unless the operator asked for something else (network storage needs DELETE/TRUNCATE). */
journalMode: durability.journalMode,