mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
feat(db): write a migration's schema change once per engine
A migration after the Postgres baseline's cut runs on both engines, but its DDL
is not one language. engineDdl(this, { sqlite, postgres }) queues the statements
of the engine the migration runs on, like addSql, and addColumnIfMissing takes a
column definition per engine, both picked in the dialect layer so a migration
never branches on the engine itself.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { isPostgres, isSqlite, unsupported } from './platform';
|
||||
import type { Platform } from '@mikro-orm/core';
|
||||
|
||||
/**
|
||||
* Schema statements a migration writes once per engine.
|
||||
*
|
||||
* A migration after the Postgres baseline's cut (`through` in
|
||||
* `assets/postgres/baseline.json`) runs on both engines, and their DDL is not
|
||||
* one language: SQLite rebuilds a table where Postgres alters it, and the
|
||||
* Postgres side has to read exactly as MikroORM generates it from the entities
|
||||
* (`bigint`, `bigserial` ids, `<table>_<column>_foreign` keys), or PARITY-PG
|
||||
* fails. So a migration hands both spellings to `engineDdl` in
|
||||
* `db/migration-utils.ts`, which picks the one for the engine it runs on here;
|
||||
* `npm run gen:pg-migration` writes the Postgres side and `lint:dialect`
|
||||
* refuses DDL a newer migration spells outside it.
|
||||
*/
|
||||
|
||||
/** One value per engine, picked by the platform the migration runs on. */
|
||||
interface PerEngine<T> {
|
||||
readonly sqlite: T;
|
||||
readonly postgres: T;
|
||||
}
|
||||
|
||||
/** The statements of one schema change, in the order they run, per engine. */
|
||||
export type EngineDdl = PerEngine<readonly string[]>;
|
||||
|
||||
/** A column definition (`status TEXT DEFAULT 'pending'`) per engine, for `addColumnIfMissing`. */
|
||||
export type EngineColumn = PerEngine<string>;
|
||||
|
||||
/** The member of `choices` for the engine behind `platform`; any other engine is refused. */
|
||||
export function forEngine<T>(platform: Platform, choices: PerEngine<T>): T {
|
||||
if (isSqlite(platform)) return choices.sqlite;
|
||||
if (isPostgres(platform)) return choices.postgres;
|
||||
return unsupported(platform);
|
||||
}
|
||||
Reference in New Issue
Block a user