@prisma-next/sql-schema-ir 0.14.0 → 0.15.0-dev.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/dist/exports/types.d.mts +2 -2
- package/dist/exports/types.mjs +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/types-CXbBE9Uv.d.mts +580 -0
- package/dist/types-CXbBE9Uv.d.mts.map +1 -0
- package/dist/types-CZqCTmou.mjs +679 -0
- package/dist/types-CZqCTmou.mjs.map +1 -0
- package/package.json +9 -8
- package/src/exports/types.ts +6 -0
- package/src/ir/primary-key.ts +38 -2
- package/src/ir/resolved-default-equality.ts +79 -0
- package/src/ir/schema-node-kinds.ts +86 -0
- package/src/ir/sql-check-constraint-ir.ts +43 -2
- package/src/ir/sql-column-default-ir.ts +96 -0
- package/src/ir/sql-column-ir.ts +142 -2
- package/src/ir/sql-foreign-key-ir.ts +71 -2
- package/src/ir/sql-index-ir.ts +84 -2
- package/src/ir/sql-schema-ir-node.ts +58 -7
- package/src/ir/sql-schema-ir.ts +22 -1
- package/src/ir/sql-table-ir.ts +31 -1
- package/src/ir/sql-unique-ir.ts +35 -2
- package/src/types.ts +10 -1
- package/dist/types-C0WFfEkA.mjs +0 -220
- package/dist/types-C0WFfEkA.mjs.map +0 -1
- package/dist/types-Du44_nsJ.d.mts +0 -261
- package/dist/types-Du44_nsJ.d.mts.map +0 -1
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
import { IRNodeBase, freezeNode } from "@prisma-next/framework-components/ir";
|
|
2
|
+
import { blindCast } from "@prisma-next/utils/casts";
|
|
3
|
+
import { canonicalStringify } from "@prisma-next/utils/canonical-stringify";
|
|
4
|
+
import { ifDefined } from "@prisma-next/utils/defined";
|
|
5
|
+
//#region src/ir/schema-node-kinds.ts
|
|
6
|
+
/**
|
|
7
|
+
* The `nodeKind` discriminant for each relational schema-diff leaf node.
|
|
8
|
+
* Each node carries a unique value; the differ pairs siblings by `id`, and
|
|
9
|
+
* these kinds distinguish the node types that appear as `PostgresTableSchemaNode`
|
|
10
|
+
* children (columns, primary key, foreign keys, uniques, indexes, checks) from
|
|
11
|
+
* each other and from the RLS policy/role kinds a target defines separately.
|
|
12
|
+
*/
|
|
13
|
+
const RelationalSchemaNodeKind = {
|
|
14
|
+
schema: "sql-schema",
|
|
15
|
+
table: "sql-table",
|
|
16
|
+
column: "sql-column",
|
|
17
|
+
columnDefault: "sql-column-default",
|
|
18
|
+
primaryKey: "sql-primary-key",
|
|
19
|
+
foreignKey: "sql-foreign-key",
|
|
20
|
+
unique: "sql-unique",
|
|
21
|
+
index: "sql-index",
|
|
22
|
+
check: "sql-check-constraint"
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The one real map from a relational `nodeKind` to the framework-neutral
|
|
26
|
+
* {@link DiffSubjectGranularity} its diff issues carry — the SQL family's
|
|
27
|
+
* post-diff filters (issue category, strict-mode gating) and the framework
|
|
28
|
+
* aggregate's unclaimed-elements sweep key on the granularity, never on the
|
|
29
|
+
* `nodeKind` spelling and never on anything stamped on the node. Resolution
|
|
30
|
+
* is by `nodeKind` equality against this map, not a suffix string match.
|
|
31
|
+
* Target-specific node kinds (Postgres namespace/table/policy/role) are
|
|
32
|
+
* outside this family layer's vocabulary and map their own kinds directly.
|
|
33
|
+
*/
|
|
34
|
+
const RELATIONAL_NODE_GRANULARITY = {
|
|
35
|
+
[RelationalSchemaNodeKind.schema]: "structural",
|
|
36
|
+
[RelationalSchemaNodeKind.table]: "entity",
|
|
37
|
+
[RelationalSchemaNodeKind.column]: "field",
|
|
38
|
+
[RelationalSchemaNodeKind.columnDefault]: "auxiliary",
|
|
39
|
+
[RelationalSchemaNodeKind.primaryKey]: "auxiliary",
|
|
40
|
+
[RelationalSchemaNodeKind.foreignKey]: "auxiliary",
|
|
41
|
+
[RelationalSchemaNodeKind.unique]: "auxiliary",
|
|
42
|
+
[RelationalSchemaNodeKind.index]: "auxiliary",
|
|
43
|
+
[RelationalSchemaNodeKind.check]: "auxiliary"
|
|
44
|
+
};
|
|
45
|
+
function isRelationalSchemaNodeKind(nodeKind) {
|
|
46
|
+
return Object.hasOwn(RELATIONAL_NODE_GRANULARITY, nodeKind);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Looks up the subject granularity for a relational `nodeKind`. Throws for a
|
|
50
|
+
* `nodeKind` outside this map (a target-specific kind, e.g. Postgres's
|
|
51
|
+
* namespace/table/policy/role) — those map their own kinds directly rather
|
|
52
|
+
* than through this family-level map.
|
|
53
|
+
*/
|
|
54
|
+
function relationalNodeGranularity(nodeKind) {
|
|
55
|
+
if (!isRelationalSchemaNodeKind(nodeKind)) throw new Error(`relationalNodeGranularity: unrecognized relational node kind "${nodeKind}"`);
|
|
56
|
+
return RELATIONAL_NODE_GRANULARITY[nodeKind];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The one real map from a relational `nodeKind` to its storage `entityKind`
|
|
60
|
+
* — the same vocabulary the contract storage's `entries` dictionary keys use
|
|
61
|
+
* (`elementCoordinates` walks it). Only the whole-table kind has an entity of
|
|
62
|
+
* its own; every other relational node (a column, an index, …) is nested
|
|
63
|
+
* under one and maps to nothing here.
|
|
64
|
+
*/
|
|
65
|
+
const RELATIONAL_NODE_ENTITY_KIND = { [RelationalSchemaNodeKind.table]: "table" };
|
|
66
|
+
/**
|
|
67
|
+
* Looks up the storage `entityKind` for a relational `nodeKind` — sibling of
|
|
68
|
+
* {@link relationalNodeGranularity}, resolved by `nodeKind` equality against
|
|
69
|
+
* {@link RELATIONAL_NODE_ENTITY_KIND}. `undefined` for a target-specific kind
|
|
70
|
+
* (targets map their own kinds directly) or a relational kind with no entity
|
|
71
|
+
* of its own.
|
|
72
|
+
*/
|
|
73
|
+
function relationalNodeEntityKind(nodeKind) {
|
|
74
|
+
return isRelationalSchemaNodeKind(nodeKind) ? RELATIONAL_NODE_ENTITY_KIND[nodeKind] : void 0;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/ir/sql-schema-ir-node.ts
|
|
78
|
+
/**
|
|
79
|
+
* SQL Schema IR node base. Carries the family-level
|
|
80
|
+
* `kind = 'sql-schema-ir'` discriminator and inherits the framework's
|
|
81
|
+
* `freezeNode` affordance.
|
|
82
|
+
*
|
|
83
|
+
* SQL Schema IR represents the actual database state as discovered by
|
|
84
|
+
* introspection (the parallel to SQL Contract IR, which represents the
|
|
85
|
+
* desired state).
|
|
86
|
+
*
|
|
87
|
+
* The discriminator is installed as a non-enumerable own property,
|
|
88
|
+
* matching the SqlNode pattern. This keeps `JSON.stringify(node)`
|
|
89
|
+
* canonical (no `kind` field), keeps `toEqual({...})` test assertions
|
|
90
|
+
* against pre-lift flat shapes passing, and keeps `node.kind` readable
|
|
91
|
+
* for dispatch.
|
|
92
|
+
*
|
|
93
|
+
* Both `kind` and `nodeKind` are required: every concrete leaf is a node
|
|
94
|
+
* the generic differ can pair and compare, so every leaf must declare which
|
|
95
|
+
* node it is. `nodeKind` has no default here — every direct subclass sets its
|
|
96
|
+
* own literal value (the relational leaves via `RelationalSchemaNodeKind`,
|
|
97
|
+
* target concretions via their own vocabulary, e.g. `PostgresSchemaNodeKind`).
|
|
98
|
+
*/
|
|
99
|
+
var SqlSchemaIRNode = class extends IRNodeBase {
|
|
100
|
+
constructor() {
|
|
101
|
+
super();
|
|
102
|
+
Object.defineProperty(this, "kind", {
|
|
103
|
+
value: "sql-schema-ir",
|
|
104
|
+
writable: false,
|
|
105
|
+
enumerable: false,
|
|
106
|
+
configurable: false
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Asserts `node` matches `predicate`, narrowing its type to `T`. The one
|
|
112
|
+
* shared implementation every node class's `static assert` and `isEqualTo`
|
|
113
|
+
* reach for, instead of each hand-writing its own throw: the message names
|
|
114
|
+
* the class the caller expected.
|
|
115
|
+
*/
|
|
116
|
+
function assertNode(node, className, predicate) {
|
|
117
|
+
if (node === void 0 || !predicate(node)) throw new Error(`Expected a ${className} but got nodeKind=${node?.nodeKind ?? "undefined"}`);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Defines a non-enumerable own property, the same treatment `kind` gets
|
|
121
|
+
* above: a derivation-time render-support field stays out of
|
|
122
|
+
* `JSON.stringify`, `toEqual({...})` structural assertions, and spreads,
|
|
123
|
+
* while remaining directly readable (`node.field`) for the one consumer
|
|
124
|
+
* that resolves it at plan time. A no-op when `value` is `undefined` — the
|
|
125
|
+
* property is simply absent, matching every other optional field on these
|
|
126
|
+
* nodes.
|
|
127
|
+
*/
|
|
128
|
+
function defineNonEnumerable(target, key, value) {
|
|
129
|
+
if (value === void 0) return;
|
|
130
|
+
Object.defineProperty(target, key, {
|
|
131
|
+
value,
|
|
132
|
+
enumerable: false,
|
|
133
|
+
writable: false,
|
|
134
|
+
configurable: false
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src/ir/primary-key.ts
|
|
139
|
+
/**
|
|
140
|
+
* Primary-key Schema IR node. Mirrors the Contract IR `PrimaryKey`
|
|
141
|
+
* shape (same `columns` + optional `name`) so verification can compare
|
|
142
|
+
* intent and actual structurally. Defined here independently to avoid
|
|
143
|
+
* a sql-schema-ir -> sql-contract dependency.
|
|
144
|
+
*
|
|
145
|
+
* Implements `DiffableNode` so a primary key is directly a table's diff-tree
|
|
146
|
+
* child. `id` is a fixed sentinel — a table has at most one primary key, so
|
|
147
|
+
* there is never a sibling to disambiguate against. `isEqualTo` compares the
|
|
148
|
+
* column tuple; the PK's own `name` is a database-assigned label with no
|
|
149
|
+
* semantic weight, so it is not compared (mirrors the policy node's
|
|
150
|
+
* name-insensitivity to non-identifying detail).
|
|
151
|
+
*/
|
|
152
|
+
var PrimaryKey = class PrimaryKey extends SqlSchemaIRNode {
|
|
153
|
+
nodeKind = RelationalSchemaNodeKind.primaryKey;
|
|
154
|
+
columns;
|
|
155
|
+
constructor(input) {
|
|
156
|
+
super();
|
|
157
|
+
this.columns = input.columns;
|
|
158
|
+
if (input.name !== void 0) this.name = input.name;
|
|
159
|
+
freezeNode(this);
|
|
160
|
+
}
|
|
161
|
+
get id() {
|
|
162
|
+
return "primary-key";
|
|
163
|
+
}
|
|
164
|
+
children() {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
static is(node) {
|
|
168
|
+
return node.nodeKind === RelationalSchemaNodeKind.primaryKey;
|
|
169
|
+
}
|
|
170
|
+
isEqualTo(other) {
|
|
171
|
+
const node = blindCast(other);
|
|
172
|
+
assertNode(node, "PrimaryKey", PrimaryKey.is);
|
|
173
|
+
return this.columns.length === node.columns.length && this.columns.every((c, i) => c === node.columns[i]);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/ir/sql-check-constraint-ir.ts
|
|
178
|
+
/**
|
|
179
|
+
* Schema IR node for a table-level check constraint that restricts a
|
|
180
|
+
* column to a set of permitted values (an enum-style `IN (...)` check).
|
|
181
|
+
*
|
|
182
|
+
* Carries the **resolved values** rather than a raw SQL predicate so
|
|
183
|
+
* callers can compare value-sets without parsing SQL.
|
|
184
|
+
*
|
|
185
|
+
* Implements `DiffableNode` so a check constraint is directly a table's
|
|
186
|
+
* diff-tree child: `id` is the constraint name. `isEqualTo` compares
|
|
187
|
+
* `column` and the permitted-value set (order-insensitive — the database
|
|
188
|
+
* does not guarantee `IN (...)` ordering).
|
|
189
|
+
*/
|
|
190
|
+
var SqlCheckConstraintIR = class SqlCheckConstraintIR extends SqlSchemaIRNode {
|
|
191
|
+
nodeKind = RelationalSchemaNodeKind.check;
|
|
192
|
+
name;
|
|
193
|
+
column;
|
|
194
|
+
permittedValues;
|
|
195
|
+
constructor(input) {
|
|
196
|
+
super();
|
|
197
|
+
this.name = input.name;
|
|
198
|
+
this.column = input.column;
|
|
199
|
+
this.permittedValues = Object.freeze([...input.permittedValues]);
|
|
200
|
+
freezeNode(this);
|
|
201
|
+
}
|
|
202
|
+
get id() {
|
|
203
|
+
return `check:${this.name}`;
|
|
204
|
+
}
|
|
205
|
+
children() {
|
|
206
|
+
return [];
|
|
207
|
+
}
|
|
208
|
+
static is(node) {
|
|
209
|
+
return node.nodeKind === RelationalSchemaNodeKind.check;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Compares the permitted-value sets only (unordered), matching the
|
|
213
|
+
* relational walk's `verifyCheckConstraints`: two checks pairing by name
|
|
214
|
+
* compare their value sets — the `column` field is descriptive, not part
|
|
215
|
+
* of the comparison, so a name-paired check with equal values verifies
|
|
216
|
+
* regardless of which column carries it.
|
|
217
|
+
*/
|
|
218
|
+
isEqualTo(other) {
|
|
219
|
+
const node = blindCast(other);
|
|
220
|
+
assertNode(node, "SqlCheckConstraintIR", SqlCheckConstraintIR.is);
|
|
221
|
+
const thisValues = new Set(this.permittedValues);
|
|
222
|
+
const otherValues = new Set(node.permittedValues);
|
|
223
|
+
if (thisValues.size !== otherValues.size) return false;
|
|
224
|
+
return [...thisValues].every((v) => otherValues.has(v));
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
//#endregion
|
|
228
|
+
//#region src/ir/resolved-default-equality.ts
|
|
229
|
+
/**
|
|
230
|
+
* Structural equality for two resolved column defaults, ported from the
|
|
231
|
+
* relational walk's `columnDefaultsEqual` normalized branch: kinds must
|
|
232
|
+
* match; literal values are normalized (Date and temporal-typed strings to
|
|
233
|
+
* ISO instants) then compared canonically (JSON objects match their
|
|
234
|
+
* canonical string form); function expressions compare case- and
|
|
235
|
+
* whitespace-insensitively.
|
|
236
|
+
*
|
|
237
|
+
* `nativeType` provides the temporal-normalization context (the actual
|
|
238
|
+
* side's resolved native type in a diff comparison).
|
|
239
|
+
*/
|
|
240
|
+
function resolvedDefaultsEqual(expected, actual, nativeType) {
|
|
241
|
+
if (expected.kind !== actual.kind) return false;
|
|
242
|
+
if (expected.kind === "literal" && actual.kind === "literal") return literalValuesEqual(normalizeLiteralValue(expected.value, nativeType), normalizeLiteralValue(actual.value, nativeType));
|
|
243
|
+
if (expected.kind === "function" && actual.kind === "function") return normalizeFunctionExpression(expected.expression) === normalizeFunctionExpression(actual.expression);
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
function normalizeFunctionExpression(expression) {
|
|
247
|
+
return expression.toLowerCase().replace(/\s+/g, "");
|
|
248
|
+
}
|
|
249
|
+
function isTemporalNativeType(nativeType) {
|
|
250
|
+
if (!nativeType) return false;
|
|
251
|
+
const normalized = nativeType.toLowerCase();
|
|
252
|
+
return normalized.includes("timestamp") || normalized === "date";
|
|
253
|
+
}
|
|
254
|
+
function normalizeLiteralValue(value, nativeType) {
|
|
255
|
+
if (value instanceof Date) return value.toISOString();
|
|
256
|
+
if (typeof value === "string" && isTemporalNativeType(nativeType)) {
|
|
257
|
+
const parsed = new Date(value);
|
|
258
|
+
if (!Number.isNaN(parsed.getTime())) return parsed.toISOString();
|
|
259
|
+
}
|
|
260
|
+
return value;
|
|
261
|
+
}
|
|
262
|
+
function literalValuesEqual(a, b) {
|
|
263
|
+
if (a === b) return true;
|
|
264
|
+
if (typeof a === "object" && a !== null && typeof b === "object" && b !== null) return canonicalStringify(a) === canonicalStringify(b);
|
|
265
|
+
if (typeof a === "object" && a !== null && typeof b === "string") try {
|
|
266
|
+
return canonicalStringify(a) === canonicalStringify(JSON.parse(b));
|
|
267
|
+
} catch {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
if (typeof a === "string" && typeof b === "object" && b !== null) try {
|
|
271
|
+
return canonicalStringify(JSON.parse(a)) === canonicalStringify(b);
|
|
272
|
+
} catch {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
//#endregion
|
|
278
|
+
//#region src/ir/sql-column-default-ir.ts
|
|
279
|
+
/**
|
|
280
|
+
* Schema-diff node for a column's default value. The default is the one
|
|
281
|
+
* column attribute with an extra/missing/drift lifecycle of its own, so it
|
|
282
|
+
* is a child node of the column rather than a compared attribute: an
|
|
283
|
+
* undeclared live default surfaces as `not-expected`, a declared default the
|
|
284
|
+
* database lacks as `not-found`, and a divergent value as `not-equal` — the
|
|
285
|
+
* three reasons cover the legacy `extra_default` / `default_missing` /
|
|
286
|
+
* `default_mismatch` vocabulary with no attribute inspection.
|
|
287
|
+
*
|
|
288
|
+
* `id` is a fixed sentinel — a column has at most one default. Built
|
|
289
|
+
* transiently by `SqlColumnIR.children()` from the column's own fields;
|
|
290
|
+
* never constructed by derivation or introspection directly.
|
|
291
|
+
*/
|
|
292
|
+
var SqlColumnDefaultIR = class SqlColumnDefaultIR extends SqlSchemaIRNode {
|
|
293
|
+
nodeKind = RelationalSchemaNodeKind.columnDefault;
|
|
294
|
+
constructor(input) {
|
|
295
|
+
super();
|
|
296
|
+
if (input.resolved !== void 0) this.resolved = input.resolved;
|
|
297
|
+
if (input.raw !== void 0) this.raw = input.raw;
|
|
298
|
+
if (input.nativeTypeContext !== void 0) this.nativeTypeContext = input.nativeTypeContext;
|
|
299
|
+
defineNonEnumerable(this, "many", input.many);
|
|
300
|
+
freezeNode(this);
|
|
301
|
+
}
|
|
302
|
+
get id() {
|
|
303
|
+
return "default";
|
|
304
|
+
}
|
|
305
|
+
children() {
|
|
306
|
+
return [];
|
|
307
|
+
}
|
|
308
|
+
static is(node) {
|
|
309
|
+
return node.nodeKind === RelationalSchemaNodeKind.columnDefault;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Structured comparison with `this` as the expected side: both sides
|
|
313
|
+
* resolved compare per the relational walk's `columnDefaultsEqual`
|
|
314
|
+
* semantics; a declared expected default against an unparseable actual
|
|
315
|
+
* (raw present, no resolved parse) is a mismatch; two raw-only nodes fall
|
|
316
|
+
* back to raw string equality.
|
|
317
|
+
*/
|
|
318
|
+
isEqualTo(other) {
|
|
319
|
+
const node = blindCast(other);
|
|
320
|
+
assertNode(node, "SqlColumnDefaultIR", SqlColumnDefaultIR.is);
|
|
321
|
+
if (this.resolved !== void 0 && node.resolved !== void 0) return resolvedDefaultsEqual(this.resolved, node.resolved, node.nativeTypeContext ?? this.nativeTypeContext);
|
|
322
|
+
if (this.resolved !== void 0 || node.resolved !== void 0) return false;
|
|
323
|
+
return this.raw === node.raw;
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
//#endregion
|
|
327
|
+
//#region src/ir/sql-column-ir.ts
|
|
328
|
+
/**
|
|
329
|
+
* Schema IR node for a single column on a table, as observed by
|
|
330
|
+
* introspection. Unlike the Contract IR `StorageColumn`, this carries
|
|
331
|
+
* the column's `name` (Schema IR columns are returned as arrays from
|
|
332
|
+
* introspection queries; the parent table re-keys them into a record
|
|
333
|
+
* for downstream consumers).
|
|
334
|
+
*
|
|
335
|
+
* Implements `DiffableNode` so a column is directly a table's diff-tree
|
|
336
|
+
* child: `id` is the column name (unique among a table's columns); `isEqualTo`
|
|
337
|
+
* compares this column's own attributes only — never children, since a
|
|
338
|
+
* column is a leaf. When both sides carry `resolvedNativeType` (stamped at
|
|
339
|
+
* derivation/introspection), the comparison uses the resolved values —
|
|
340
|
+
* resolved native type, nullability, and structured default equality per
|
|
341
|
+
* the relational walk's `columnDefaultsEqual` semantics, with `this` as the
|
|
342
|
+
* expected side. Otherwise it falls back to comparing raw fields.
|
|
343
|
+
*/
|
|
344
|
+
var SqlColumnIR = class SqlColumnIR extends SqlSchemaIRNode {
|
|
345
|
+
nodeKind = RelationalSchemaNodeKind.column;
|
|
346
|
+
name;
|
|
347
|
+
nativeType;
|
|
348
|
+
nullable;
|
|
349
|
+
constructor(input) {
|
|
350
|
+
super();
|
|
351
|
+
this.name = input.name;
|
|
352
|
+
this.nativeType = input.nativeType;
|
|
353
|
+
this.nullable = input.nullable;
|
|
354
|
+
if (input.default !== void 0) this.default = input.default;
|
|
355
|
+
if (input.annotations !== void 0) this.annotations = input.annotations;
|
|
356
|
+
if (input.many !== void 0) this.many = input.many;
|
|
357
|
+
if (input.resolvedNativeType !== void 0) this.resolvedNativeType = input.resolvedNativeType;
|
|
358
|
+
if (input.resolvedDefault !== void 0) this.resolvedDefault = input.resolvedDefault;
|
|
359
|
+
defineNonEnumerable(this, "codecRef", input.codecRef);
|
|
360
|
+
defineNonEnumerable(this, "codecBaseNativeType", input.codecBaseNativeType);
|
|
361
|
+
defineNonEnumerable(this, "codecNamedType", input.codecNamedType);
|
|
362
|
+
freezeNode(this);
|
|
363
|
+
}
|
|
364
|
+
get id() {
|
|
365
|
+
return `column:${this.name}`;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* The column's default, when declared/present, is the column's one child
|
|
369
|
+
* node — it has an extra/missing/drift lifecycle of its own, so the differ
|
|
370
|
+
* recurses to it rather than `isEqualTo` comparing it. Built transiently
|
|
371
|
+
* from this column's own fields.
|
|
372
|
+
*/
|
|
373
|
+
children() {
|
|
374
|
+
if (this.resolvedDefault === void 0 && this.default === void 0) return [];
|
|
375
|
+
return [new SqlColumnDefaultIR({
|
|
376
|
+
...ifDefined("resolved", this.resolvedDefault),
|
|
377
|
+
...ifDefined("raw", this.default),
|
|
378
|
+
...ifDefined("nativeTypeContext", this.resolvedNativeType),
|
|
379
|
+
...ifDefined("many", this.many ?? this.codecRef?.many)
|
|
380
|
+
})];
|
|
381
|
+
}
|
|
382
|
+
static is(node) {
|
|
383
|
+
return node.nodeKind === RelationalSchemaNodeKind.column;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Compares the column's own attributes only — the default lives on the
|
|
387
|
+
* child node. When both sides carry `resolvedNativeType`, the resolved
|
|
388
|
+
* value governs (array-ness rides on its `[]` suffix); otherwise raw
|
|
389
|
+
* fields compare.
|
|
390
|
+
*/
|
|
391
|
+
isEqualTo(other) {
|
|
392
|
+
const node = blindCast(other);
|
|
393
|
+
assertNode(node, "SqlColumnIR", SqlColumnIR.is);
|
|
394
|
+
if (this.resolvedNativeType !== void 0 && node.resolvedNativeType !== void 0) return this.resolvedNativeType === node.resolvedNativeType && this.nullable === node.nullable;
|
|
395
|
+
return this.nativeType === node.nativeType && this.nullable === node.nullable && Boolean(this.many) === Boolean(node.many);
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
//#endregion
|
|
399
|
+
//#region src/ir/sql-foreign-key-ir.ts
|
|
400
|
+
/**
|
|
401
|
+
* Schema IR node for a foreign-key constraint as observed by
|
|
402
|
+
* introspection. The `referencedTable` / `referencedColumns` field
|
|
403
|
+
* names match the introspection vocabulary (`pg_constraint.confkey`,
|
|
404
|
+
* etc.) and intentionally differ from the Contract IR's nested
|
|
405
|
+
* `references: { table, columns }` shape so that the verifier's
|
|
406
|
+
* structural comparison stays explicit about which side it's reading.
|
|
407
|
+
*
|
|
408
|
+
* Implements `DiffableNode` so a foreign key is directly a table's diff-tree
|
|
409
|
+
* child. Foreign keys are frequently unnamed (introspection may not carry a
|
|
410
|
+
* constraint name, and the contract side never invents one), so `id` is
|
|
411
|
+
* derived from the referencing/referenced coordinates rather than `name` —
|
|
412
|
+
* the same tuple that makes two FK constraints the same constraint. This
|
|
413
|
+
* also serves as the comparison key: two FKs with the same coordinates are
|
|
414
|
+
* paired by the differ, and `isEqualTo` then compares the remaining
|
|
415
|
+
* attribute — the referential actions.
|
|
416
|
+
*/
|
|
417
|
+
var SqlForeignKeyIR = class SqlForeignKeyIR extends SqlSchemaIRNode {
|
|
418
|
+
nodeKind = RelationalSchemaNodeKind.foreignKey;
|
|
419
|
+
columns;
|
|
420
|
+
referencedTable;
|
|
421
|
+
referencedColumns;
|
|
422
|
+
constructor(input) {
|
|
423
|
+
super();
|
|
424
|
+
this.columns = input.columns;
|
|
425
|
+
this.referencedTable = input.referencedTable;
|
|
426
|
+
this.referencedColumns = input.referencedColumns;
|
|
427
|
+
if (input.referencedSchema !== void 0) this.referencedSchema = input.referencedSchema;
|
|
428
|
+
if (input.name !== void 0) this.name = input.name;
|
|
429
|
+
if (input.onDelete !== void 0) this.onDelete = input.onDelete;
|
|
430
|
+
if (input.onUpdate !== void 0) this.onUpdate = input.onUpdate;
|
|
431
|
+
if (input.annotations !== void 0) this.annotations = input.annotations;
|
|
432
|
+
const resolvedReferencedNamespace = input.resolvedReferencedNamespace ?? input.referencedSchema;
|
|
433
|
+
if (resolvedReferencedNamespace !== void 0) this.resolvedReferencedNamespace = resolvedReferencedNamespace;
|
|
434
|
+
freezeNode(this);
|
|
435
|
+
}
|
|
436
|
+
get id() {
|
|
437
|
+
const referencedNamespace = this.resolvedReferencedNamespace ?? "";
|
|
438
|
+
return `foreign-key:${this.columns.join(",")}->${referencedNamespace}.${this.referencedTable}(${this.referencedColumns.join(",")})`;
|
|
439
|
+
}
|
|
440
|
+
children() {
|
|
441
|
+
return [];
|
|
442
|
+
}
|
|
443
|
+
static is(node) {
|
|
444
|
+
return node.nodeKind === RelationalSchemaNodeKind.foreignKey;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Referential-action comparison with `this` as the expected side, matching
|
|
448
|
+
* the relational walk's `getReferentialActionMismatches`: `noAction` is the
|
|
449
|
+
* database default and equivalent to an undeclared action, and drift is
|
|
450
|
+
* flagged only when the expected side declares a (normalized) action.
|
|
451
|
+
*/
|
|
452
|
+
isEqualTo(other) {
|
|
453
|
+
const node = blindCast(other);
|
|
454
|
+
assertNode(node, "SqlForeignKeyIR", SqlForeignKeyIR.is);
|
|
455
|
+
return referentialActionMatches(this.onDelete, node.onDelete) && referentialActionMatches(this.onUpdate, node.onUpdate);
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
function referentialActionMatches(expected, actual) {
|
|
459
|
+
const normalizedExpected = expected === "noAction" ? void 0 : expected;
|
|
460
|
+
if (normalizedExpected === void 0) return true;
|
|
461
|
+
return normalizedExpected === (actual === "noAction" ? void 0 : actual);
|
|
462
|
+
}
|
|
463
|
+
//#endregion
|
|
464
|
+
//#region src/ir/sql-index-ir.ts
|
|
465
|
+
/**
|
|
466
|
+
* Schema IR node for a secondary index as observed by introspection.
|
|
467
|
+
* Unlike the Contract IR `Index`, the Schema IR carries an explicit
|
|
468
|
+
* `unique` field — introspection sees the underlying index regardless
|
|
469
|
+
* of whether the user expressed it as `@@index` or `@@unique`, and the
|
|
470
|
+
* verifier needs to distinguish them when comparing to the Contract.
|
|
471
|
+
*
|
|
472
|
+
* Implements `DiffableNode` so an index is directly a table's diff-tree
|
|
473
|
+
* child. Indexes are frequently unnamed, so `id` is derived from the column
|
|
474
|
+
* tuple — the same tuple that makes two indexes the same index, so it
|
|
475
|
+
* doubles as the pairing key. `isEqualTo` is symmetric structural equality
|
|
476
|
+
* on the remaining attributes: `unique`, `type`, and `options`. A unique
|
|
477
|
+
* index and a non-unique index on the same columns are different objects
|
|
478
|
+
* and are not equal — there is no "stronger satisfies weaker".
|
|
479
|
+
*
|
|
480
|
+
* Deliberately column-tuple-only (not `unique`): a contract `@@index`
|
|
481
|
+
* (non-unique) must still pair against a live unique index of the same
|
|
482
|
+
* columns so the differ can report the mismatch as an incompatible index
|
|
483
|
+
* change rather than a spurious missing+extra pair — see
|
|
484
|
+
* `planner.unique-index-structural.test.ts`. The corollary is that two
|
|
485
|
+
* indexes legitimately coexisting on one table with the *same* column tuple
|
|
486
|
+
* (e.g. a unique index and a redundant plain index Postgres has no problem
|
|
487
|
+
* hosting side by side) collide on this id; the postgres control adapter's
|
|
488
|
+
* introspection keeps only one such index per table+column-tuple rather
|
|
489
|
+
* than handing the differ two same-tree siblings it cannot represent.
|
|
490
|
+
*/
|
|
491
|
+
var SqlIndexIR = class SqlIndexIR extends SqlSchemaIRNode {
|
|
492
|
+
nodeKind = RelationalSchemaNodeKind.index;
|
|
493
|
+
columns;
|
|
494
|
+
unique;
|
|
495
|
+
constructor(input) {
|
|
496
|
+
super();
|
|
497
|
+
this.columns = input.columns;
|
|
498
|
+
this.unique = input.unique;
|
|
499
|
+
if (input.name !== void 0) this.name = input.name;
|
|
500
|
+
if (input.type !== void 0) this.type = input.type;
|
|
501
|
+
if (input.options !== void 0) this.options = input.options;
|
|
502
|
+
if (input.annotations !== void 0) this.annotations = input.annotations;
|
|
503
|
+
freezeNode(this);
|
|
504
|
+
}
|
|
505
|
+
get id() {
|
|
506
|
+
return `index:${this.columns.join(",")}`;
|
|
507
|
+
}
|
|
508
|
+
children() {
|
|
509
|
+
return [];
|
|
510
|
+
}
|
|
511
|
+
static is(node) {
|
|
512
|
+
return node.nodeKind === RelationalSchemaNodeKind.index;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Symmetric structural equality: two paired index nodes are equal iff their
|
|
516
|
+
* `unique` flag, `type`, and (loosely-compared) `options` all match. There
|
|
517
|
+
* is no satisfaction — a unique index does not equal a non-unique index.
|
|
518
|
+
* `options` compares loosely (introspection stringifies reloptions); `type`
|
|
519
|
+
* compares strictly after the introspection-side btree→undefined
|
|
520
|
+
* normalization done at construction.
|
|
521
|
+
*/
|
|
522
|
+
isEqualTo(other) {
|
|
523
|
+
const node = blindCast(other);
|
|
524
|
+
assertNode(node, "SqlIndexIR", SqlIndexIR.is);
|
|
525
|
+
return this.unique === node.unique && this.type === node.type && indexOptionsLooselyEqual(this.options, node.options);
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
/**
|
|
529
|
+
* Option-bag equality ported from the relational walk: same key set, values
|
|
530
|
+
* compared via `String()` coercion — Postgres introspection returns
|
|
531
|
+
* reloptions values as raw strings (`'70'`, `'false'`) while contract option
|
|
532
|
+
* leaves are typed (number, boolean, string).
|
|
533
|
+
*/
|
|
534
|
+
function indexOptionsLooselyEqual(a, b) {
|
|
535
|
+
const aKeys = a ? Object.keys(a).sort() : [];
|
|
536
|
+
const bKeys = b ? Object.keys(b).sort() : [];
|
|
537
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
538
|
+
for (let i = 0; i < aKeys.length; i += 1) if (aKeys[i] !== bKeys[i]) return false;
|
|
539
|
+
if (aKeys.length === 0) return true;
|
|
540
|
+
for (const key of aKeys) if (String(a?.[key]) !== String(b?.[key])) return false;
|
|
541
|
+
return true;
|
|
542
|
+
}
|
|
543
|
+
//#endregion
|
|
544
|
+
//#region src/ir/sql-unique-ir.ts
|
|
545
|
+
/**
|
|
546
|
+
* Schema IR node for a table-level unique constraint as observed by
|
|
547
|
+
* introspection.
|
|
548
|
+
*
|
|
549
|
+
* Implements `DiffableNode` so a unique constraint is directly a table's
|
|
550
|
+
* diff-tree child. Unique constraints are frequently unnamed, so `id` is
|
|
551
|
+
* derived from the column tuple rather than `name` — the column tuple is
|
|
552
|
+
* also what makes two unique constraints the same constraint, so it doubles
|
|
553
|
+
* as the pairing key. There are no further attributes to compare once
|
|
554
|
+
* columns are equal (the differ pairs on `id`), so `isEqualTo` is identity.
|
|
555
|
+
*/
|
|
556
|
+
var SqlUniqueIR = class SqlUniqueIR extends SqlSchemaIRNode {
|
|
557
|
+
nodeKind = RelationalSchemaNodeKind.unique;
|
|
558
|
+
columns;
|
|
559
|
+
constructor(input) {
|
|
560
|
+
super();
|
|
561
|
+
this.columns = [...input.columns];
|
|
562
|
+
if (input.name !== void 0) this.name = input.name;
|
|
563
|
+
if (input.annotations !== void 0) this.annotations = input.annotations;
|
|
564
|
+
freezeNode(this);
|
|
565
|
+
}
|
|
566
|
+
get id() {
|
|
567
|
+
return `unique:${this.columns.join(",")}`;
|
|
568
|
+
}
|
|
569
|
+
children() {
|
|
570
|
+
return [];
|
|
571
|
+
}
|
|
572
|
+
static is(node) {
|
|
573
|
+
return node.nodeKind === RelationalSchemaNodeKind.unique;
|
|
574
|
+
}
|
|
575
|
+
isEqualTo(other) {
|
|
576
|
+
const node = blindCast(other);
|
|
577
|
+
assertNode(node, "SqlUniqueIR", SqlUniqueIR.is);
|
|
578
|
+
return this.id === node.id;
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
//#endregion
|
|
582
|
+
//#region src/ir/sql-table-ir.ts
|
|
583
|
+
/**
|
|
584
|
+
* Schema IR node for a single table as observed by introspection.
|
|
585
|
+
*
|
|
586
|
+
* Unlike the Contract IR `StorageTable`, this carries the table's
|
|
587
|
+
* `name` — introspection queries return tables as arrays and the
|
|
588
|
+
* verifier keys them into `SqlSchemaIR.tables` afterwards, so the name
|
|
589
|
+
* stays on the table object for downstream call sites that walk
|
|
590
|
+
* `Object.values(schema.tables)`.
|
|
591
|
+
*
|
|
592
|
+
* The constructor normalises nested IR-class fields so downstream
|
|
593
|
+
* walks see a uniform AST regardless of whether the input was a
|
|
594
|
+
* plain-data literal (from introspection) or already-constructed
|
|
595
|
+
* class instances.
|
|
596
|
+
*
|
|
597
|
+
* Implements `DiffableNode` so a flat (single-schema) tree is directly
|
|
598
|
+
* diffable: `id` is the table name; `isEqualTo` is identity (the table's
|
|
599
|
+
* structural drift is entirely expressed by its children); `children()`
|
|
600
|
+
* yields every column, the primary key (when present), every foreign key,
|
|
601
|
+
* unique, index, and check constraint — the same composition and order as
|
|
602
|
+
* the Postgres table node, minus policies.
|
|
603
|
+
*/
|
|
604
|
+
var SqlTableIR = class extends SqlSchemaIRNode {
|
|
605
|
+
nodeKind = RelationalSchemaNodeKind.table;
|
|
606
|
+
name;
|
|
607
|
+
columns;
|
|
608
|
+
foreignKeys;
|
|
609
|
+
uniques;
|
|
610
|
+
indexes;
|
|
611
|
+
constructor(input) {
|
|
612
|
+
super();
|
|
613
|
+
this.name = input.name;
|
|
614
|
+
this.columns = Object.freeze(Object.fromEntries(Object.entries(input.columns).map(([key, col]) => [key, col instanceof SqlColumnIR ? col : new SqlColumnIR(col)])));
|
|
615
|
+
this.foreignKeys = Object.freeze(input.foreignKeys.map((fk) => fk instanceof SqlForeignKeyIR ? fk : new SqlForeignKeyIR(fk)));
|
|
616
|
+
this.uniques = Object.freeze(input.uniques.map((u) => u instanceof SqlUniqueIR ? u : new SqlUniqueIR(u)));
|
|
617
|
+
this.indexes = Object.freeze(input.indexes.map((i) => i instanceof SqlIndexIR ? i : new SqlIndexIR(i)));
|
|
618
|
+
if (input.primaryKey !== void 0) this.primaryKey = input.primaryKey instanceof PrimaryKey ? input.primaryKey : new PrimaryKey(input.primaryKey);
|
|
619
|
+
if (input.annotations !== void 0) this.annotations = input.annotations;
|
|
620
|
+
if (input.checks !== void 0 && input.checks.length > 0) this.checks = Object.freeze(input.checks.map((c) => c instanceof SqlCheckConstraintIR ? c : new SqlCheckConstraintIR(c)));
|
|
621
|
+
freezeNode(this);
|
|
622
|
+
}
|
|
623
|
+
get id() {
|
|
624
|
+
return this.name;
|
|
625
|
+
}
|
|
626
|
+
isEqualTo(other) {
|
|
627
|
+
return this.id === other.id;
|
|
628
|
+
}
|
|
629
|
+
children() {
|
|
630
|
+
return [
|
|
631
|
+
...Object.values(this.columns),
|
|
632
|
+
...this.primaryKey ? [this.primaryKey] : [],
|
|
633
|
+
...this.foreignKeys,
|
|
634
|
+
...this.uniques,
|
|
635
|
+
...this.indexes,
|
|
636
|
+
...this.checks ?? []
|
|
637
|
+
];
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
//#endregion
|
|
641
|
+
//#region src/ir/sql-schema-ir.ts
|
|
642
|
+
/**
|
|
643
|
+
* Root Schema IR node representing the complete database schema as
|
|
644
|
+
* observed by introspection. Target-agnostic; used by both verifiers
|
|
645
|
+
* (compare against intended Contract storage) and migration planners
|
|
646
|
+
* (derive operations needed to reconcile).
|
|
647
|
+
*
|
|
648
|
+
* The constructor normalises nested `SqlTableIR` instances so
|
|
649
|
+
* downstream walks see a uniform AST regardless of whether the input
|
|
650
|
+
* was a plain-data literal or already-constructed class instances.
|
|
651
|
+
*
|
|
652
|
+
* Implements `DiffableNode` as the root of a flat (single-schema) diff
|
|
653
|
+
* tree: `id` is the fixed sentinel `'database'` (roots have no siblings),
|
|
654
|
+
* `isEqualTo` is identity (a container has no own attributes), and
|
|
655
|
+
* `children()` yields the table nodes.
|
|
656
|
+
*/
|
|
657
|
+
var SqlSchemaIR = class extends SqlSchemaIRNode {
|
|
658
|
+
nodeKind = RelationalSchemaNodeKind.schema;
|
|
659
|
+
tables;
|
|
660
|
+
constructor(input) {
|
|
661
|
+
super();
|
|
662
|
+
this.tables = Object.freeze(Object.fromEntries(Object.entries(input.tables).map(([key, t]) => [key, t instanceof SqlTableIR ? t : new SqlTableIR(t)])));
|
|
663
|
+
if (input.annotations !== void 0) this.annotations = input.annotations;
|
|
664
|
+
freezeNode(this);
|
|
665
|
+
}
|
|
666
|
+
get id() {
|
|
667
|
+
return "database";
|
|
668
|
+
}
|
|
669
|
+
isEqualTo(other) {
|
|
670
|
+
return this.id === other.id;
|
|
671
|
+
}
|
|
672
|
+
children() {
|
|
673
|
+
return Object.values(this.tables);
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
//#endregion
|
|
677
|
+
export { SqlForeignKeyIR as a, SqlCheckConstraintIR as c, assertNode as d, RelationalSchemaNodeKind as f, SqlIndexIR as i, PrimaryKey as l, relationalNodeGranularity as m, SqlTableIR as n, SqlColumnIR as o, relationalNodeEntityKind as p, SqlUniqueIR as r, SqlColumnDefaultIR as s, SqlSchemaIR as t, SqlSchemaIRNode as u };
|
|
678
|
+
|
|
679
|
+
//# sourceMappingURL=types-CZqCTmou.mjs.map
|