mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
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:
@@ -0,0 +1,61 @@
|
||||
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';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
|
||||
/**
|
||||
* 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). 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';
|
||||
|
||||
describe(): string {
|
||||
return `SQLite file ${resolveDbPath()}`;
|
||||
}
|
||||
|
||||
ormOptions() {
|
||||
return { driver: SharedSqliteDriver, dbName: resolveDbPath() };
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the file (normally a no-op: the ORM's first connect inside
|
||||
* NestFactory.create() already did, said here so the boot does not depend on
|
||||
* that), and registers the reopen. A reopen swaps the file under the same
|
||||
* handle, which Kysely caches, so the ORM's connection is closed and connected
|
||||
* again first: the driver then reaches the new handle through
|
||||
* `createKyselyDialect()`. The bound driver's `destroy()` is a no-op
|
||||
* (`orm-driver.ts`), so the close never reaches the handle `db/database.ts` owns.
|
||||
*/
|
||||
open(orm: AnyOrm, onReopened: () => Promise<void>): void {
|
||||
getRawConnection();
|
||||
registerReinitializeHook(async () => {
|
||||
const connection = orm.em.getConnection();
|
||||
await connection.close(true);
|
||||
await connection.connect();
|
||||
await onReopened();
|
||||
});
|
||||
}
|
||||
|
||||
close(): void {
|
||||
closeDb();
|
||||
}
|
||||
|
||||
reopen(): Promise<void> {
|
||||
return reinitialize();
|
||||
}
|
||||
|
||||
schemaBootstrap(orm: AnyOrm, options: { snapshot: boolean }): Promise<void> {
|
||||
return runSchemaBootstrap(orm, options);
|
||||
}
|
||||
|
||||
/** A no-op: SQLite has one connection and one writer, so its transactions already run one at a time. */
|
||||
serializeWrites(_em: EntityManager): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
|
||||
/**
|
||||
* The SQLite engine's whole-file maintenance: the two statements a backup,
|
||||
* a restore and the demo reset need that are about the database file rather
|
||||
* than any table (`PRAGMA wal_checkpoint`, `VACUUM INTO`). Neither has a
|
||||
* Postgres spelling, so they live under the engine that owns the file instead
|
||||
* of in `MaintenanceRepository`, whose remaining statements run on every
|
||||
* engine (and which the Postgres probe calls method by method).
|
||||
*
|
||||
* Provided through Nest DI by `MaintenanceModule` (`nest/database/`), a
|
||||
* factory over the context-resolving global `EntityManager`, and injected by
|
||||
* the SQLite backup port (`SqliteDatabaseBackup`). Free of Nest decorators
|
||||
* like every other file under `db/`, so a test can build one over its own
|
||||
* `EntityManager`.
|
||||
*/
|
||||
export class SqliteMaintenance {
|
||||
constructor(private readonly em: EntityManager) {}
|
||||
|
||||
/**
|
||||
* MikroORM's own validating `getContext()`: throws outside a request
|
||||
* context, the fail-closed check `TrekRepository` applies to every
|
||||
* repository, since a raw `connection.execute()` has no such guard.
|
||||
*/
|
||||
private validateRequestContext(): void {
|
||||
this.em.getContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* `PRAGMA wal_checkpoint(TRUNCATE)`: flushes the WAL into the main file
|
||||
* before a snapshot or a swap. Reached through the backup port's
|
||||
* `checkpoint()`; every caller treats it as best effort.
|
||||
*/
|
||||
async walCheckpoint(): Promise<void> {
|
||||
this.validateRequestContext();
|
||||
await this.em.getConnection().execute('PRAGMA wal_checkpoint(TRUNCATE)');
|
||||
}
|
||||
|
||||
/**
|
||||
* `` VACUUM INTO '<path>' ``: a consistent point-in-time copy of the file
|
||||
* under concurrent writers, reached through the backup port's
|
||||
* `snapshot()`. Single quotes in `path` are escaped by doubling.
|
||||
*/
|
||||
async vacuumInto(path: string): Promise<void> {
|
||||
this.validateRequestContext();
|
||||
const escaped = path.replaceAll("'", "''");
|
||||
await this.em.getConnection().execute(`VACUUM INTO '${escaped}'`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user