@pgsql/transform 18.6.0 → 18.8.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/transform.js CHANGED
@@ -68,6 +68,36 @@ function createResult() {
68
68
  errors: [],
69
69
  };
70
70
  }
71
+ /**
72
+ * Routing claims: each routable site (a RangeVar, a qualified name list, a
73
+ * bare schema-name field) is resolved through the router at most once per
74
+ * transform result.
75
+ *
76
+ * The walker visits parents before children, so a statement-level handler
77
+ * with namespace context (e.g. `AlterFunctionStmt` routing its
78
+ * `ObjectWithArgs` as a `function`) claims the site before a generic child
79
+ * visitor (`ObjectWithArgs`, namespace `unknown`) reaches it — first (most
80
+ * contextual) visitor wins. Without claims a site is routed twice, which
81
+ * corrupts cyclic mappings (a→b, b→a swaps back) and applies the less precise
82
+ * namespace second.
83
+ */
84
+ const routingClaims = new WeakMap();
85
+ function claimSite(result, site, field = '*') {
86
+ let byNode = routingClaims.get(result);
87
+ if (!byNode) {
88
+ byNode = new WeakMap();
89
+ routingClaims.set(result, byNode);
90
+ }
91
+ let fields = byNode.get(site);
92
+ if (!fields) {
93
+ fields = new Set();
94
+ byNode.set(site, fields);
95
+ }
96
+ if (fields.has(field))
97
+ return false;
98
+ fields.add(field);
99
+ return true;
100
+ }
71
101
  /**
72
102
  * Transform a schema name if it exists in the mapping
73
103
  */
@@ -96,17 +126,31 @@ function shouldTransformSchema(schemaName, schemaMapping) {
96
126
  function transformNameList(names, schemaMapping, result, ns = 'unknown') {
97
127
  if (!names || names.length < 2)
98
128
  return;
129
+ if (!claimSite(result, names))
130
+ return;
99
131
  const router = asRouter(schemaMapping);
100
132
  // For schema-qualified names, the first element is the schema.
101
133
  const first = names[0];
102
134
  if (first?.String?.sval) {
103
135
  const schemaName = first.String.sval;
104
- const objName = names[names.length - 1]?.String?.sval;
105
- const newName = router.resolve(schemaName, objName, ns);
106
- if (newName && newName !== schemaName) {
136
+ const last = names[names.length - 1];
137
+ const objName = last?.String?.sval;
138
+ const target = router.resolveObject(schemaName, objName, ns);
139
+ if (!target)
140
+ return;
141
+ if (target.name !== undefined && last?.String?.sval) {
107
142
  result.schemasFound.add(schemaName);
108
- first.String.sval = newName;
109
- result.schemasTransformed.set(schemaName, newName);
143
+ last.String.sval = target.name;
144
+ }
145
+ if (target.schema === null) {
146
+ // De-qualify: drop the schema element and rely on search_path.
147
+ result.schemasFound.add(schemaName);
148
+ names.splice(0, 1);
149
+ }
150
+ else if (target.schema && target.schema !== schemaName) {
151
+ result.schemasFound.add(schemaName);
152
+ first.String.sval = target.schema;
153
+ result.schemasTransformed.set(schemaName, target.schema);
110
154
  }
111
155
  }
112
156
  }
@@ -119,6 +163,8 @@ function transformSchemaNameField(container, field, schemaMapping, result) {
119
163
  const schemaName = container?.[field];
120
164
  if (typeof schemaName !== 'string')
121
165
  return;
166
+ if (!claimSite(result, container, field))
167
+ return;
122
168
  // A bare schema name is an operation on the schema itself: only the
123
169
  // schema-level default applies.
124
170
  const newName = asRouter(schemaMapping).resolve(schemaName, undefined, 'schema');
@@ -130,19 +176,35 @@ function transformSchemaNameField(container, field, schemaMapping, result) {
130
176
  }
131
177
  /**
132
178
  * Transform a RangeVar-like relation object (has schemaname/relname fields).
133
- * Many statement nodes embed a relation that the walker does NOT auto-recurse into.
179
+ * The walker auto-recurses into embedded relations (concrete `RangeVar`
180
+ * fields are tag-synthesized), so the generic `RangeVar` visitor covers
181
+ * every occurrence; statement handlers call this directly only when they
182
+ * carry extra context, and the claim guard keeps each relation routed once.
134
183
  */
135
184
  function transformRelation(relation, schemaMapping, result) {
136
185
  if (!relation?.schemaname)
137
186
  return;
187
+ if (!claimSite(result, relation))
188
+ return;
138
189
  const oldName = relation.schemaname;
139
190
  // A RangeVar names a relation (table/view/sequence/matview); route by the
140
191
  // relation name so object-level routes can send it to its own schema.
141
- const newName = asRouter(schemaMapping).resolve(oldName, relation.relname, 'relation');
142
- if (newName && newName !== oldName) {
192
+ const target = asRouter(schemaMapping).resolveObject(oldName, relation.relname, 'relation');
193
+ if (!target)
194
+ return;
195
+ if (target.name !== undefined && relation.relname) {
143
196
  result.schemasFound.add(oldName);
144
- relation.schemaname = newName;
145
- result.schemasTransformed.set(oldName, newName);
197
+ relation.relname = target.name;
198
+ }
199
+ if (target.schema === null) {
200
+ // De-qualify: drop the schema qualifier and rely on search_path.
201
+ result.schemasFound.add(oldName);
202
+ delete relation.schemaname;
203
+ }
204
+ else if (target.schema && target.schema !== oldName) {
205
+ result.schemasFound.add(oldName);
206
+ relation.schemaname = target.schema;
207
+ result.schemasTransformed.set(oldName, target.schema);
146
208
  }
147
209
  }
148
210
  /**
@@ -183,6 +245,8 @@ function createSqlVisitor(schemaMapping, result, visitorOptions) {
183
245
  const schemaName = container?.[key];
184
246
  if (typeof schemaName !== 'string')
185
247
  return;
248
+ if (!claimSite(result, container, key))
249
+ return;
186
250
  const newName = router.resolve(schemaName, undefined, 'schema');
187
251
  if (newName && newName !== schemaName) {
188
252
  result.schemasFound.add(schemaName);
@@ -240,15 +304,6 @@ function createSqlVisitor(schemaMapping, result, visitorOptions) {
240
304
  const node = path.node;
241
305
  transformNameList(node.funcname, router, result, 'function');
242
306
  },
243
- // Transform CallStmt (CALL schema.procedure(...)).
244
- // Note: The walker does NOT auto-recurse into CallStmt.funccall,
245
- // so the FuncCall visitor above never sees it.
246
- CallStmt: (path) => {
247
- const node = path.node;
248
- if (node.funccall?.funcname) {
249
- transformNameList(node.funccall.funcname, router, result, 'function');
250
- }
251
- },
252
307
  // Transform TypeName nodes (type references with schema-qualified names)
253
308
  TypeName: (path) => {
254
309
  const node = path.node;
@@ -306,8 +361,9 @@ function createSqlVisitor(schemaMapping, result, visitorOptions) {
306
361
  }
307
362
  },
308
363
  // Transform DropStmt (DROP SCHEMA name; DROP TABLE/INDEX/POLICY/TRIGGER/
309
- // TYPE/FUNCTION schema.obj). The walker does NOT auto-recurse into
310
- // DropStmt.objects.
364
+ // TYPE/FUNCTION schema.obj) with the namespace derived from removeType,
365
+ // so object routes apply; claims stop the generic List/ObjectWithArgs
366
+ // visitors from re-routing the same names with namespace `unknown`.
311
367
  DropStmt: (path) => {
312
368
  const node = path.node;
313
369
  if (node.removeType !== 'OBJECT_SCHEMA' && Array.isArray(node.objects)) {
@@ -341,79 +397,10 @@ function createSqlVisitor(schemaMapping, result, visitorOptions) {
341
397
  }
342
398
  }
343
399
  },
344
- // Transform InsertStmt (INSERT INTO schema.table)
345
- // Note: The walker does NOT auto-recurse into InsertStmt.relation.
346
- InsertStmt: (path) => {
347
- transformRelation(path.node.relation, schemaMapping, result);
348
- },
349
- // Transform CreateStmt (CREATE TABLE schema.table)
350
- // Note: The walker does NOT auto-recurse into CreateStmt.relation.
351
- CreateStmt: (path) => {
352
- transformRelation(path.node.relation, schemaMapping, result);
353
- },
354
- // Transform UpdateStmt (UPDATE schema.table SET ...)
355
- // Note: The walker does NOT auto-recurse into UpdateStmt.relation.
356
- UpdateStmt: (path) => {
357
- transformRelation(path.node.relation, schemaMapping, result);
358
- },
359
- // Transform DeleteStmt (DELETE FROM schema.table)
360
- // Note: The walker does NOT auto-recurse into DeleteStmt.relation.
361
- DeleteStmt: (path) => {
362
- transformRelation(path.node.relation, schemaMapping, result);
363
- },
364
- // Transform MergeStmt (MERGE INTO schema.table USING ...)
365
- // Note: The walker does NOT auto-recurse into MergeStmt.relation.
366
- MergeStmt: (path) => {
367
- transformRelation(path.node.relation, schemaMapping, result);
368
- },
369
- // Transform IndexStmt (CREATE INDEX ... ON schema.table)
370
- // Note: The walker does NOT auto-recurse into IndexStmt.relation.
371
- IndexStmt: (path) => {
372
- transformRelation(path.node.relation, schemaMapping, result);
373
- },
374
- // Transform Constraint (REFERENCES schema.table for foreign keys)
375
- // Note: The walker does NOT auto-recurse into Constraint.pktable.
376
- Constraint: (path) => {
377
- const node = path.node;
378
- if (node.contype === 'CONSTR_FOREIGN' && node.pktable) {
379
- transformRelation(node.pktable, schemaMapping, result);
380
- }
381
- },
382
- // Transform CompositeTypeStmt (CREATE TYPE schema.name AS (...))
383
- // Note: The walker does NOT auto-recurse into CompositeTypeStmt.typevar.
384
- CompositeTypeStmt: (path) => {
385
- transformRelation(path.node.typevar, schemaMapping, result);
386
- },
387
- // Transform ColumnDef (column definitions with schema-qualified types)
388
- // Note: The walker does NOT auto-recurse into ColumnDef.typeName.
389
- ColumnDef: (path) => {
390
- const node = path.node;
391
- if (node.typeName?.names) {
392
- transformNameList(node.typeName.names, schemaMapping, result, 'type');
393
- }
394
- },
395
- // Transform ViewStmt (CREATE VIEW schema.viewname AS ...)
396
- // Note: The walker does NOT auto-recurse into ViewStmt.view.
397
- ViewStmt: (path) => {
398
- transformRelation(path.node.view, schemaMapping, result);
399
- },
400
- // Transform AlterTableStmt (ALTER TABLE schema.table ...)
401
- // Note: The walker does NOT auto-recurse into AlterTableStmt.relation.
402
- AlterTableStmt: (path) => {
403
- transformRelation(path.node.relation, schemaMapping, result);
404
- },
405
- // Transform CreateSeqStmt (CREATE SEQUENCE schema.seqname)
406
- // Note: The walker does NOT auto-recurse into CreateSeqStmt.sequence.
407
- CreateSeqStmt: (path) => {
408
- transformRelation(path.node.sequence, schemaMapping, result);
409
- },
410
- // Transform AlterSeqStmt (ALTER SEQUENCE schema.seqname [OWNED BY schema.table.col])
411
- // Note: The walker does NOT auto-recurse into AlterSeqStmt.sequence.
412
- // Also handles OWNED BY clause where the schema name is in DefElem.arg.List.items.
400
+ // Transform AlterSeqStmt OWNED BY (schema name in DefElem.arg.List.items;
401
+ // a bare name list no generic visitor covers)
413
402
  AlterSeqStmt: (path) => {
414
403
  const node = path.node;
415
- transformRelation(node.sequence, schemaMapping, result);
416
- // Handle OWNED BY option: schema name is in the name list
417
404
  if (node.options) {
418
405
  for (const opt of node.options) {
419
406
  if (opt?.DefElem?.defname === 'owned_by' && opt.DefElem?.arg?.List?.items) {
@@ -422,67 +409,12 @@ function createSqlVisitor(schemaMapping, result, visitorOptions) {
422
409
  }
423
410
  }
424
411
  },
425
- // Transform CreatePolicyStmt (CREATE POLICY ... ON schema.table)
426
- // Note: The walker does NOT auto-recurse into CreatePolicyStmt.table.
427
- CreatePolicyStmt: (path) => {
428
- transformRelation(path.node.table, schemaMapping, result);
429
- },
430
- // Transform RuleStmt (CREATE RULE ... ON schema.table)
431
- // Note: The walker does NOT auto-recurse into RuleStmt.relation.
432
- RuleStmt: (path) => {
433
- transformRelation(path.node.relation, schemaMapping, result);
434
- },
435
- // Transform CopyStmt (COPY schema.table ...)
436
- // Note: The walker does NOT auto-recurse into CopyStmt.relation.
437
- CopyStmt: (path) => {
438
- transformRelation(path.node.relation, schemaMapping, result);
439
- },
440
- // Transform ClusterStmt (CLUSTER schema.table USING index)
441
- // Note: The walker does NOT auto-recurse into ClusterStmt.relation.
442
- ClusterStmt: (path) => {
443
- transformRelation(path.node.relation, schemaMapping, result);
444
- },
445
- // Transform VacuumRelation (VACUUM/ANALYZE schema.table)
446
- // Note: The walker visits VacuumRelation but does NOT recurse into its relation.
447
- VacuumRelation: (path) => {
448
- transformRelation(path.node.relation, schemaMapping, result);
449
- },
450
- // Transform RefreshMatViewStmt (REFRESH MATERIALIZED VIEW schema.view)
451
- // Note: The walker does NOT auto-recurse into RefreshMatViewStmt.relation.
452
- RefreshMatViewStmt: (path) => {
453
- transformRelation(path.node.relation, schemaMapping, result);
454
- },
455
- // Transform CreateTableAsStmt (CREATE MATERIALIZED VIEW schema.viewname AS ...)
456
- // Note: The walker does NOT auto-recurse into CreateTableAsStmt.into.rel.
457
- CreateTableAsStmt: (path) => {
458
- transformRelation(path.node.into?.rel, schemaMapping, result);
459
- },
460
- // Transform TypeCast nodes (type casts like NULL::schema.type)
461
- // Note: The walker visits TypeCast but does NOT recurse into the TypeName node.
462
- TypeCast: (path) => {
463
- const node = path.node;
464
- if (node.typeName?.names) {
465
- transformNameList(node.typeName.names, schemaMapping, result, 'type');
466
- }
467
- },
468
- // Transform CreateFunctionStmt (CREATE FUNCTION schema.funcname)
469
- // Also handles RETURNS [SETOF] schema.type via returnType.names
412
+ // Transform CreateFunctionStmt (CREATE FUNCTION schema.funcname).
413
+ // funcname is a bare name list (no generic visitor); return and parameter
414
+ // types are TypeName nodes the walker reaches on its own.
470
415
  CreateFunctionStmt: (path) => {
471
416
  const node = path.node;
472
417
  transformNameList(node.funcname, schemaMapping, result, 'function');
473
- // Transform the return type (e.g., RETURNS SETOF schema.tablename)
474
- if (node.returnType?.names) {
475
- transformNameList(node.returnType.names, schemaMapping, result, 'type');
476
- }
477
- // Transform parameter types (the walker does NOT auto-recurse into
478
- // FunctionParameter.argType TypeName nodes)
479
- if (Array.isArray(node.parameters)) {
480
- for (const param of node.parameters) {
481
- if (param?.FunctionParameter?.argType?.names) {
482
- transformNameList(param.FunctionParameter.argType.names, schemaMapping, result, 'type');
483
- }
484
- }
485
- }
486
418
  // Transform schema-qualified references inside the function body
487
419
  // (AS $$...$$ is a String under the 'as' DefElem, which the walker
488
420
  // does not parse as SQL). LANGUAGE plpgsql bodies are additionally
@@ -509,12 +441,11 @@ function createSqlVisitor(schemaMapping, result, visitorOptions) {
509
441
  }
510
442
  },
511
443
  // Transform CreateTrigStmt (CREATE TRIGGER ... EXECUTE PROCEDURE schema.func)
512
- // Note: The walker does NOT auto-recurse into CreateTrigStmt.relation,
513
- // so we must explicitly transform the RangeVar here.
444
+ // funcname is a bare name list; the ON relation is a RangeVar the walker
445
+ // reaches on its own.
514
446
  CreateTrigStmt: (path) => {
515
447
  const node = path.node;
516
448
  transformNameList(node.funcname, schemaMapping, result, 'function');
517
- transformRelation(node.relation, schemaMapping, result);
518
449
  },
519
450
  // Transform CommentStmt (COMMENT ON TABLE/COLUMN/FUNCTION schema.obj,
520
451
  // COMMENT ON SCHEMA schema)
@@ -525,13 +456,14 @@ function createSqlVisitor(schemaMapping, result, visitorOptions) {
525
456
  // For functions, object is { ObjectWithArgs: { objname: [...] } }
526
457
  // For schemas, object is a bare { String: { sval } }
527
458
  if (node.object) {
459
+ const ns = namespaceOfObjectType(node.objtype);
528
460
  // Handle List-wrapped objects (COMMENT ON TABLE schema.tbl, COMMENT ON COLUMN schema.tbl.col)
529
461
  if (node.object?.List?.items) {
530
- transformNameList(node.object.List.items, schemaMapping, result);
462
+ transformNameList(node.object.List.items, schemaMapping, result, ns);
531
463
  }
532
464
  // Handle ObjectWithArgs-style objects (COMMENT ON FUNCTION schema.func(args))
533
465
  else if (node.object?.ObjectWithArgs?.objname) {
534
- transformNameList(node.object.ObjectWithArgs.objname, schemaMapping, result);
466
+ transformNameList(node.object.ObjectWithArgs.objname, schemaMapping, result, ns);
535
467
  }
536
468
  // Handle bare schema names (COMMENT ON SCHEMA schema)
537
469
  else if (node.object?.String && node.objtype === 'OBJECT_SCHEMA') {
@@ -588,41 +520,41 @@ function createSqlVisitor(schemaMapping, result, visitorOptions) {
588
520
  const node = path.node;
589
521
  transformNameList(node.objname, schemaMapping, result);
590
522
  },
591
- // Transform AlterObjectSchemaStmt (ALTER ... SET SCHEMA newschema)
592
- // The walker does NOT auto-recurse into AlterObjectSchemaStmt.relation
593
- // or .object, so we must transform relation, object names, and newschema.
523
+ // Transform AlterObjectSchemaStmt (ALTER ... SET SCHEMA newschema):
524
+ // object name lists and the destination schema. The relation form is a
525
+ // RangeVar the walker reaches on its own.
594
526
  AlterObjectSchemaStmt: (path) => {
595
527
  const node = path.node;
596
- transformRelation(node.relation, schemaMapping, result);
528
+ const ns = namespaceOfObjectType(node.objectType);
597
529
  // Non-relation objects (ALTER TYPE/FUNCTION ... SET SCHEMA)
598
530
  if (node.object?.List?.items) {
599
- transformNameList(node.object.List.items, schemaMapping, result);
531
+ transformNameList(node.object.List.items, schemaMapping, result, ns);
600
532
  }
601
533
  else if (node.object?.ObjectWithArgs?.objname) {
602
- transformNameList(node.object.ObjectWithArgs.objname, schemaMapping, result);
534
+ transformNameList(node.object.ObjectWithArgs.objname, schemaMapping, result, ns);
603
535
  }
604
536
  else if (node.object?.TypeName?.names) {
605
- transformNameList(node.object.TypeName.names, schemaMapping, result);
537
+ transformNameList(node.object.TypeName.names, schemaMapping, result, ns);
606
538
  }
607
539
  // Transform the newschema (destination schema)
608
540
  transformSchemaNameField(node, 'newschema', schemaMapping, result);
609
541
  },
610
542
  // Transform RenameStmt (ALTER SCHEMA old RENAME TO new;
611
- // ALTER TABLE schema.tbl RENAME ...). The walker does NOT auto-recurse
612
- // into RenameStmt.relation or .object.
543
+ // ALTER TABLE schema.tbl RENAME ...). The relation form is a RangeVar
544
+ // the walker reaches on its own.
613
545
  RenameStmt: (path) => {
614
546
  const node = path.node;
615
- transformRelation(node.relation, schemaMapping, result);
616
547
  if (node.renameType === 'OBJECT_SCHEMA') {
617
548
  // subname is the old schema name; newname is the target
618
549
  transformSchemaNameField(node, 'subname', schemaMapping, result);
619
550
  transformSchemaNameField(node, 'newname', schemaMapping, result);
620
551
  }
552
+ const ns = namespaceOfObjectType(node.renameType);
621
553
  if (node.object?.List?.items) {
622
- transformNameList(node.object.List.items, schemaMapping, result);
554
+ transformNameList(node.object.List.items, schemaMapping, result, ns);
623
555
  }
624
556
  else if (node.object?.ObjectWithArgs?.objname) {
625
- transformNameList(node.object.ObjectWithArgs.objname, schemaMapping, result);
557
+ transformNameList(node.object.ObjectWithArgs.objname, schemaMapping, result, ns);
626
558
  }
627
559
  },
628
560
  // Transform AlterFunctionStmt (ALTER FUNCTION schema.fn(...) SET ...)
@@ -635,46 +567,26 @@ function createSqlVisitor(schemaMapping, result, visitorOptions) {
635
567
  // Transform AlterOwnerStmt (ALTER ... OWNER TO role)
636
568
  AlterOwnerStmt: (path) => {
637
569
  const node = path.node;
638
- transformRelation(node.relation, schemaMapping, result);
570
+ const ns = namespaceOfObjectType(node.objectType);
639
571
  if (node.object?.List?.items) {
640
- transformNameList(node.object.List.items, schemaMapping, result);
572
+ transformNameList(node.object.List.items, schemaMapping, result, ns);
641
573
  }
642
574
  else if (node.object?.ObjectWithArgs?.objname) {
643
- transformNameList(node.object.ObjectWithArgs.objname, schemaMapping, result);
575
+ transformNameList(node.object.ObjectWithArgs.objname, schemaMapping, result, ns);
644
576
  }
645
577
  else if (node.object?.String && node.objectType === 'OBJECT_SCHEMA') {
646
578
  transformSchemaNameField(node.object.String, 'sval', schemaMapping, result);
647
579
  }
648
580
  },
649
- // Transform CreateCastStmt (CREATE CAST (src AS tgt) WITH FUNCTION fn)
581
+ // Transform CreateCastStmt (CREATE CAST (src AS tgt) WITH FUNCTION fn).
582
+ // The function name gets its `function` namespace here; the cast's
583
+ // source/target/argument types are TypeName nodes the walker reaches on
584
+ // its own.
650
585
  CreateCastStmt: (path) => {
651
586
  const node = path.node;
652
- if (node.sourcetype?.names) {
653
- transformNameList(node.sourcetype.names, schemaMapping, result, 'type');
654
- }
655
- if (node.targettype?.names) {
656
- transformNameList(node.targettype.names, schemaMapping, result, 'type');
657
- }
658
587
  if (node.func?.objname) {
659
588
  transformNameList(node.func.objname, schemaMapping, result, 'function');
660
589
  }
661
- if (Array.isArray(node.func?.objargs)) {
662
- for (const arg of node.func.objargs) {
663
- if (arg?.TypeName?.names) {
664
- transformNameList(arg.TypeName.names, schemaMapping, result, 'type');
665
- }
666
- else if (arg?.names) {
667
- transformNameList(arg.names, schemaMapping, result, 'type');
668
- }
669
- }
670
- }
671
- if (Array.isArray(node.func?.objfuncargs)) {
672
- for (const param of node.func.objfuncargs) {
673
- if (param?.FunctionParameter?.argType?.names) {
674
- transformNameList(param.FunctionParameter.argType.names, schemaMapping, result, 'type');
675
- }
676
- }
677
- }
678
590
  },
679
591
  // Transform CreateEventTrigStmt (CREATE EVENT TRIGGER ... EXECUTE FUNCTION schema.fn())
680
592
  CreateEventTrigStmt: (path) => {