refactor(db): split the Postgres schema adjustments by concern

schema-adjust.ts mixed the statement splitter, the trigger ports and the
column adjustments, and every caller repeated `meta.props as EntityProperty[]
as ColumnMeta[]` to reach them. Now:

- sql-split.ts holds splitStatements. Its scope is written down: `;` outside
  quotes, '/" quoted text and the $$ body, and the session-setting lines it
  drops. A tagged dollar quote ($tag$) is refused with an error instead of
  being split inside, which the splitter could not do correctly.
- postgres-triggers.ts holds the hand-written trigger ports, which the
  baseline and the parity test read by name.
- schema-adjust.ts holds the column adjustments. applyPostgresColumnAdjustments
  (renamed from adjustColumns, which hid that it mutates its input) takes an
  entity's props directly: ColumnMeta now reads customType as unknown, so
  the entity's property metadata satisfies it without a cast. Each adjustment
  is a small function that returns whether it changed the column.

The timestamp check is now `instanceof DbTimestampType` rather than a compare
on the constructor's name, which would have broken silently on a rename.
This commit is contained in:
Isaias Tavares
2026-11-05 00:32:18 -03:00
parent d498fe2bea
commit 33a18f29dd
8 changed files with 193 additions and 133 deletions
@@ -4,19 +4,19 @@
* NOCASE collation, and splitting the generated DDL so one refused statement
* does not take the rest with it.
*/
import { POSTGRES_TRIGGERS } from '../../../../../src/db/engine/postgres/postgres-triggers';
import {
adjustColumns,
applyPostgresColumnAdjustments,
type ColumnMeta,
NOCASE_COLLATION_SQL,
PG_NOW_TEXT_DEFAULT,
portableDefault,
POSTGRES_TRIGGERS,
splitStatements,
type ColumnMeta,
} from '../../../../../src/db/engine/postgres/schema-adjust';
import { splitStatements } from '../../../../../src/db/engine/postgres/sql-split';
import { DbTimestampType } from '../../../../../src/db/types';
import { describe, expect, it } from 'vitest';
class DbTimestampType {}
class TextType {}
describe('pg-probe schema', () => {
@@ -42,7 +42,7 @@ describe('pg-probe schema', () => {
{ name: 'region', columnTypes: ['text'], collation: 'NOCASE' },
{ name: 'body', columnTypes: ['text'] },
];
expect(adjustColumns(columns)).toEqual(['created_at', 'stamp', 'region']);
expect(applyPostgresColumnAdjustments(columns)).toEqual(['created_at', 'stamp', 'region']);
expect(columns[0]).toMatchObject({ columnTypes: ['text'], defaultRaw: PG_NOW_TEXT_DEFAULT });
expect(columns[1]).toMatchObject({ columnTypes: ['text'], defaultRaw: "'x'" });
expect(columns[2]).toMatchObject({
@@ -60,7 +60,7 @@ describe('pg-probe schema', () => {
{ name: 'enabled', columnTypes: ['boolean'] },
{ name: 'note', columnTypes: ['text'] },
];
expect(adjustColumns(columns)).toEqual(['id', 'next_run_at']);
expect(applyPostgresColumnAdjustments(columns)).toEqual(['id', 'next_run_at']);
expect(columns.map((column) => column.columnTypes)).toEqual([['bigint'], ['bigint'], ['boolean'], ['text']]);
});
@@ -80,6 +80,13 @@ describe('pg-probe schema', () => {
expect(splitStatements(' ; ; ')).toEqual([]);
});
it('PGPROBE-045: refuses a tagged dollar quote, which it does not understand, rather than splitting inside it', () => {
expect(() => splitStatements('create function f() as $f$ select 1; $f$;')).toThrow(
/only the \$\$ dollar quote, not \$f\$/,
);
expect(splitStatements("select '$f$'; select 2")).toEqual(["select '$f$'", 'select 2']);
});
it('PGPROBE-044: a $$ body stays one statement, semicolons and all, and every trigger port splits into its two', () => {
const fn =
"CREATE FUNCTION f() RETURNS trigger LANGUAGE plpgsql AS $$\nBEGIN\n UPDATE t SET x = ';';\n RETURN NEW;\nEND\n$$";