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
+37
View File
@@ -0,0 +1,37 @@
import { MikroORM } from '@mikro-orm/postgresql';
/**
* The databases the Postgres lane creates on the server TREK_TEST_DB_URL names:
* one migrated template per run, and one copy of it per test file.
*/
export const TEMPLATE_DATABASE = 'trek_test_template';
export const WORKER_DATABASE_PREFIX = 'trek_test_w';
/** `adminUrl` pointed at `database`, everything else (credentials, host, sslmode) kept. */
export function databaseUrl(adminUrl: string, database: string): string {
const url = new URL(adminUrl);
url.pathname = `/${database}`;
return url.toString();
}
type Run = <T = unknown>(sql: string, params?: unknown[]) => Promise<T[]>;
/**
* Runs `fn` on a connection to the admin database, outside any transaction
* (CREATE and DROP DATABASE refuse to run in one), through the same Postgres
* driver the app uses.
*/
export async function withAdmin<T>(adminUrl: string, fn: (run: Run) => Promise<T>): Promise<T> {
const orm = await MikroORM.init({
clientUrl: adminUrl,
entities: [],
discovery: { warnWhenNoEntities: false },
pool: { min: 0, max: 1 },
});
try {
const connection = orm.em.getConnection();
return await fn((sql, params = []) => connection.execute(sql, params, 'all'));
} finally {
await orm.close(true);
}
}