@prisma-next/sql-schema-ir 0.14.0-dev.49 → 0.14.0-dev.50
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-u-aICOCH.d.mts +565 -0
- package/dist/types-u-aICOCH.d.mts.map +1 -0
- package/dist/types-vvIwr6RV.mjs +664 -0
- package/dist/types-vvIwr6RV.mjs.map +1 -0
- package/package.json +7 -6
- 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 +136 -2
- package/src/ir/sql-foreign-key-ir.ts +71 -2
- package/src/ir/sql-index-ir.ts +69 -2
- package/src/ir/sql-schema-ir-node.ts +53 -11
- 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 +51 -2
- package/src/types.ts +10 -1
- package/dist/types-CJU1kcRM.d.mts +0 -273
- package/dist/types-CJU1kcRM.d.mts.map +0 -1
- package/dist/types-Drcy_LQx.mjs +0 -229
- package/dist/types-Drcy_LQx.mjs.map +0 -1
package/src/ir/sql-column-ir.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
import type { ColumnDefault } from '@prisma-next/contract/types';
|
|
2
|
+
import type { CodecRef } from '@prisma-next/framework-components/codec';
|
|
3
|
+
import type { DiffableNode } from '@prisma-next/framework-components/control';
|
|
1
4
|
import { freezeNode } from '@prisma-next/framework-components/ir';
|
|
2
|
-
import {
|
|
5
|
+
import { blindCast } from '@prisma-next/utils/casts';
|
|
6
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
7
|
+
import { RelationalSchemaNodeKind } from './schema-node-kinds';
|
|
8
|
+
import { SqlColumnDefaultIR } from './sql-column-default-ir';
|
|
9
|
+
import { assertNode, defineNonEnumerable, SqlSchemaIRNode } from './sql-schema-ir-node';
|
|
3
10
|
|
|
4
11
|
/**
|
|
5
12
|
* Namespaced annotations for extensibility. Each namespace
|
|
@@ -18,6 +25,54 @@ export interface SqlColumnIRInput {
|
|
|
18
25
|
readonly annotations?: SqlAnnotations;
|
|
19
26
|
/** True when the column is a native array (e.g. `text[]`, `int4[]`). The `nativeType` carries the element type only (e.g. `text`, `int4`). */
|
|
20
27
|
readonly many?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Fully resolved native type, comparable across the two diff sides:
|
|
30
|
+
* the contract-derived side stamps the codec-expanded type (typeRef
|
|
31
|
+
* resolved, parameterized types expanded, `[]` appended for arrays);
|
|
32
|
+
* the introspected side stamps the target-normalized type with the same
|
|
33
|
+
* `[]` convention. Stamped at construction by derivation/introspection;
|
|
34
|
+
* absent on raw hand-built nodes.
|
|
35
|
+
*/
|
|
36
|
+
readonly resolvedNativeType?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Structured default, comparable across the two diff sides: the
|
|
39
|
+
* contract-derived side stamps the contract's `ColumnDefault`; the
|
|
40
|
+
* introspected side stamps the target default-normalizer's parse of the
|
|
41
|
+
* raw expression. Absent when the column declares no default, or when
|
|
42
|
+
* the introspected raw default could not be parsed.
|
|
43
|
+
*/
|
|
44
|
+
readonly resolvedDefault?: ColumnDefault;
|
|
45
|
+
/**
|
|
46
|
+
* The column's resolved codec reference — the identity the migration
|
|
47
|
+
* planner's op-builders resolve DDL type rendering against at plan time
|
|
48
|
+
* (parameterized type expansion, e.g. `character` + `{ length: 36 }` →
|
|
49
|
+
* `character(36)`), calling the same codec hooks the pre-`plan(start,
|
|
50
|
+
* end)` op-path called. Carried the same way the query AST carries
|
|
51
|
+
* `CodecRef` (TML-2456) and the migration DDL renderer (TML-2918).
|
|
52
|
+
* Stamped on the EXPECTED (contract-derived) column at derivation,
|
|
53
|
+
* post-`typeRef` resolution (the referenced storage type's own
|
|
54
|
+
* codec/params, not the column's `typeRef` pointer). Absent on
|
|
55
|
+
* introspected/hand-built nodes — the actual side never renders DDL —
|
|
56
|
+
* and never compared by `isEqualTo`.
|
|
57
|
+
*/
|
|
58
|
+
readonly codecRef?: CodecRef;
|
|
59
|
+
/**
|
|
60
|
+
* The column's resolved BASE native type: pre-parameter-expansion,
|
|
61
|
+
* pre-array-suffix (e.g. `character`, not `character(36)` or
|
|
62
|
+
* `character[]`). Distinct from {@link resolvedNativeType} (the EXPANDED
|
|
63
|
+
* comparison value) — DDL rendering re-expands a parameterized type at
|
|
64
|
+
* plan time from this base, so it must not already carry the expansion.
|
|
65
|
+
* Stamped alongside {@link codecRef}.
|
|
66
|
+
*/
|
|
67
|
+
readonly codecBaseNativeType?: string;
|
|
68
|
+
/**
|
|
69
|
+
* True when the contract column declared its type via a named
|
|
70
|
+
* `storage.types` reference (`typeRef`) rather than inline fields — the
|
|
71
|
+
* migration planner quotes the base native type as an identifier in this
|
|
72
|
+
* case (e.g. a native enum's type name), matching the pre-`plan(start,
|
|
73
|
+
* end)` rendering exactly.
|
|
74
|
+
*/
|
|
75
|
+
readonly codecNamedType?: boolean;
|
|
21
76
|
}
|
|
22
77
|
|
|
23
78
|
/**
|
|
@@ -26,8 +81,19 @@ export interface SqlColumnIRInput {
|
|
|
26
81
|
* the column's `name` (Schema IR columns are returned as arrays from
|
|
27
82
|
* introspection queries; the parent table re-keys them into a record
|
|
28
83
|
* for downstream consumers).
|
|
84
|
+
*
|
|
85
|
+
* Implements `DiffableNode` so a column is directly a table's diff-tree
|
|
86
|
+
* child: `id` is the column name (unique among a table's columns); `isEqualTo`
|
|
87
|
+
* compares this column's own attributes only — never children, since a
|
|
88
|
+
* column is a leaf. When both sides carry `resolvedNativeType` (stamped at
|
|
89
|
+
* derivation/introspection), the comparison uses the resolved values —
|
|
90
|
+
* resolved native type, nullability, and structured default equality per
|
|
91
|
+
* the relational walk's `columnDefaultsEqual` semantics, with `this` as the
|
|
92
|
+
* expected side. Otherwise it falls back to comparing raw fields.
|
|
29
93
|
*/
|
|
30
|
-
export class SqlColumnIR extends SqlSchemaIRNode {
|
|
94
|
+
export class SqlColumnIR extends SqlSchemaIRNode implements DiffableNode {
|
|
95
|
+
override readonly nodeKind = RelationalSchemaNodeKind.column;
|
|
96
|
+
|
|
31
97
|
readonly name: string;
|
|
32
98
|
readonly nativeType: string;
|
|
33
99
|
readonly nullable: boolean;
|
|
@@ -35,6 +101,14 @@ export class SqlColumnIR extends SqlSchemaIRNode {
|
|
|
35
101
|
declare readonly annotations?: SqlAnnotations;
|
|
36
102
|
/** True when the column is a native array (e.g. `text[]`, `int4[]`). The `nativeType` carries the element type only (e.g. `text`, `int4`). */
|
|
37
103
|
declare readonly many?: boolean;
|
|
104
|
+
declare readonly resolvedNativeType?: string;
|
|
105
|
+
declare readonly resolvedDefault?: ColumnDefault;
|
|
106
|
+
/** See {@link SqlColumnIRInput.codecRef}. Non-enumerable so it stays out of JSON and structural equality. */
|
|
107
|
+
declare readonly codecRef?: CodecRef;
|
|
108
|
+
/** See {@link SqlColumnIRInput.codecBaseNativeType}. Non-enumerable, same reason as {@link codecRef}. */
|
|
109
|
+
declare readonly codecBaseNativeType?: string;
|
|
110
|
+
/** See {@link SqlColumnIRInput.codecNamedType}. Non-enumerable, same reason as {@link codecRef}. */
|
|
111
|
+
declare readonly codecNamedType?: boolean;
|
|
38
112
|
|
|
39
113
|
constructor(input: SqlColumnIRInput) {
|
|
40
114
|
super();
|
|
@@ -44,6 +118,66 @@ export class SqlColumnIR extends SqlSchemaIRNode {
|
|
|
44
118
|
if (input.default !== undefined) this.default = input.default;
|
|
45
119
|
if (input.annotations !== undefined) this.annotations = input.annotations;
|
|
46
120
|
if (input.many !== undefined) this.many = input.many;
|
|
121
|
+
if (input.resolvedNativeType !== undefined) this.resolvedNativeType = input.resolvedNativeType;
|
|
122
|
+
if (input.resolvedDefault !== undefined) this.resolvedDefault = input.resolvedDefault;
|
|
123
|
+
defineNonEnumerable(this, 'codecRef', input.codecRef);
|
|
124
|
+
defineNonEnumerable(this, 'codecBaseNativeType', input.codecBaseNativeType);
|
|
125
|
+
defineNonEnumerable(this, 'codecNamedType', input.codecNamedType);
|
|
47
126
|
freezeNode(this);
|
|
48
127
|
}
|
|
128
|
+
|
|
129
|
+
get id(): string {
|
|
130
|
+
return `column:${this.name}`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The column's default, when declared/present, is the column's one child
|
|
135
|
+
* node — it has an extra/missing/drift lifecycle of its own, so the differ
|
|
136
|
+
* recurses to it rather than `isEqualTo` comparing it. Built transiently
|
|
137
|
+
* from this column's own fields.
|
|
138
|
+
*/
|
|
139
|
+
children(): readonly DiffableNode[] {
|
|
140
|
+
if (this.resolvedDefault === undefined && this.default === undefined) {
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
return [
|
|
144
|
+
new SqlColumnDefaultIR({
|
|
145
|
+
...ifDefined('resolved', this.resolvedDefault),
|
|
146
|
+
...ifDefined('raw', this.default),
|
|
147
|
+
...ifDefined('nativeTypeContext', this.resolvedNativeType),
|
|
148
|
+
// `this.many` is unset on contract-derived columns (array-ness rides
|
|
149
|
+
// on the `nativeType` `[]` suffix there — `codecRef.many` carries
|
|
150
|
+
// it instead); introspected/hand-built columns set `this.many`
|
|
151
|
+
// directly. Either source works for the default node's array-
|
|
152
|
+
// literal rendering.
|
|
153
|
+
...ifDefined('many', this.many ?? this.codecRef?.many),
|
|
154
|
+
}),
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static is(node: SqlSchemaIRNode): node is SqlColumnIR {
|
|
159
|
+
return node.nodeKind === RelationalSchemaNodeKind.column;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Compares the column's own attributes only — the default lives on the
|
|
164
|
+
* child node. When both sides carry `resolvedNativeType`, the resolved
|
|
165
|
+
* value governs (array-ness rides on its `[]` suffix); otherwise raw
|
|
166
|
+
* fields compare.
|
|
167
|
+
*/
|
|
168
|
+
isEqualTo(other: DiffableNode): boolean {
|
|
169
|
+
const node = blindCast<
|
|
170
|
+
SqlSchemaIRNode,
|
|
171
|
+
'every diff-tree node the differ pairs is a SqlSchemaIRNode'
|
|
172
|
+
>(other);
|
|
173
|
+
assertNode(node, 'SqlColumnIR', SqlColumnIR.is);
|
|
174
|
+
if (this.resolvedNativeType !== undefined && node.resolvedNativeType !== undefined) {
|
|
175
|
+
return this.resolvedNativeType === node.resolvedNativeType && this.nullable === node.nullable;
|
|
176
|
+
}
|
|
177
|
+
return (
|
|
178
|
+
this.nativeType === node.nativeType &&
|
|
179
|
+
this.nullable === node.nullable &&
|
|
180
|
+
Boolean(this.many) === Boolean(node.many)
|
|
181
|
+
);
|
|
182
|
+
}
|
|
49
183
|
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import type { DiffableNode } from '@prisma-next/framework-components/control';
|
|
1
2
|
import { freezeNode } from '@prisma-next/framework-components/ir';
|
|
3
|
+
import { blindCast } from '@prisma-next/utils/casts';
|
|
4
|
+
import { RelationalSchemaNodeKind } from './schema-node-kinds';
|
|
2
5
|
import type { SqlAnnotations } from './sql-column-ir';
|
|
3
|
-
import { SqlSchemaIRNode } from './sql-schema-ir-node';
|
|
6
|
+
import { assertNode, SqlSchemaIRNode } from './sql-schema-ir-node';
|
|
4
7
|
|
|
5
8
|
export type SqlReferentialAction = 'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault';
|
|
6
9
|
|
|
@@ -14,6 +17,15 @@ export interface SqlForeignKeyIRInput {
|
|
|
14
17
|
readonly onDelete?: SqlReferentialAction;
|
|
15
18
|
readonly onUpdate?: SqlReferentialAction;
|
|
16
19
|
readonly annotations?: SqlAnnotations;
|
|
20
|
+
/**
|
|
21
|
+
* The real live DDL namespace of the referenced table, comparable across
|
|
22
|
+
* the two diff sides. Contract-derived trees stamp it explicitly (resolving
|
|
23
|
+
* namespace ids — including the unbound sentinel — to the DDL namespace);
|
|
24
|
+
* introspected FKs default it to `referencedSchema`, whose value already
|
|
25
|
+
* is the live namespace. Folded into `id` in place of the raw value so an
|
|
26
|
+
* unbound-namespace contract FK pairs with its introspected counterpart.
|
|
27
|
+
*/
|
|
28
|
+
readonly resolvedReferencedNamespace?: string;
|
|
17
29
|
}
|
|
18
30
|
|
|
19
31
|
/**
|
|
@@ -23,8 +35,19 @@ export interface SqlForeignKeyIRInput {
|
|
|
23
35
|
* etc.) and intentionally differ from the Contract IR's nested
|
|
24
36
|
* `references: { table, columns }` shape so that the verifier's
|
|
25
37
|
* structural comparison stays explicit about which side it's reading.
|
|
38
|
+
*
|
|
39
|
+
* Implements `DiffableNode` so a foreign key is directly a table's diff-tree
|
|
40
|
+
* child. Foreign keys are frequently unnamed (introspection may not carry a
|
|
41
|
+
* constraint name, and the contract side never invents one), so `id` is
|
|
42
|
+
* derived from the referencing/referenced coordinates rather than `name` —
|
|
43
|
+
* the same tuple that makes two FK constraints the same constraint. This
|
|
44
|
+
* also serves as the comparison key: two FKs with the same coordinates are
|
|
45
|
+
* paired by the differ, and `isEqualTo` then compares the remaining
|
|
46
|
+
* attribute — the referential actions.
|
|
26
47
|
*/
|
|
27
|
-
export class SqlForeignKeyIR extends SqlSchemaIRNode {
|
|
48
|
+
export class SqlForeignKeyIR extends SqlSchemaIRNode implements DiffableNode {
|
|
49
|
+
override readonly nodeKind = RelationalSchemaNodeKind.foreignKey;
|
|
50
|
+
|
|
28
51
|
readonly columns: readonly string[];
|
|
29
52
|
readonly referencedTable: string;
|
|
30
53
|
readonly referencedColumns: readonly string[];
|
|
@@ -33,6 +56,7 @@ export class SqlForeignKeyIR extends SqlSchemaIRNode {
|
|
|
33
56
|
declare readonly onDelete?: SqlReferentialAction;
|
|
34
57
|
declare readonly onUpdate?: SqlReferentialAction;
|
|
35
58
|
declare readonly annotations?: SqlAnnotations;
|
|
59
|
+
declare readonly resolvedReferencedNamespace?: string;
|
|
36
60
|
|
|
37
61
|
constructor(input: SqlForeignKeyIRInput) {
|
|
38
62
|
super();
|
|
@@ -44,6 +68,51 @@ export class SqlForeignKeyIR extends SqlSchemaIRNode {
|
|
|
44
68
|
if (input.onDelete !== undefined) this.onDelete = input.onDelete;
|
|
45
69
|
if (input.onUpdate !== undefined) this.onUpdate = input.onUpdate;
|
|
46
70
|
if (input.annotations !== undefined) this.annotations = input.annotations;
|
|
71
|
+
const resolvedReferencedNamespace = input.resolvedReferencedNamespace ?? input.referencedSchema;
|
|
72
|
+
if (resolvedReferencedNamespace !== undefined) {
|
|
73
|
+
this.resolvedReferencedNamespace = resolvedReferencedNamespace;
|
|
74
|
+
}
|
|
47
75
|
freezeNode(this);
|
|
48
76
|
}
|
|
77
|
+
|
|
78
|
+
get id(): string {
|
|
79
|
+
const referencedNamespace = this.resolvedReferencedNamespace ?? '';
|
|
80
|
+
return `foreign-key:${this.columns.join(',')}->${referencedNamespace}.${this.referencedTable}(${this.referencedColumns.join(',')})`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
children(): readonly DiffableNode[] {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static is(node: SqlSchemaIRNode): node is SqlForeignKeyIR {
|
|
88
|
+
return node.nodeKind === RelationalSchemaNodeKind.foreignKey;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Referential-action comparison with `this` as the expected side, matching
|
|
93
|
+
* the relational walk's `getReferentialActionMismatches`: `noAction` is the
|
|
94
|
+
* database default and equivalent to an undeclared action, and drift is
|
|
95
|
+
* flagged only when the expected side declares a (normalized) action.
|
|
96
|
+
*/
|
|
97
|
+
isEqualTo(other: DiffableNode): boolean {
|
|
98
|
+
const node = blindCast<
|
|
99
|
+
SqlSchemaIRNode,
|
|
100
|
+
'every diff-tree node the differ pairs is a SqlSchemaIRNode'
|
|
101
|
+
>(other);
|
|
102
|
+
assertNode(node, 'SqlForeignKeyIR', SqlForeignKeyIR.is);
|
|
103
|
+
return (
|
|
104
|
+
referentialActionMatches(this.onDelete, node.onDelete) &&
|
|
105
|
+
referentialActionMatches(this.onUpdate, node.onUpdate)
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function referentialActionMatches(
|
|
111
|
+
expected: SqlReferentialAction | undefined,
|
|
112
|
+
actual: SqlReferentialAction | undefined,
|
|
113
|
+
): boolean {
|
|
114
|
+
const normalizedExpected = expected === 'noAction' ? undefined : expected;
|
|
115
|
+
if (normalizedExpected === undefined) return true;
|
|
116
|
+
const normalizedActual = actual === 'noAction' ? undefined : actual;
|
|
117
|
+
return normalizedExpected === normalizedActual;
|
|
49
118
|
}
|
package/src/ir/sql-index-ir.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import type { DiffableNode } from '@prisma-next/framework-components/control';
|
|
1
2
|
import { freezeNode } from '@prisma-next/framework-components/ir';
|
|
3
|
+
import { blindCast } from '@prisma-next/utils/casts';
|
|
4
|
+
import { RelationalSchemaNodeKind } from './schema-node-kinds';
|
|
2
5
|
import type { SqlAnnotations } from './sql-column-ir';
|
|
3
|
-
import { SqlSchemaIRNode } from './sql-schema-ir-node';
|
|
6
|
+
import { assertNode, SqlSchemaIRNode } from './sql-schema-ir-node';
|
|
4
7
|
|
|
5
8
|
export interface SqlIndexIRInput {
|
|
6
9
|
readonly columns: readonly string[];
|
|
@@ -17,8 +20,16 @@ export interface SqlIndexIRInput {
|
|
|
17
20
|
* `unique` field — introspection sees the underlying index regardless
|
|
18
21
|
* of whether the user expressed it as `@@index` or `@@unique`, and the
|
|
19
22
|
* verifier needs to distinguish them when comparing to the Contract.
|
|
23
|
+
*
|
|
24
|
+
* Implements `DiffableNode` so an index is directly a table's diff-tree
|
|
25
|
+
* child. Indexes are frequently unnamed, so `id` is derived from the column
|
|
26
|
+
* tuple — the same tuple that makes two indexes the same index, so it
|
|
27
|
+
* doubles as the pairing key. `isEqualTo` compares the remaining attributes:
|
|
28
|
+
* `unique`, `type`, and `options`.
|
|
20
29
|
*/
|
|
21
|
-
export class SqlIndexIR extends SqlSchemaIRNode {
|
|
30
|
+
export class SqlIndexIR extends SqlSchemaIRNode implements DiffableNode {
|
|
31
|
+
override readonly nodeKind = RelationalSchemaNodeKind.index;
|
|
32
|
+
|
|
22
33
|
readonly columns: readonly string[];
|
|
23
34
|
readonly unique: boolean;
|
|
24
35
|
declare readonly name?: string;
|
|
@@ -36,4 +47,60 @@ export class SqlIndexIR extends SqlSchemaIRNode {
|
|
|
36
47
|
if (input.annotations !== undefined) this.annotations = input.annotations;
|
|
37
48
|
freezeNode(this);
|
|
38
49
|
}
|
|
50
|
+
|
|
51
|
+
get id(): string {
|
|
52
|
+
return `index:${this.columns.join(',')}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
children(): readonly DiffableNode[] {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static is(node: SqlSchemaIRNode): node is SqlIndexIR {
|
|
60
|
+
return node.nodeKind === RelationalSchemaNodeKind.index;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Comparison with `this` as the expected side, matching the relational
|
|
65
|
+
* walk's index satisfaction: a unique actual index satisfies a non-unique
|
|
66
|
+
* expected index (stronger satisfies weaker), while an expected unique
|
|
67
|
+
* index requires a unique actual. Type and options compare as attributes.
|
|
68
|
+
*/
|
|
69
|
+
isEqualTo(other: DiffableNode): boolean {
|
|
70
|
+
const node = blindCast<
|
|
71
|
+
SqlSchemaIRNode,
|
|
72
|
+
'every diff-tree node the differ pairs is a SqlSchemaIRNode'
|
|
73
|
+
>(other);
|
|
74
|
+
assertNode(node, 'SqlIndexIR', SqlIndexIR.is);
|
|
75
|
+
return (
|
|
76
|
+
(!this.unique || node.unique) &&
|
|
77
|
+
this.type === node.type &&
|
|
78
|
+
indexOptionsLooselyEqual(this.options, node.options)
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Option-bag equality ported from the relational walk: same key set, values
|
|
85
|
+
* compared via `String()` coercion — Postgres introspection returns
|
|
86
|
+
* reloptions values as raw strings (`'70'`, `'false'`) while contract option
|
|
87
|
+
* leaves are typed (number, boolean, string).
|
|
88
|
+
*/
|
|
89
|
+
function indexOptionsLooselyEqual(
|
|
90
|
+
a: Record<string, unknown> | undefined,
|
|
91
|
+
b: Record<string, unknown> | undefined,
|
|
92
|
+
): boolean {
|
|
93
|
+
const aKeys = a ? Object.keys(a).sort() : [];
|
|
94
|
+
const bKeys = b ? Object.keys(b).sort() : [];
|
|
95
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
96
|
+
for (let i = 0; i < aKeys.length; i += 1) {
|
|
97
|
+
if (aKeys[i] !== bKeys[i]) return false;
|
|
98
|
+
}
|
|
99
|
+
if (aKeys.length === 0) return true;
|
|
100
|
+
for (const key of aKeys) {
|
|
101
|
+
if (String(a?.[key]) !== String(b?.[key])) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
39
106
|
}
|
|
@@ -7,29 +7,32 @@ import { IRNodeBase } from '@prisma-next/framework-components/ir';
|
|
|
7
7
|
*
|
|
8
8
|
* SQL Schema IR represents the actual database state as discovered by
|
|
9
9
|
* introspection (the parallel to SQL Contract IR, which represents the
|
|
10
|
-
* desired state).
|
|
11
|
-
* polymorphic dispatch — verifiers and planners walk by structural
|
|
12
|
-
* position, not by inspecting `kind` — so a single family-level
|
|
13
|
-
* discriminator is sufficient. Future per-leaf overrides land cleanly
|
|
14
|
-
* the same way as on the Contract side.
|
|
10
|
+
* desired state).
|
|
15
11
|
*
|
|
16
12
|
* The discriminator is installed as a non-enumerable own property,
|
|
17
13
|
* matching the SqlNode pattern. This keeps `JSON.stringify(node)`
|
|
18
14
|
* canonical (no `kind` field), keeps `toEqual({...})` test assertions
|
|
19
15
|
* against pre-lift flat shapes passing, and keeps `node.kind` readable
|
|
20
|
-
* for
|
|
16
|
+
* for dispatch.
|
|
17
|
+
*
|
|
18
|
+
* Both `kind` and `nodeKind` are required: every concrete leaf is a node
|
|
19
|
+
* the generic differ can pair and compare, so every leaf must declare which
|
|
20
|
+
* node it is. `nodeKind` has no default here — every direct subclass sets its
|
|
21
|
+
* own literal value (the relational leaves via `RelationalSchemaNodeKind`,
|
|
22
|
+
* target concretions via their own vocabulary, e.g. `PostgresSchemaNodeKind`).
|
|
21
23
|
*/
|
|
22
24
|
export abstract class SqlSchemaIRNode extends IRNodeBase {
|
|
23
|
-
readonly kind
|
|
25
|
+
declare readonly kind: string;
|
|
24
26
|
|
|
25
27
|
/**
|
|
26
|
-
* Enumerable discriminant identifying which node this is (
|
|
27
|
-
*
|
|
28
|
-
*
|
|
28
|
+
* Enumerable discriminant identifying which node this is (column / primary
|
|
29
|
+
* key / foreign key / unique / index / check / database / namespace /
|
|
30
|
+
* table / policy / role / …). Concretions set a unique value; the
|
|
31
|
+
* `.is`/`.assert` guards compare against it. Unlike `kind`, it is
|
|
29
32
|
* enumerable, so it survives a spread that flattens a node into a plain
|
|
30
33
|
* object.
|
|
31
34
|
*/
|
|
32
|
-
readonly nodeKind
|
|
35
|
+
abstract readonly nodeKind: string;
|
|
33
36
|
|
|
34
37
|
constructor() {
|
|
35
38
|
super();
|
|
@@ -41,3 +44,42 @@ export abstract class SqlSchemaIRNode extends IRNodeBase {
|
|
|
41
44
|
});
|
|
42
45
|
}
|
|
43
46
|
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Asserts `node` matches `predicate`, narrowing its type to `T`. The one
|
|
50
|
+
* shared implementation every node class's `static assert` and `isEqualTo`
|
|
51
|
+
* reach for, instead of each hand-writing its own throw: the message names
|
|
52
|
+
* the class the caller expected.
|
|
53
|
+
*/
|
|
54
|
+
export function assertNode<T extends SqlSchemaIRNode>(
|
|
55
|
+
node: SqlSchemaIRNode | undefined,
|
|
56
|
+
className: string,
|
|
57
|
+
predicate: (node: SqlSchemaIRNode) => node is T,
|
|
58
|
+
): asserts node is T {
|
|
59
|
+
if (node === undefined || !predicate(node)) {
|
|
60
|
+
throw new Error(`Expected a ${className} but got nodeKind=${node?.nodeKind ?? 'undefined'}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Defines a non-enumerable own property, the same treatment `kind` gets
|
|
66
|
+
* above: a derivation-time render-support field stays out of
|
|
67
|
+
* `JSON.stringify`, `toEqual({...})` structural assertions, and spreads,
|
|
68
|
+
* while remaining directly readable (`node.field`) for the one consumer
|
|
69
|
+
* that resolves it at plan time. A no-op when `value` is `undefined` — the
|
|
70
|
+
* property is simply absent, matching every other optional field on these
|
|
71
|
+
* nodes.
|
|
72
|
+
*/
|
|
73
|
+
export function defineNonEnumerable<T extends object>(
|
|
74
|
+
target: T,
|
|
75
|
+
key: string,
|
|
76
|
+
value: unknown,
|
|
77
|
+
): void {
|
|
78
|
+
if (value === undefined) return;
|
|
79
|
+
Object.defineProperty(target, key, {
|
|
80
|
+
value,
|
|
81
|
+
enumerable: false,
|
|
82
|
+
writable: false,
|
|
83
|
+
configurable: false,
|
|
84
|
+
});
|
|
85
|
+
}
|
package/src/ir/sql-schema-ir.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { DiffableNode } from '@prisma-next/framework-components/control';
|
|
1
2
|
import { freezeNode } from '@prisma-next/framework-components/ir';
|
|
3
|
+
import { RelationalSchemaNodeKind } from './schema-node-kinds';
|
|
2
4
|
import type { SqlAnnotations } from './sql-column-ir';
|
|
3
5
|
import { SqlSchemaIRNode } from './sql-schema-ir-node';
|
|
4
6
|
import { SqlTableIR, type SqlTableIRInput } from './sql-table-ir';
|
|
@@ -17,8 +19,15 @@ export interface SqlSchemaIRInput {
|
|
|
17
19
|
* The constructor normalises nested `SqlTableIR` instances so
|
|
18
20
|
* downstream walks see a uniform AST regardless of whether the input
|
|
19
21
|
* was a plain-data literal or already-constructed class instances.
|
|
22
|
+
*
|
|
23
|
+
* Implements `DiffableNode` as the root of a flat (single-schema) diff
|
|
24
|
+
* tree: `id` is the fixed sentinel `'database'` (roots have no siblings),
|
|
25
|
+
* `isEqualTo` is identity (a container has no own attributes), and
|
|
26
|
+
* `children()` yields the table nodes.
|
|
20
27
|
*/
|
|
21
|
-
export class SqlSchemaIR extends SqlSchemaIRNode {
|
|
28
|
+
export class SqlSchemaIR extends SqlSchemaIRNode implements DiffableNode {
|
|
29
|
+
override readonly nodeKind = RelationalSchemaNodeKind.schema;
|
|
30
|
+
|
|
22
31
|
readonly tables: Readonly<Record<string, SqlTableIR>>;
|
|
23
32
|
declare readonly annotations?: SqlAnnotations;
|
|
24
33
|
|
|
@@ -35,4 +44,16 @@ export class SqlSchemaIR extends SqlSchemaIRNode {
|
|
|
35
44
|
if (input.annotations !== undefined) this.annotations = input.annotations;
|
|
36
45
|
freezeNode(this);
|
|
37
46
|
}
|
|
47
|
+
|
|
48
|
+
get id(): string {
|
|
49
|
+
return 'database';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
isEqualTo(other: DiffableNode): boolean {
|
|
53
|
+
return this.id === other.id;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
children(): readonly DiffableNode[] {
|
|
57
|
+
return Object.values(this.tables);
|
|
58
|
+
}
|
|
38
59
|
}
|
package/src/ir/sql-table-ir.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import type { DiffableNode } from '@prisma-next/framework-components/control';
|
|
1
2
|
import { freezeNode } from '@prisma-next/framework-components/ir';
|
|
2
3
|
import { PrimaryKey, type PrimaryKeyInput } from './primary-key';
|
|
4
|
+
import { RelationalSchemaNodeKind } from './schema-node-kinds';
|
|
3
5
|
import { SqlCheckConstraintIR, type SqlCheckConstraintIRInput } from './sql-check-constraint-ir';
|
|
4
6
|
import { type SqlAnnotations, SqlColumnIR, type SqlColumnIRInput } from './sql-column-ir';
|
|
5
7
|
import { SqlForeignKeyIR, type SqlForeignKeyIRInput } from './sql-foreign-key-ir';
|
|
@@ -32,8 +34,17 @@ export interface SqlTableIRInput {
|
|
|
32
34
|
* walks see a uniform AST regardless of whether the input was a
|
|
33
35
|
* plain-data literal (from introspection) or already-constructed
|
|
34
36
|
* class instances.
|
|
37
|
+
*
|
|
38
|
+
* Implements `DiffableNode` so a flat (single-schema) tree is directly
|
|
39
|
+
* diffable: `id` is the table name; `isEqualTo` is identity (the table's
|
|
40
|
+
* structural drift is entirely expressed by its children); `children()`
|
|
41
|
+
* yields every column, the primary key (when present), every foreign key,
|
|
42
|
+
* unique, index, and check constraint — the same composition and order as
|
|
43
|
+
* the Postgres table node, minus policies.
|
|
35
44
|
*/
|
|
36
|
-
export class SqlTableIR extends SqlSchemaIRNode {
|
|
45
|
+
export class SqlTableIR extends SqlSchemaIRNode implements DiffableNode {
|
|
46
|
+
override readonly nodeKind = RelationalSchemaNodeKind.table;
|
|
47
|
+
|
|
37
48
|
readonly name: string;
|
|
38
49
|
readonly columns: Readonly<Record<string, SqlColumnIR>>;
|
|
39
50
|
readonly foreignKeys: ReadonlyArray<SqlForeignKeyIR>;
|
|
@@ -79,4 +90,23 @@ export class SqlTableIR extends SqlSchemaIRNode {
|
|
|
79
90
|
}
|
|
80
91
|
freezeNode(this);
|
|
81
92
|
}
|
|
93
|
+
|
|
94
|
+
get id(): string {
|
|
95
|
+
return this.name;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
isEqualTo(other: DiffableNode): boolean {
|
|
99
|
+
return this.id === other.id;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
children(): readonly DiffableNode[] {
|
|
103
|
+
return [
|
|
104
|
+
...Object.values(this.columns),
|
|
105
|
+
...(this.primaryKey ? [this.primaryKey] : []),
|
|
106
|
+
...this.foreignKeys,
|
|
107
|
+
...this.uniques,
|
|
108
|
+
...this.indexes,
|
|
109
|
+
...(this.checks ?? []),
|
|
110
|
+
];
|
|
111
|
+
}
|
|
82
112
|
}
|
package/src/ir/sql-unique-ir.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ⚠️ ACCEPTED-UNDER-PROTEST DEBT — DELETE IN SLICE 2.6 (`unify-unique-and-index-nodes`).
|
|
3
|
+
*
|
|
4
|
+
* A unique constraint IS a unique index. This slice modeled uniques and indexes
|
|
5
|
+
* as two separate node kinds (`SqlUniqueIR` vs `SqlIndexIR` — even though
|
|
6
|
+
* `SqlIndexIR` already carries `unique: boolean`), so the strict-by-kind differ
|
|
7
|
+
* cannot pair a contract unique with a live unique index. `SqlUniqueIR` is a
|
|
8
|
+
* strict subset of `SqlIndexIR` and should not be a separate node kind.
|
|
9
|
+
*
|
|
10
|
+
* The fix — delete `SqlUniqueIR`, model unique enforcement as
|
|
11
|
+
* `SqlIndexIR { unique: true }` on both derivation and introspection, and
|
|
12
|
+
* delete `diff-tree-normalization.ts` — is specified in
|
|
13
|
+
* `projects/postgres-rls/slices/unify-unique-and-index-nodes/spec.md` and
|
|
14
|
+
* scheduled as the very next slice. Do NOT extend this.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { DiffableNode } from '@prisma-next/framework-components/control';
|
|
1
18
|
import { freezeNode } from '@prisma-next/framework-components/ir';
|
|
19
|
+
import { blindCast } from '@prisma-next/utils/casts';
|
|
20
|
+
import { RelationalSchemaNodeKind } from './schema-node-kinds';
|
|
2
21
|
import type { SqlAnnotations } from './sql-column-ir';
|
|
3
|
-
import { SqlSchemaIRNode } from './sql-schema-ir-node';
|
|
22
|
+
import { assertNode, SqlSchemaIRNode } from './sql-schema-ir-node';
|
|
4
23
|
|
|
5
24
|
export interface SqlUniqueIRInput {
|
|
6
25
|
readonly columns: readonly string[];
|
|
@@ -11,8 +30,17 @@ export interface SqlUniqueIRInput {
|
|
|
11
30
|
/**
|
|
12
31
|
* Schema IR node for a table-level unique constraint as observed by
|
|
13
32
|
* introspection.
|
|
33
|
+
*
|
|
34
|
+
* Implements `DiffableNode` so a unique constraint is directly a table's
|
|
35
|
+
* diff-tree child. Unique constraints are frequently unnamed, so `id` is
|
|
36
|
+
* derived from the column tuple rather than `name` — the column tuple is
|
|
37
|
+
* also what makes two unique constraints the same constraint, so it doubles
|
|
38
|
+
* as the pairing key. There are no further attributes to compare once
|
|
39
|
+
* columns are equal (the differ pairs on `id`), so `isEqualTo` is identity.
|
|
14
40
|
*/
|
|
15
|
-
export class SqlUniqueIR extends SqlSchemaIRNode {
|
|
41
|
+
export class SqlUniqueIR extends SqlSchemaIRNode implements DiffableNode {
|
|
42
|
+
override readonly nodeKind = RelationalSchemaNodeKind.unique;
|
|
43
|
+
|
|
16
44
|
readonly columns: readonly string[];
|
|
17
45
|
declare readonly name?: string;
|
|
18
46
|
declare readonly annotations?: SqlAnnotations;
|
|
@@ -24,4 +52,25 @@ export class SqlUniqueIR extends SqlSchemaIRNode {
|
|
|
24
52
|
if (input.annotations !== undefined) this.annotations = input.annotations;
|
|
25
53
|
freezeNode(this);
|
|
26
54
|
}
|
|
55
|
+
|
|
56
|
+
get id(): string {
|
|
57
|
+
return `unique:${this.columns.join(',')}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
children(): readonly DiffableNode[] {
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static is(node: SqlSchemaIRNode): node is SqlUniqueIR {
|
|
65
|
+
return node.nodeKind === RelationalSchemaNodeKind.unique;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
isEqualTo(other: DiffableNode): boolean {
|
|
69
|
+
const node = blindCast<
|
|
70
|
+
SqlSchemaIRNode,
|
|
71
|
+
'every diff-tree node the differ pairs is a SqlSchemaIRNode'
|
|
72
|
+
>(other);
|
|
73
|
+
assertNode(node, 'SqlUniqueIR', SqlUniqueIR.is);
|
|
74
|
+
return this.id === node.id;
|
|
75
|
+
}
|
|
27
76
|
}
|
package/src/types.ts
CHANGED
|
@@ -10,10 +10,19 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
export { PrimaryKey, type PrimaryKeyInput } from './ir/primary-key';
|
|
13
|
+
export {
|
|
14
|
+
RelationalSchemaNodeKind,
|
|
15
|
+
relationalNodeEntityKind,
|
|
16
|
+
relationalNodeGranularity,
|
|
17
|
+
} from './ir/schema-node-kinds';
|
|
13
18
|
export {
|
|
14
19
|
SqlCheckConstraintIR,
|
|
15
20
|
type SqlCheckConstraintIRInput,
|
|
16
21
|
} from './ir/sql-check-constraint-ir';
|
|
22
|
+
export {
|
|
23
|
+
SqlColumnDefaultIR,
|
|
24
|
+
type SqlColumnDefaultIRInput,
|
|
25
|
+
} from './ir/sql-column-default-ir';
|
|
17
26
|
export {
|
|
18
27
|
type SqlAnnotations,
|
|
19
28
|
SqlColumnIR,
|
|
@@ -26,7 +35,7 @@ export {
|
|
|
26
35
|
} from './ir/sql-foreign-key-ir';
|
|
27
36
|
export { SqlIndexIR, type SqlIndexIRInput } from './ir/sql-index-ir';
|
|
28
37
|
export { SqlSchemaIR, type SqlSchemaIRInput } from './ir/sql-schema-ir';
|
|
29
|
-
export { SqlSchemaIRNode } from './ir/sql-schema-ir-node';
|
|
38
|
+
export { assertNode, SqlSchemaIRNode } from './ir/sql-schema-ir-node';
|
|
30
39
|
export { SqlTableIR, type SqlTableIRInput } from './ir/sql-table-ir';
|
|
31
40
|
export { SqlUniqueIR, type SqlUniqueIRInput } from './ir/sql-unique-ir';
|
|
32
41
|
|