@pgsql/transform 18.3.1 → 18.4.1

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/esm/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export { classifyStatements } from './facts';
2
2
  export { collectCreatedObjects, createQualifyVisitor, mergeInventories, qualifyUnqualified, } from './qualify';
3
+ export { SchemaRouter } from './router';
3
4
  export { captureAstsFromSql, captureTransformAsts, firstDifference, normalizeParseTree, validateRoundTrip, } from './round-trip';
4
5
  export { CLEAN_TREE_VOLATILE_KEYS, cleanTree, normalizeTree, trimDefElemBody, } from './round-trip-core';
5
6
  export { createPlpgsqlVisitor, createSqlVisitor, escapeRegexp, extractPgpmHeader, shouldTransformSchema, transformComments, transformJsonStringValues, transformNameList, transformPlpgsqlTypeAst, transformPlpgsqlTypeString, transformRelation, transformSchemaName, transformSql, transformSqlStatement, transformVerifyCalls, validateNoUntransformedSchemas, walkPlpgsqlForSchemas, } from './transform';
package/esm/router.js ADDED
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Schema routing for the core transform.
3
+ *
4
+ * The schema transform historically rewrote every reference to a source
5
+ * schema to a single target schema (`Map<oldSchema, newSchema>`). That is the
6
+ * degenerate, whole-schema case of a more general question asked at every
7
+ * schema-qualified occurrence:
8
+ *
9
+ * given a reference to `(schema, name)` in namespace `ns`, what schema
10
+ * should it live in now?
11
+ *
12
+ * A {@link SchemaRouter} answers exactly that. It unifies two dimensions:
13
+ *
14
+ * - **schema-level** default: move everything in a source schema to one target
15
+ * (the classic `Map` behaviour), and
16
+ * - **object-level** routes: send a specific object — a table, a function, a
17
+ * type — to its own target schema, independent of its siblings.
18
+ *
19
+ * Object routes are bucketed by PostgreSQL namespace (`relations`, `functions`,
20
+ * `types` — matching `pg_class` / `pg_proc` / `pg_type`), mirroring the routing
21
+ * model already used by {@link qualifyUnqualified}. Resolution is
22
+ * object-route-first, then the schema-level default, then "leave unchanged".
23
+ */
24
+ const NS_BUCKET = {
25
+ relation: 'relations',
26
+ function: 'functions',
27
+ type: 'types'
28
+ };
29
+ /**
30
+ * Resolves the target schema for any schema-qualified occurrence, unifying the
31
+ * whole-schema `Map` behaviour and per-object routing behind one `resolve`.
32
+ */
33
+ export class SchemaRouter {
34
+ routes;
35
+ constructor(routes = {}) {
36
+ this.routes = routes instanceof Map ? new Map(routes) : new Map(Object.entries(routes));
37
+ }
38
+ /** Build a router from the classic whole-schema `Map<oldSchema,newSchema>`. */
39
+ static fromSchemaMap(mapping) {
40
+ const entries = mapping instanceof Map ? [...mapping.entries()] : Object.entries(mapping);
41
+ const spec = {};
42
+ for (const [from, to] of entries)
43
+ spec[from] = { schema: to };
44
+ return new SchemaRouter(spec);
45
+ }
46
+ /** Coerce a `Map`, plain mapping, or existing router into a router. */
47
+ static from(source) {
48
+ if (source instanceof SchemaRouter)
49
+ return source;
50
+ return SchemaRouter.fromSchemaMap(source);
51
+ }
52
+ /** True when this router might rewrite something in `sourceSchema`. */
53
+ has(sourceSchema) {
54
+ if (!sourceSchema)
55
+ return false;
56
+ return this.routes.has(sourceSchema);
57
+ }
58
+ /** True when the router carries no routes at all. */
59
+ get size() {
60
+ return this.routes.size;
61
+ }
62
+ /**
63
+ * True when any route targets individual objects (as opposed to whole
64
+ * schemas). Object routes require AST-precise rewriting of opaque function
65
+ * bodies; whole-schema routes are handled by the cheaper string passes.
66
+ */
67
+ hasObjectRoutes() {
68
+ for (const route of this.routes.values()) {
69
+ if (route.relations || route.functions || route.types)
70
+ return true;
71
+ }
72
+ return false;
73
+ }
74
+ /** Every source schema this router may touch. */
75
+ sourceSchemas() {
76
+ return [...this.routes.keys()];
77
+ }
78
+ /**
79
+ * Resolve the target schema for `(sourceSchema, name)` in namespace `ns`,
80
+ * or `undefined` to leave it unchanged. Object routes win over the
81
+ * schema-level default; the default applies to schema-level operations and
82
+ * to any object without a specific route.
83
+ */
84
+ resolve(sourceSchema, name, ns = 'unknown') {
85
+ if (!sourceSchema)
86
+ return undefined;
87
+ const route = this.routes.get(sourceSchema);
88
+ if (!route)
89
+ return undefined;
90
+ if (name && (ns === 'relation' || ns === 'function' || ns === 'type')) {
91
+ const bucket = route[NS_BUCKET[ns]];
92
+ const mapped = bucket?.[name];
93
+ if (mapped !== undefined)
94
+ return mapped;
95
+ }
96
+ return route.schema;
97
+ }
98
+ /**
99
+ * Source schemas whose *every* object is guaranteed to move — i.e. those
100
+ * with a schema-level default. After a transform, none of these should
101
+ * survive as a qualifier; partially-routed schemas may legitimately remain,
102
+ * so they are excluded from the strict leftover check.
103
+ */
104
+ fullyMovedSchemas() {
105
+ const out = new Map();
106
+ for (const [from, route] of this.routes) {
107
+ if (route.schema !== undefined)
108
+ out.set(from, route.schema);
109
+ }
110
+ return out;
111
+ }
112
+ /**
113
+ * A flat schema-level view (`oldSchema → newSchema`) for legacy string-level
114
+ * passes and comment/verify/JSON rewrites that operate per source schema.
115
+ * Only schema-level defaults are included; object-only routes carry no single
116
+ * schema answer and are omitted.
117
+ */
118
+ schemaLevelMap() {
119
+ return this.fullyMovedSchemas();
120
+ }
121
+ }