@pgsql/transform 18.7.0 → 18.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/graph.js ADDED
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildStatementGraph = buildStatementGraph;
4
+ const keyOf = (q) => `${q.schema ?? ''}.${q.name}`;
5
+ /**
6
+ * Build the typed statement dependency graph for a classified script.
7
+ *
8
+ * Edges only exist between statements of the same script: a reference with
9
+ * no in-script producer is an external dependency and induces no edge (the
10
+ * caller decides what to do with those — pgpm expresses them as
11
+ * cross-package requires).
12
+ */
13
+ function buildStatementGraph(facts) {
14
+ const nodes = facts.map((f, index) => ({
15
+ index,
16
+ facts: f,
17
+ out: [],
18
+ in: []
19
+ }));
20
+ const producers = new Map();
21
+ facts.forEach((f, i) => {
22
+ for (const created of f.creates) {
23
+ const key = keyOf(created);
24
+ const list = producers.get(key) ?? [];
25
+ list.push(i);
26
+ producers.set(key, list);
27
+ }
28
+ });
29
+ // The producer a reference binds to is the closest preceding statement
30
+ // that creates the object (redefinitions shadow earlier ones); when the
31
+ // reference precedes every producer, it binds to the first one — that is
32
+ // exactly the forward edge a reordering pass must satisfy.
33
+ const bind = (ref, from) => {
34
+ const list = producers.get(keyOf(ref));
35
+ if (!list || list.length === 0)
36
+ return undefined;
37
+ let found;
38
+ for (const i of list) {
39
+ if (i === from)
40
+ return undefined; // self-dependency: never an edge
41
+ if (i < from)
42
+ found = i;
43
+ }
44
+ return found ?? list[0];
45
+ };
46
+ const edges = [];
47
+ const addEdge = (from, to, kind, via) => {
48
+ if (edges.some(e => e.from === from && e.to === to && e.kind === kind))
49
+ return;
50
+ const edge = { from, to, kind, via };
51
+ edges.push(edge);
52
+ nodes[from].out.push(edge);
53
+ nodes[to].in.push(edge);
54
+ };
55
+ facts.forEach((f, i) => {
56
+ const bodyOnly = new Set(f.bodyReferences.map(keyOf));
57
+ const fkKeys = new Set(f.fkTargets.map(keyOf));
58
+ for (const ref of f.references) {
59
+ const to = bind(ref, i);
60
+ if (to === undefined)
61
+ continue;
62
+ const key = keyOf(ref);
63
+ const kind = fkKeys.has(key) ? 'fk' : bodyOnly.has(key) ? 'late' : 'hard';
64
+ addEdge(i, to, kind, ref);
65
+ }
66
+ });
67
+ const components = condense(nodes);
68
+ const order = stableTopoOrder(nodes, components);
69
+ return { nodes, edges, producers, components, order };
70
+ }
71
+ /**
72
+ * Tarjan SCC over `hard` + `fk` edges (`late` edges are ignored — they are
73
+ * call-time bindings and legitimately cyclic). Components are returned in
74
+ * reverse-topological completion order and then re-sorted topologically with
75
+ * source-order tie-breaking by {@link stableTopoOrder}; here we only sort
76
+ * each component's members and order components by their smallest member so
77
+ * output is deterministic.
78
+ */
79
+ function condense(nodes) {
80
+ const n = nodes.length;
81
+ const indexOf = new Array(n).fill(-1);
82
+ const low = new Array(n).fill(0);
83
+ const onStack = new Array(n).fill(false);
84
+ const stack = [];
85
+ const components = [];
86
+ let counter = 0;
87
+ const orderingEdges = (v) => nodes[v].out.filter(e => e.kind !== 'late').map(e => e.to);
88
+ // Iterative Tarjan (DDL scripts can be tens of thousands of statements).
89
+ const visit = (root) => {
90
+ const frames = [{ v: root, edges: orderingEdges(root), i: 0 }];
91
+ indexOf[root] = low[root] = counter++;
92
+ stack.push(root);
93
+ onStack[root] = true;
94
+ while (frames.length > 0) {
95
+ const frame = frames[frames.length - 1];
96
+ if (frame.i < frame.edges.length) {
97
+ const w = frame.edges[frame.i++];
98
+ if (indexOf[w] === -1) {
99
+ indexOf[w] = low[w] = counter++;
100
+ stack.push(w);
101
+ onStack[w] = true;
102
+ frames.push({ v: w, edges: orderingEdges(w), i: 0 });
103
+ }
104
+ else if (onStack[w]) {
105
+ low[frame.v] = Math.min(low[frame.v], indexOf[w]);
106
+ }
107
+ }
108
+ else {
109
+ frames.pop();
110
+ if (frames.length > 0) {
111
+ const parent = frames[frames.length - 1];
112
+ low[parent.v] = Math.min(low[parent.v], low[frame.v]);
113
+ }
114
+ if (low[frame.v] === indexOf[frame.v]) {
115
+ const component = [];
116
+ for (;;) {
117
+ const w = stack.pop();
118
+ onStack[w] = false;
119
+ component.push(w);
120
+ if (w === frame.v)
121
+ break;
122
+ }
123
+ component.sort((a, b) => a - b);
124
+ components.push(component);
125
+ }
126
+ }
127
+ }
128
+ };
129
+ for (let v = 0; v < n; v++) {
130
+ if (indexOf[v] === -1)
131
+ visit(v);
132
+ }
133
+ components.sort((a, b) => a[0] - b[0]);
134
+ return components;
135
+ }
136
+ /**
137
+ * Topologically order the condensation with source order breaking ties
138
+ * (Kahn's algorithm over components, min-heap on smallest member index),
139
+ * then flatten: members of each component stay in source order.
140
+ */
141
+ function stableTopoOrder(nodes, components) {
142
+ const componentOf = new Array(nodes.length).fill(0);
143
+ components.forEach((members, c) => {
144
+ for (const m of members)
145
+ componentOf[m] = c;
146
+ });
147
+ const succ = components.map(() => new Set());
148
+ const indegree = new Array(components.length).fill(0);
149
+ for (const node of nodes) {
150
+ for (const e of node.out) {
151
+ if (e.kind === 'late')
152
+ continue;
153
+ const from = componentOf[e.from];
154
+ const to = componentOf[e.to];
155
+ if (from === to)
156
+ continue;
157
+ // Dependency edge from → to means `to` must come first.
158
+ if (!succ[to].has(from)) {
159
+ succ[to].add(from);
160
+ indegree[from]++;
161
+ }
162
+ }
163
+ }
164
+ const ready = [];
165
+ for (let c = 0; c < components.length; c++) {
166
+ if (indegree[c] === 0)
167
+ ready.push(c);
168
+ }
169
+ const takeMin = () => {
170
+ let best = 0;
171
+ for (let i = 1; i < ready.length; i++) {
172
+ if (components[ready[i]][0] < components[ready[best]][0])
173
+ best = i;
174
+ }
175
+ return ready.splice(best, 1)[0];
176
+ };
177
+ const order = [];
178
+ while (ready.length > 0) {
179
+ const c = takeMin();
180
+ order.push(...components[c]);
181
+ for (const next of succ[c]) {
182
+ if (--indegree[next] === 0)
183
+ ready.push(next);
184
+ }
185
+ }
186
+ return order;
187
+ }
package/index.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  export type { ExtensionAction, ExtensionFact, QualifiedName, StatementFacts, StatementKind, } from './facts';
2
2
  export { classifyStatements } from './facts';
3
+ export type { EdgeKind, StatementEdge, StatementGraph, StatementNode, } from './graph';
4
+ export { buildStatementGraph } from './graph';
5
+ export type { Granularity, RestructureOptions, RestructureResult, } from './restructure';
6
+ export { orderStatements, restructureSql } from './restructure';
3
7
  export type { ExtensionDefinition, ExtensionRoute, ExtensionRouteSpec, ExtensionRouterOptions, ExtensionSymbol, ExtensionSymbolNamespace, SymbolRewrite, } from './extension-router';
4
8
  export { COMMON_EXTENSIONS, ExtensionRouter } from './extension-router';
5
9
  export type { ExtensionTransformResult } from './extension-transform';
package/index.js CHANGED
@@ -1,8 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.walkPlpgsqlForSchemas = exports.validateNoUntransformedSchemas = exports.transformVerifyCalls = exports.transformSqlStatement = exports.transformSql = exports.transformSchemaName = exports.transformRelation = exports.transformPlpgsqlTypeString = exports.transformPlpgsqlTypeAst = exports.transformNameList = exports.transformJsonStringValues = exports.transformComments = exports.shouldTransformSchema = exports.extractPgpmHeader = exports.escapeRegexp = exports.createSqlVisitor = exports.createPlpgsqlVisitor = exports.trimDefElemBody = exports.normalizeTree = exports.cleanTree = exports.CLEAN_TREE_VOLATILE_KEYS = exports.validateRoundTrip = exports.normalizeParseTree = exports.firstDifference = exports.captureTransformAsts = exports.captureAstsFromSql = exports.SchemaRouter = exports.qualifyUnqualified = exports.mergeInventories = exports.createQualifyVisitor = exports.collectCreatedObjects = exports.transformRoles = exports.createRoleVisitor = exports.createRoleResult = exports.RoleRouter = exports.transformExtensions = exports.createExtensionVisitor = exports.createExtensionResult = exports.ExtensionRouter = exports.COMMON_EXTENSIONS = exports.classifyStatements = void 0;
3
+ exports.walkPlpgsqlForSchemas = exports.validateNoUntransformedSchemas = exports.transformVerifyCalls = exports.transformSqlStatement = exports.transformSql = exports.transformSchemaName = exports.transformRelation = exports.transformPlpgsqlTypeString = exports.transformPlpgsqlTypeAst = exports.transformNameList = exports.transformJsonStringValues = exports.transformComments = exports.shouldTransformSchema = exports.extractPgpmHeader = exports.escapeRegexp = exports.createSqlVisitor = exports.createPlpgsqlVisitor = exports.trimDefElemBody = exports.normalizeTree = exports.cleanTree = exports.CLEAN_TREE_VOLATILE_KEYS = exports.validateRoundTrip = exports.normalizeParseTree = exports.firstDifference = exports.captureTransformAsts = exports.captureAstsFromSql = exports.SchemaRouter = exports.qualifyUnqualified = exports.mergeInventories = exports.createQualifyVisitor = exports.collectCreatedObjects = exports.transformRoles = exports.createRoleVisitor = exports.createRoleResult = exports.RoleRouter = exports.transformExtensions = exports.createExtensionVisitor = exports.createExtensionResult = exports.ExtensionRouter = exports.COMMON_EXTENSIONS = exports.restructureSql = exports.orderStatements = exports.buildStatementGraph = exports.classifyStatements = void 0;
4
4
  var facts_1 = require("./facts");
5
5
  Object.defineProperty(exports, "classifyStatements", { enumerable: true, get: function () { return facts_1.classifyStatements; } });
6
+ var graph_1 = require("./graph");
7
+ Object.defineProperty(exports, "buildStatementGraph", { enumerable: true, get: function () { return graph_1.buildStatementGraph; } });
8
+ var restructure_1 = require("./restructure");
9
+ Object.defineProperty(exports, "orderStatements", { enumerable: true, get: function () { return restructure_1.orderStatements; } });
10
+ Object.defineProperty(exports, "restructureSql", { enumerable: true, get: function () { return restructure_1.restructureSql; } });
6
11
  var extension_router_1 = require("./extension-router");
7
12
  Object.defineProperty(exports, "COMMON_EXTENSIONS", { enumerable: true, get: function () { return extension_router_1.COMMON_EXTENSIONS; } });
8
13
  Object.defineProperty(exports, "ExtensionRouter", { enumerable: true, get: function () { return extension_router_1.ExtensionRouter; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgsql/transform",
3
- "version": "18.7.0",
3
+ "version": "18.9.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "AST-based SQL transformation, qualification, classification and closure analysis for PostgreSQL",
6
6
  "main": "index.js",
@@ -33,8 +33,8 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@pgsql/quotes": "18.1.0",
36
- "@pgsql/traverse": "18.3.0",
37
- "plpgsql-parser": "18.2.1"
36
+ "@pgsql/traverse": "18.4.0",
37
+ "plpgsql-parser": "18.2.2"
38
38
  },
39
39
  "devDependencies": {
40
40
  "makage": "^0.1.8"
@@ -49,5 +49,5 @@
49
49
  "classify",
50
50
  "plpgsql"
51
51
  ],
52
- "gitHead": "057e427849438bfea946d6d85557603ba7fea134"
52
+ "gitHead": "ffe07838bbc332b12062679f4f0af6b952380cb1"
53
53
  }
@@ -0,0 +1,29 @@
1
+ /** The target shape of a restructured script. */
2
+ export type Granularity = 'atomic' | 'object' | 'consolidated';
3
+ export interface RestructureOptions {
4
+ granularity: Granularity;
5
+ }
6
+ export interface RestructureResult {
7
+ /** The restructured script, statements in stable topological order. */
8
+ sql: string;
9
+ /** Number of statements folded into a `CREATE TABLE` (fold direction). */
10
+ folded: number;
11
+ /** Number of statements produced by explosion (atomize direction). */
12
+ exploded: number;
13
+ /** Folds that were rejected (with the reason) and other non-fatal notes. */
14
+ warnings: string[];
15
+ }
16
+ type AnyNode = Record<string, any>;
17
+ /**
18
+ * Restructure a DDL script to the requested granularity. The input and
19
+ * output scripts deploy to identical schemas; only statement shape and
20
+ * order change.
21
+ */
22
+ export declare function restructureSql(sql: string, options: RestructureOptions): RestructureResult;
23
+ /**
24
+ * Re-emit statements in the stable topological order of their dependency
25
+ * graph. Statements are deparsed and re-classified so the ordering reflects
26
+ * exactly what will be emitted.
27
+ */
28
+ export declare function orderStatements(stmts: AnyNode[]): AnyNode[];
29
+ export {};
package/restructure.js ADDED
@@ -0,0 +1,331 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.restructureSql = restructureSql;
4
+ exports.orderStatements = orderStatements;
5
+ /**
6
+ * Granularity restructuring: rewrite a DDL script between equivalent shapes
7
+ * without changing the schema it produces.
8
+ *
9
+ * Three levels, two directions:
10
+ *
11
+ * - `atomic` — every table is a bare `CREATE TABLE ()` followed by one
12
+ * `ALTER TABLE ADD COLUMN` per column and one `ADD CONSTRAINT` per
13
+ * constraint (the shape machine emitters produce).
14
+ * - `object` — each table is fully baked: columns and same-table constraints
15
+ * (PK / UNIQUE / CHECK / NOT NULL / DEFAULT) fold into the `CREATE TABLE`;
16
+ * cross-object statements (FKs, indexes, triggers, policies) stay separate.
17
+ * - `consolidated` — additionally inlines foreign keys into the table
18
+ * definition whenever the dependency graph proves it safe (the referenced
19
+ * table can be ordered first); FKs on cycles stay as `ALTER TABLE`.
20
+ *
21
+ * Every fold is validated against the {@link buildStatementGraph} statement
22
+ * graph: a candidate merge is rejected if it would create an ordering cycle,
23
+ * so the pass degrades gracefully to the atomic form instead of emitting an
24
+ * undeployable script. Output statements are re-emitted in the graph's
25
+ * stable topological order.
26
+ */
27
+ const plpgsql_parser_1 = require("plpgsql-parser");
28
+ const facts_1 = require("./facts");
29
+ const graph_1 = require("./graph");
30
+ const relKey = (rel) => rel ? `${rel.schemaname ?? ''}.${rel.relname}` : null;
31
+ const sameRel = (a, b) => !!a && !!b && relKey(a) === relKey(b);
32
+ /**
33
+ * Restructure a DDL script to the requested granularity. The input and
34
+ * output scripts deploy to identical schemas; only statement shape and
35
+ * order change.
36
+ */
37
+ function restructureSql(sql, options) {
38
+ const warnings = [];
39
+ const parsed = (0, plpgsql_parser_1.parseSql)(sql);
40
+ const stmts = (parsed?.stmts ?? [])
41
+ .map((s) => s?.stmt)
42
+ .filter(Boolean);
43
+ let outStmts;
44
+ let folded = 0;
45
+ let exploded = 0;
46
+ if (options.granularity === 'atomic') {
47
+ const result = explodeStatements(stmts);
48
+ outStmts = result.stmts;
49
+ exploded = result.exploded;
50
+ }
51
+ else {
52
+ const facts = (0, facts_1.classifyStatements)(sql);
53
+ const graph = (0, graph_1.buildStatementGraph)(facts);
54
+ const result = foldStatements(stmts, graph, options.granularity === 'consolidated', warnings);
55
+ outStmts = result.stmts;
56
+ folded = result.folded;
57
+ }
58
+ const ordered = orderStatements(outStmts);
59
+ const text = ordered.map(s => `${plpgsql_parser_1.Deparser.deparse(s)};`).join('\n\n');
60
+ return { sql: text, folded, exploded, warnings };
61
+ }
62
+ /**
63
+ * Re-emit statements in the stable topological order of their dependency
64
+ * graph. Statements are deparsed and re-classified so the ordering reflects
65
+ * exactly what will be emitted.
66
+ */
67
+ function orderStatements(stmts) {
68
+ if (stmts.length <= 1)
69
+ return stmts;
70
+ const script = stmts.map(s => `${plpgsql_parser_1.Deparser.deparse(s)};`).join('\n');
71
+ const graph = (0, graph_1.buildStatementGraph)((0, facts_1.classifyStatements)(script));
72
+ if (graph.order.length !== stmts.length)
73
+ return stmts;
74
+ return graph.order.map(i => stmts[i]);
75
+ }
76
+ /**
77
+ * Fold `ALTER TABLE` commands into their table's `CREATE TABLE` statement.
78
+ * `inlineFks` additionally folds `ADD FOREIGN KEY` when the graph proves the
79
+ * referenced table can be created first.
80
+ */
81
+ function foldStatements(stmts, graph, inlineFks, warnings) {
82
+ // Locate the CREATE TABLE for each relation.
83
+ const createIndex = new Map();
84
+ stmts.forEach((s, i) => {
85
+ const create = s?.CreateStmt;
86
+ if (create?.relation) {
87
+ const key = relKey(create.relation);
88
+ if (key && !createIndex.has(key))
89
+ createIndex.set(key, i);
90
+ }
91
+ });
92
+ // Fold bookkeeping is a union-find over statements: folding j into the
93
+ // CREATE at i merges their graph nodes, and the merged node's dependency
94
+ // set is the union of both. Reachability is then computed over merged
95
+ // nodes so a second fold cannot silently complete a cycle the first one
96
+ // started (the mutual-FK case).
97
+ const rep = new Map();
98
+ const find = (v) => {
99
+ let r = v;
100
+ while (rep.has(r))
101
+ r = rep.get(r);
102
+ return r;
103
+ };
104
+ // Per-statement dependencies: typed graph edges plus the implicit edge
105
+ // from every ALTER to its own table's CREATE (the facts model records the
106
+ // alter as *targeting* the table, so the graph carries no self edge).
107
+ const deps = new Map();
108
+ stmts.forEach((s, j) => {
109
+ const set = new Set();
110
+ for (const e of graph.nodes[j]?.out ?? []) {
111
+ if (e.kind !== 'late')
112
+ set.add(e.to);
113
+ }
114
+ const alterKey = relKey(s?.AlterTableStmt?.relation);
115
+ const own = alterKey ? createIndex.get(alterKey) : undefined;
116
+ if (own !== undefined && own !== j)
117
+ set.add(own);
118
+ deps.set(j, set);
119
+ });
120
+ const depsOf = (v) => [...(deps.get(find(v)) ?? [])].map(find).filter(t => t !== find(v));
121
+ const dependsOn = (from, target) => {
122
+ const goal = find(target);
123
+ const seen = new Set();
124
+ const stack = [find(from)];
125
+ while (stack.length > 0) {
126
+ const v = stack.pop();
127
+ if (v === goal)
128
+ return true;
129
+ if (seen.has(v))
130
+ continue;
131
+ seen.add(v);
132
+ stack.push(...depsOf(v));
133
+ }
134
+ return false;
135
+ };
136
+ /**
137
+ * A command at statement `j` may fold into the CREATE at statement `i`
138
+ * iff none of j's dependencies (other than i itself) transitively depend
139
+ * on i — otherwise the merged node would sit on a cycle.
140
+ */
141
+ const safeToFold = (j, i) => !depsOf(j).some(t => t !== find(i) && dependsOn(t, i));
142
+ /** Merge statement `j` into the CREATE at `i`. */
143
+ const absorb = (i, j) => {
144
+ const target = find(i);
145
+ const merged = deps.get(target) ?? new Set();
146
+ for (const t of deps.get(find(j)) ?? [])
147
+ merged.add(t);
148
+ deps.set(target, merged);
149
+ if (find(j) !== target)
150
+ rep.set(find(j), target);
151
+ };
152
+ const remove = new Set();
153
+ let folded = 0;
154
+ stmts.forEach((s, j) => {
155
+ const alter = s?.AlterTableStmt;
156
+ if (!alter || alter.objtype !== 'OBJECT_TABLE')
157
+ return;
158
+ const key = relKey(alter.relation);
159
+ const i = key ? createIndex.get(key) : undefined;
160
+ if (i === undefined || i === j)
161
+ return;
162
+ const create = stmts[i].CreateStmt;
163
+ const remaining = [];
164
+ for (const wrapped of alter.cmds ?? []) {
165
+ const cmd = wrapped?.AlterTableCmd;
166
+ if (!cmd) {
167
+ remaining.push(wrapped);
168
+ continue;
169
+ }
170
+ const constraint = cmd.def?.Constraint;
171
+ const isFk = cmd.subtype === 'AT_AddConstraint' && constraint?.contype === 'CONSTR_FOREIGN';
172
+ const selfFk = isFk && sameRel(constraint?.pktable, alter.relation);
173
+ let foldable = false;
174
+ switch (cmd.subtype) {
175
+ case 'AT_AddColumn':
176
+ foldable = !!cmd.def?.ColumnDef;
177
+ break;
178
+ case 'AT_ColumnDefault':
179
+ foldable = !!cmd.name && cmd.def !== undefined;
180
+ break;
181
+ case 'AT_SetNotNull':
182
+ foldable = !!cmd.name;
183
+ break;
184
+ case 'AT_AddConstraint':
185
+ foldable = isFk ? selfFk || inlineFks : true;
186
+ break;
187
+ default:
188
+ foldable = false;
189
+ }
190
+ if (foldable && !selfFk && !safeToFold(j, i)) {
191
+ warnings.push(`kept atomic: statement ${j} on ${key} would create an ordering cycle if folded`);
192
+ foldable = false;
193
+ }
194
+ if (!foldable) {
195
+ remaining.push(wrapped);
196
+ continue;
197
+ }
198
+ if (!applyFold(create, cmd)) {
199
+ remaining.push(wrapped);
200
+ continue;
201
+ }
202
+ absorb(i, j);
203
+ folded++;
204
+ }
205
+ if (remaining.length === 0) {
206
+ remove.add(j);
207
+ }
208
+ else {
209
+ alter.cmds = remaining;
210
+ }
211
+ });
212
+ return { stmts: stmts.filter((_, i) => !remove.has(i)), folded };
213
+ }
214
+ /** Apply one foldable ALTER TABLE command onto a CreateStmt. */
215
+ function applyFold(create, cmd) {
216
+ create.tableElts = create.tableElts ?? [];
217
+ const findColumn = (name) => create.tableElts.find((e) => e?.ColumnDef?.colname === name)?.ColumnDef;
218
+ switch (cmd.subtype) {
219
+ case 'AT_AddColumn':
220
+ create.tableElts.push({ ColumnDef: cmd.def.ColumnDef });
221
+ return true;
222
+ case 'AT_ColumnDefault': {
223
+ const col = findColumn(cmd.name);
224
+ if (!col)
225
+ return false;
226
+ col.constraints = col.constraints ?? [];
227
+ col.constraints.push({ Constraint: { contype: 'CONSTR_DEFAULT', raw_expr: cmd.def } });
228
+ return true;
229
+ }
230
+ case 'AT_SetNotNull': {
231
+ const col = findColumn(cmd.name);
232
+ if (!col)
233
+ return false;
234
+ col.constraints = col.constraints ?? [];
235
+ col.constraints.push({ Constraint: { contype: 'CONSTR_NOTNULL' } });
236
+ return true;
237
+ }
238
+ case 'AT_AddConstraint':
239
+ create.tableElts.push({ Constraint: cmd.def.Constraint });
240
+ return true;
241
+ default:
242
+ return false;
243
+ }
244
+ }
245
+ /** Column-level constraint types that stay inline on `ADD COLUMN`. */
246
+ const INLINE_COLUMN_CONSTRAINTS = new Set(['CONSTR_DEFAULT', 'CONSTR_NOTNULL', 'CONSTR_NULL', 'CONSTR_IDENTITY', 'CONSTR_GENERATED']);
247
+ /**
248
+ * Explode consolidated `CREATE TABLE` statements into the atomic shape:
249
+ * bare create, one `ADD COLUMN` per column (keeping column-local defaults /
250
+ * NOT NULL inline), one `ADD CONSTRAINT` per table-level or key constraint.
251
+ */
252
+ function explodeStatements(stmts) {
253
+ const out = [];
254
+ let exploded = 0;
255
+ const alterFor = (relation, cmd) => ({
256
+ AlterTableStmt: {
257
+ objtype: 'OBJECT_TABLE',
258
+ relation,
259
+ cmds: [{ AlterTableCmd: cmd }]
260
+ }
261
+ });
262
+ for (const s of stmts) {
263
+ const create = s?.CreateStmt;
264
+ const elts = create?.tableElts ?? [];
265
+ // Typed tables / partitions / inheritance keep their shape.
266
+ if (!create || elts.length === 0 || create.ofTypename || create.partbound || create.inhRelations) {
267
+ out.push(s);
268
+ continue;
269
+ }
270
+ const relation = create.relation;
271
+ const columns = [];
272
+ const constraints = [];
273
+ for (const elt of elts) {
274
+ if (elt?.ColumnDef)
275
+ columns.push(elt.ColumnDef);
276
+ else if (elt?.Constraint)
277
+ constraints.push(elt.Constraint);
278
+ else {
279
+ // Unknown element (e.g. LIKE clause): keep the table intact.
280
+ columns.length = 0;
281
+ constraints.length = 0;
282
+ break;
283
+ }
284
+ }
285
+ if (columns.length === 0 && constraints.length === 0) {
286
+ out.push(s);
287
+ continue;
288
+ }
289
+ out.push({ CreateStmt: { ...create, tableElts: [] } });
290
+ for (const col of columns) {
291
+ const inline = [];
292
+ for (const wrapped of col.constraints ?? []) {
293
+ const c = wrapped?.Constraint;
294
+ if (c && INLINE_COLUMN_CONSTRAINTS.has(c.contype)) {
295
+ inline.push(wrapped);
296
+ }
297
+ else if (c) {
298
+ // Promote a column constraint (PK/UNIQUE/CHECK/FK) to table level.
299
+ constraints.push(columnConstraintToTable(c, col.colname));
300
+ }
301
+ }
302
+ out.push(alterFor(relation, {
303
+ subtype: 'AT_AddColumn',
304
+ def: { ColumnDef: { ...col, constraints: inline.length > 0 ? inline : undefined } }
305
+ }));
306
+ exploded++;
307
+ }
308
+ for (const constraint of constraints) {
309
+ out.push(alterFor(relation, {
310
+ subtype: 'AT_AddConstraint',
311
+ def: { Constraint: constraint }
312
+ }));
313
+ exploded++;
314
+ }
315
+ }
316
+ return { stmts: out, exploded };
317
+ }
318
+ /** Rewrite a column-level constraint as its table-level equivalent. */
319
+ function columnConstraintToTable(constraint, colname) {
320
+ const strNode = (sval) => ({ String: { sval } });
321
+ switch (constraint.contype) {
322
+ case 'CONSTR_PRIMARY':
323
+ case 'CONSTR_UNIQUE':
324
+ return { ...constraint, keys: [strNode(colname)] };
325
+ case 'CONSTR_FOREIGN':
326
+ return { ...constraint, fk_attrs: [strNode(colname)] };
327
+ default:
328
+ // CHECK and others are valid table constraints as-is.
329
+ return constraint;
330
+ }
331
+ }
package/transform.d.ts CHANGED
@@ -110,7 +110,10 @@ export declare function transformNameList(names: any[] | undefined, schemaMappin
110
110
  export declare function transformSchemaNameField(container: any, field: string, schemaMapping: SchemaMappingInput, result: SchemaTransformResult): void;
111
111
  /**
112
112
  * Transform a RangeVar-like relation object (has schemaname/relname fields).
113
- * Many statement nodes embed a relation that the walker does NOT auto-recurse into.
113
+ * The walker auto-recurses into embedded relations (concrete `RangeVar`
114
+ * fields are tag-synthesized), so the generic `RangeVar` visitor covers
115
+ * every occurrence; statement handlers call this directly only when they
116
+ * carry extra context, and the claim guard keeps each relation routed once.
114
117
  */
115
118
  export declare function transformRelation(relation: any, schemaMapping: SchemaMappingInput, result: SchemaTransformResult): void;
116
119
  /**
@@ -134,34 +137,13 @@ export declare function createSqlVisitor(schemaMapping: SchemaMappingInput, resu
134
137
  RangeVar: (path: any) => void;
135
138
  CreateSchemaStmt: (path: any) => void;
136
139
  FuncCall: (path: any) => void;
137
- CallStmt: (path: any) => void;
138
140
  TypeName: (path: any) => void;
139
141
  ColumnRef: (path: any) => void;
140
142
  GrantStmt: (path: any) => void;
141
143
  VariableSetStmt: (path: any) => void;
142
144
  AlterDefaultPrivilegesStmt: (path: any) => void;
143
145
  DropStmt: (path: any) => void;
144
- InsertStmt: (path: any) => void;
145
- CreateStmt: (path: any) => void;
146
- UpdateStmt: (path: any) => void;
147
- DeleteStmt: (path: any) => void;
148
- MergeStmt: (path: any) => void;
149
- IndexStmt: (path: any) => void;
150
- Constraint: (path: any) => void;
151
- CompositeTypeStmt: (path: any) => void;
152
- ColumnDef: (path: any) => void;
153
- ViewStmt: (path: any) => void;
154
- AlterTableStmt: (path: any) => void;
155
- CreateSeqStmt: (path: any) => void;
156
146
  AlterSeqStmt: (path: any) => void;
157
- CreatePolicyStmt: (path: any) => void;
158
- RuleStmt: (path: any) => void;
159
- CopyStmt: (path: any) => void;
160
- ClusterStmt: (path: any) => void;
161
- VacuumRelation: (path: any) => void;
162
- RefreshMatViewStmt: (path: any) => void;
163
- CreateTableAsStmt: (path: any) => void;
164
- TypeCast: (path: any) => void;
165
147
  CreateFunctionStmt: (path: any) => void;
166
148
  CreateTrigStmt: (path: any) => void;
167
149
  CommentStmt: (path: any) => void;