mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +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:
@@ -11,6 +11,7 @@ import {
|
||||
writePostgresBaseline,
|
||||
type PostgresBaseline,
|
||||
} from '../../../../../src/db/engine/postgres/baseline';
|
||||
import { POSTGRES_TRIGGERS } from '../../../../../src/db/engine/postgres/postgres-triggers';
|
||||
import { knownMigrationNames } from '../../../../../src/db/known-migrations';
|
||||
|
||||
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
@@ -67,6 +68,7 @@ describe('Postgres baseline files', () => {
|
||||
});
|
||||
|
||||
describe('checkBaseline', () => {
|
||||
const PORTED = Object.keys(POSTGRES_TRIGGERS);
|
||||
const generated = (through: string, migrations: string[]): PostgresBaseline => ({
|
||||
through,
|
||||
migrations,
|
||||
@@ -74,27 +76,50 @@ describe('checkBaseline', () => {
|
||||
});
|
||||
|
||||
it('PGBASE-010: at the newest migration the baseline must be exactly what the entities generate', () => {
|
||||
expect(checkBaseline(sample, generated(B, [A, B]), true)).toEqual([]);
|
||||
expect(checkBaseline(sample, generated(B, [A, B]), false)).toEqual([
|
||||
expect(checkBaseline(sample, generated(B, [A, B]), true, PORTED)).toEqual([]);
|
||||
expect(checkBaseline(sample, generated(B, [A, B]), false, PORTED)).toEqual([
|
||||
'the entities generate a different schema; run npm run gen:pg-baseline',
|
||||
]);
|
||||
});
|
||||
|
||||
it('PGBASE-011: once a migration lands after the cut, the baseline is frozen and only its manifest is checked', () => {
|
||||
expect(checkBaseline(sample, generated(C, [A, B, C]), false)).toEqual([]);
|
||||
expect(checkBaseline(sample, generated(C, [A, B, C]), false, PORTED)).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGBASE-013: while not frozen, a SQLite trigger without a port is reported; once frozen the triggers are not checked', () => {
|
||||
const withNewTrigger = [...PORTED, 'trg_new_trigger'];
|
||||
expect(checkBaseline(sample, generated(B, [A, B]), true, withNewTrigger)).toEqual([
|
||||
'SQLite triggers without a Postgres port in POSTGRES_TRIGGERS: trg_new_trigger',
|
||||
]);
|
||||
expect(checkBaseline(sample, generated(C, [A, B, C]), false, withNewTrigger)).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGBASE-012: a manifest naming a migration that is gone, or missing one before its cut, is reported', () => {
|
||||
expect(checkBaseline(sample, generated(C, [B, C]), true)).toEqual([
|
||||
expect(checkBaseline(sample, generated(C, [B, C]), true, PORTED)).toEqual([
|
||||
`the baseline names migrations that do not exist: ${A}`,
|
||||
]);
|
||||
const skipped = { ...sample, migrations: [B] };
|
||||
expect(checkBaseline(skipped, generated(B, [A, B]), true)).toEqual([
|
||||
expect(checkBaseline(skipped, generated(B, [A, B]), true, PORTED)).toEqual([
|
||||
`migrations up to ${B} are missing from it: ${A}`,
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('readPostgresBaseline manifest', () => {
|
||||
it('PGBASE-004: a manifest whose migrations are not all names, or are empty, is refused', () => {
|
||||
const target = scratch();
|
||||
for (const manifest of [
|
||||
{ through: B, migrations: [A, 3] },
|
||||
{ through: B, migrations: [] },
|
||||
{ through: 7, migrations: [B] },
|
||||
]) {
|
||||
writePostgresBaseline(sample, target);
|
||||
writeFileSync(path.join(target, 'baseline.json'), JSON.stringify(manifest));
|
||||
expect(() => readPostgresBaseline(target), JSON.stringify(manifest)).toThrow(/must name "through"/);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('triggerPortProblems', () => {
|
||||
it('PGBASE-020: every SQLite trigger needs a Postgres port, and a port without its trigger is stale', () => {
|
||||
expect(triggerPortProblems(['a', 'b'], ['b', 'a'])).toEqual([]);
|
||||
|
||||
Reference in New Issue
Block a user