mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +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:
+15
-35
@@ -1,4 +1,3 @@
|
||||
import mikroOrmConfig from '../mikro-orm.config';
|
||||
import { withRequestContext } from '../nest/database/request-context';
|
||||
import { runDemoSeed } from './database';
|
||||
import { ensureEmailCaseIndex } from './email-case-index';
|
||||
@@ -6,16 +5,11 @@ import { migrateToHead, NO_SAFETY_NET } from './legacy-baseline';
|
||||
import { preMigrateSnapshot } from './pre-migrate-snapshot';
|
||||
import type { AnyEntity, EntityClass, EntityManager, EntitySchema, IDatabaseDriver, MikroORM } from '@mikro-orm/core';
|
||||
import type { Migrator } from '@mikro-orm/migrations';
|
||||
import { MikroORM as SqliteMikroORM } from '@mikro-orm/sqlite';
|
||||
|
||||
/**
|
||||
* Schema ownership at startup.
|
||||
*
|
||||
* This replaces `createTables()` → `runMigrations()` → `runSeeds()`, which used
|
||||
* to run synchronously inside `database.ts::initDb()` at module load. The
|
||||
* migrator is async, so the work moved to `buildApp()`, which is the first place
|
||||
* in the boot with an await and still runs before anything reads the database
|
||||
* (`bootstrap.ts` resolves SettingsService right after).
|
||||
* Schema ownership at startup. The migrator is async, so the schema is brought
|
||||
* to head from `DatabaseLifecycle.open()`, which `buildApp()` calls before
|
||||
* anything reads the database. Nothing runs at module load.
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -35,10 +29,10 @@ export type AnyOrm = MikroORM<
|
||||
* Migrate to head, then seed. Safe to call again, both halves are idempotent.
|
||||
*
|
||||
* `snapshot: false` skips the pre-migration copy (`pre-migrate-snapshot.ts`).
|
||||
* Only the restore hook passes it: the file it migrates was just unpacked from
|
||||
* a backup archive, which is still there to go back to.
|
||||
* Only the reopen after a restore passes it: the file it migrates was just
|
||||
* unpacked from a backup archive, which is still there to go back to.
|
||||
*/
|
||||
export async function runSchemaBootstrap(orm: AnyOrm, { snapshot = true }: { snapshot?: boolean } = {}): Promise<void> {
|
||||
export async function runSchemaBootstrap(orm: AnyOrm, { snapshot }: { snapshot: boolean }): Promise<void> {
|
||||
const migrator = orm.config.getExtension('@mikro-orm/migrator') as Migrator;
|
||||
const connection = orm.em.getConnection();
|
||||
// A database the retired positional runner migrated has no migrator rows yet;
|
||||
@@ -52,8 +46,16 @@ export async function runSchemaBootstrap(orm: AnyOrm, { snapshot = true }: { sna
|
||||
undefined,
|
||||
snapshot ? preMigrateSnapshot(connection) : NO_SAFETY_NET,
|
||||
);
|
||||
await finishSchemaBootstrap(orm);
|
||||
}
|
||||
|
||||
/**
|
||||
* What every engine does once its schema is at head: the deferred index, the
|
||||
* seeders, the demo seed. Idempotent, like the migration step before it.
|
||||
*/
|
||||
export async function finishSchemaBootstrap(orm: AnyOrm): Promise<void> {
|
||||
// The one index a migration may have had to defer (see email-case-index.ts).
|
||||
await ensureEmailCaseIndex(connection);
|
||||
await ensureEmailCaseIndex(orm.em.getConnection());
|
||||
|
||||
const defaultSeeder = orm.config.get('seeder').defaultSeeder;
|
||||
if (defaultSeeder) await orm.seeder.seedString(defaultSeeder);
|
||||
@@ -77,25 +79,3 @@ export async function runSchemaBootstrap(orm: AnyOrm, { snapshot = true }: { sna
|
||||
// fully-synchronous version had by construction.
|
||||
await withRequestContext(orm, () => runDemoSeed());
|
||||
}
|
||||
|
||||
/**
|
||||
* An ORM instance outside Nest, for the vitest global setup that builds the
|
||||
* schema snapshot. Production uses the container-owned one from
|
||||
* `MikroOrmModule.forRoot()` instead of creating a second.
|
||||
*/
|
||||
export function createStandaloneOrm(): Promise<AnyOrm> {
|
||||
return SqliteMikroORM.init({
|
||||
...mikroOrmConfig,
|
||||
// Entity discovery is skipped deliberately. Migrations and seeders are raw
|
||||
// SQL and need no metadata, and the global setup that calls this runs as ESM
|
||||
// in vitest's main process, where discovering `*.entity.ts` fails on Node's
|
||||
// extensionless import resolution.
|
||||
entities: [],
|
||||
entitiesTs: [],
|
||||
discovery: { warnWhenNoEntities: false },
|
||||
// The migrator's per-migration progress log is production boot output; the
|
||||
// global setup already reports the snapshot in one line of its own, and a
|
||||
// failing migration still throws.
|
||||
migrations: { ...mikroOrmConfig.migrations, silent: true },
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user