mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
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:
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Splits generated DDL into single statements, so one statement Postgres
|
||||
* refuses does not take the rest of the schema with it.
|
||||
*
|
||||
* Understood: `;` outside quotes, `'` and `"` quoted text, and the `$$` dollar
|
||||
* quote a function body is written in, which is the only dollar quote TREK's
|
||||
* generated DDL and baseline contain. A tagged dollar quote (`$tag$`) is not
|
||||
* understood, so it is refused rather than split inside.
|
||||
*/
|
||||
|
||||
/** A tagged dollar quote (`$tag$`), matched where the scan stands. */
|
||||
const TAGGED_DOLLAR_QUOTE = /\$[A-Za-z_]\w*\$/y;
|
||||
|
||||
function refuseTaggedDollarQuote(sql: string, at: number): void {
|
||||
TAGGED_DOLLAR_QUOTE.lastIndex = at;
|
||||
const tag = TAGGED_DOLLAR_QUOTE.exec(sql)?.[0];
|
||||
if (tag) throw new Error(`splitStatements understands only the $$ dollar quote, not ${tag} (at ${at})`);
|
||||
}
|
||||
|
||||
/**
|
||||
* The statements in `sql`, trimmed and in order. A statement that sets a
|
||||
* session parameter (`SET ...`) is dropped: a dump's preamble, which a schema
|
||||
* the entities generate never needs.
|
||||
*/
|
||||
export function splitStatements(sql: string): string[] {
|
||||
const statements: string[] = [];
|
||||
let current = '';
|
||||
let quote: '"' | "'" | '$$' | null = null;
|
||||
for (let i = 0; i < sql.length; i++) {
|
||||
const char = sql[i];
|
||||
const dollar = sql.startsWith('$$', i);
|
||||
if (quote === '$$') {
|
||||
if (dollar) {
|
||||
quote = null;
|
||||
current += '$';
|
||||
i++;
|
||||
}
|
||||
current += char;
|
||||
} else if (quote) {
|
||||
if (char === quote) quote = null;
|
||||
current += char;
|
||||
} else if (dollar) {
|
||||
quote = '$$';
|
||||
current += '$$';
|
||||
i++;
|
||||
} else if (char === "'" || char === '"') {
|
||||
quote = char;
|
||||
current += char;
|
||||
} else if (char === ';') {
|
||||
if (current.trim()) statements.push(current.trim());
|
||||
current = '';
|
||||
} else {
|
||||
if (char === '$') refuseTaggedDollarQuote(sql, i);
|
||||
current += char;
|
||||
}
|
||||
}
|
||||
if (current.trim()) statements.push(current.trim());
|
||||
return statements.filter((statement) => !/^set\s/i.test(statement));
|
||||
}
|
||||
Reference in New Issue
Block a user