mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
refactor(db): generate the Postgres baseline in one pass, check it in two modes
--check migrated a temporary SQLite database twice: once inside the generation and again to read the triggers for the port check. The generation now returns the triggers it already read, so a check is one migration. main is split into writeMode and checkMode, and the frozen-or-not decision lives in one place, checkBaseline. A baseline whose `through` is the newest migration must match the entities and port every SQLite trigger; a baseline frozen behind a newer migration is only checked by its manifest. The old code made the same decision twice, once for the match and once for the triggers, and the two could drift apart. The migrated-database read is split into listing the objects and reading them, so the temporary file is removed in one place. Manifest parsing uses a type guard, isBaselineManifest, instead of casts after a check, and PGBASE-004 pins what it refuses.
This commit is contained in:
@@ -41,17 +41,26 @@ export function renderBaseline(baseline: PostgresBaseline): { sql: string; manif
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether `data` is a manifest a baseline can be read from: a `through` naming
|
||||
* one of a non-empty list of `migrations`, every entry a string.
|
||||
*/
|
||||
function isBaselineManifest(data: unknown): data is BaselineManifest {
|
||||
if (typeof data !== 'object' || data === null || !('through' in data) || !('migrations' in data)) return false;
|
||||
const { through, migrations } = data;
|
||||
return (
|
||||
typeof through === 'string' &&
|
||||
Array.isArray(migrations) &&
|
||||
migrations.length > 0 &&
|
||||
migrations.every((name): name is string => typeof name === 'string') &&
|
||||
migrations.includes(through)
|
||||
);
|
||||
}
|
||||
|
||||
function parseManifest(text: string, file: string): BaselineManifest {
|
||||
const data: unknown = JSON.parse(text);
|
||||
const manifest = data as Partial<BaselineManifest>;
|
||||
const valid =
|
||||
typeof manifest.through === 'string' &&
|
||||
Array.isArray(manifest.migrations) &&
|
||||
manifest.migrations.length > 0 &&
|
||||
manifest.migrations.every((name) => typeof name === 'string') &&
|
||||
manifest.migrations.includes(manifest.through);
|
||||
if (!valid) throw new Error(`${file} must name "through" and list it among "migrations"`);
|
||||
return { through: manifest.through as string, migrations: manifest.migrations as string[] };
|
||||
if (!isBaselineManifest(data)) throw new Error(`${file} must name "through" and list it among "migrations"`);
|
||||
return { through: data.through, migrations: data.migrations };
|
||||
}
|
||||
|
||||
/** Reads the committed baseline; a missing or malformed one throws, so a boot cannot start from half a schema. */
|
||||
|
||||
Reference in New Issue
Block a user