@prisma-next/family-sql 0.12.0-dev.6 → 0.12.0-dev.60
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/{authoring-type-constructors-F4JpCJl7.mjs → authoring-type-constructors-D4lQ-qpj.mjs} +1 -1
- package/dist/{authoring-type-constructors-F4JpCJl7.mjs.map → authoring-type-constructors-D4lQ-qpj.mjs.map} +1 -1
- package/dist/control-adapter-76pzcIFV.d.mts +172 -0
- package/dist/control-adapter-76pzcIFV.d.mts.map +1 -0
- package/dist/control-adapter.d.mts +2 -109
- package/dist/control.d.mts +118 -4
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +305 -44
- package/dist/control.mjs.map +1 -1
- package/dist/ir.d.mts +4 -5
- package/dist/ir.d.mts.map +1 -1
- package/dist/ir.mjs +1 -1
- package/dist/migration.d.mts +1 -1
- package/dist/migration.d.mts.map +1 -1
- package/dist/pack.mjs +1 -1
- package/dist/runtime.d.mts +4 -2
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +4 -2
- package/dist/runtime.mjs.map +1 -1
- package/dist/schema-verify.d.mts +2 -1
- package/dist/schema-verify.d.mts.map +1 -1
- package/dist/schema-verify.mjs +1 -1
- package/dist/{sql-contract-serializer-8axtK4lg.mjs → sql-contract-serializer-CY7qnms7.mjs} +18 -36
- package/dist/sql-contract-serializer-CY7qnms7.mjs.map +1 -0
- package/dist/{timestamp-now-generator-BkjCQIde.mjs → timestamp-now-generator-CloimujU.mjs} +1 -1
- package/dist/{timestamp-now-generator-BkjCQIde.mjs.map → timestamp-now-generator-CloimujU.mjs.map} +1 -1
- package/dist/{types-CeeCStqw.d.mts → types-CRpx_fk5.d.mts} +69 -15
- package/dist/types-CRpx_fk5.d.mts.map +1 -0
- package/dist/{verify-Crewz6hG.mjs → verify-C-G0obRm.mjs} +1 -1
- package/dist/{verify-Crewz6hG.mjs.map → verify-C-G0obRm.mjs.map} +1 -1
- package/dist/{verify-sql-schema-CN7pPoTC.d.mts → verify-sql-schema-DcMaT5Zj.d.mts} +1 -1
- package/dist/{verify-sql-schema-CN7pPoTC.d.mts.map → verify-sql-schema-DcMaT5Zj.d.mts.map} +1 -1
- package/dist/{verify-sql-schema-CYLsGCFO.mjs → verify-sql-schema-d3P9NA_8.mjs} +400 -317
- package/dist/verify-sql-schema-d3P9NA_8.mjs.map +1 -0
- package/dist/verify.mjs +1 -1
- package/package.json +23 -23
- package/src/core/control-adapter.ts +105 -7
- package/src/core/control-instance.ts +260 -66
- package/src/core/default-namespace.ts +9 -0
- package/src/core/ir/sql-contract-serializer-base.ts +72 -56
- package/src/core/migrations/contract-to-schema-ir.ts +13 -9
- package/src/core/migrations/control-policy.ts +322 -0
- package/src/core/migrations/field-event-planner.ts +2 -2
- package/src/core/migrations/plan-helpers.ts +16 -0
- package/src/core/migrations/types.ts +16 -6
- package/src/core/schema-verify/control-verify-emit.ts +46 -0
- package/src/core/schema-verify/verifier-disposition.ts +53 -0
- package/src/core/schema-verify/verify-helpers.ts +151 -110
- package/src/core/schema-verify/verify-sql-schema.ts +281 -178
- package/src/exports/control.ts +6 -0
- package/src/exports/runtime.ts +7 -0
- package/dist/control-adapter.d.mts.map +0 -1
- package/dist/sql-contract-serializer-8axtK4lg.mjs.map +0 -1
- package/dist/types-CeeCStqw.d.mts.map +0 -1
- package/dist/verify-sql-schema-CYLsGCFO.mjs.map +0 -1
|
@@ -24,6 +24,7 @@ import type {
|
|
|
24
24
|
SqlTableIR,
|
|
25
25
|
SqlUniqueIR,
|
|
26
26
|
} from '@prisma-next/sql-schema-ir/types';
|
|
27
|
+
import { blindCast } from '@prisma-next/utils/casts';
|
|
27
28
|
import { ifDefined } from '@prisma-next/utils/defined';
|
|
28
29
|
|
|
29
30
|
/**
|
|
@@ -218,7 +219,7 @@ function convertTable(
|
|
|
218
219
|
name,
|
|
219
220
|
columns,
|
|
220
221
|
...ifDefined('primaryKey', table.primaryKey),
|
|
221
|
-
foreignKeys: table.foreignKeys.map(convertForeignKey),
|
|
222
|
+
foreignKeys: table.foreignKeys.filter((fk) => fk.constraint !== false).map(convertForeignKey),
|
|
222
223
|
uniques: table.uniques.map(convertUnique),
|
|
223
224
|
indexes: [...table.indexes.map(convertIndex), ...fkBackingIndexes],
|
|
224
225
|
};
|
|
@@ -250,11 +251,11 @@ export function detectDestructiveChanges(
|
|
|
250
251
|
for (const namespaceId of namespaceIds) {
|
|
251
252
|
const fromNs = from.namespaces[namespaceId];
|
|
252
253
|
const toNs = to.namespaces[namespaceId];
|
|
253
|
-
const fromTables = fromNs?.
|
|
254
|
+
const fromTables = fromNs?.entries.table;
|
|
254
255
|
if (!fromTables) continue;
|
|
255
256
|
|
|
256
257
|
for (const tableName of Object.keys(fromTables)) {
|
|
257
|
-
const toTableRaw = toNs?.
|
|
258
|
+
const toTableRaw = toNs?.entries.table[tableName];
|
|
258
259
|
if (!(toTableRaw instanceof StorageTable)) {
|
|
259
260
|
conflicts.push({
|
|
260
261
|
kind: 'tableRemoved',
|
|
@@ -327,20 +328,23 @@ export function contractToSchemaIR(
|
|
|
327
328
|
...((storage.types ?? {}) as ResolvedStorageTypes),
|
|
328
329
|
};
|
|
329
330
|
for (const ns of Object.values(storage.namespaces)) {
|
|
330
|
-
const nsEnums =
|
|
331
|
+
const nsEnums = ns.entries['type'];
|
|
331
332
|
if (nsEnums) {
|
|
332
333
|
for (const [k, v] of Object.entries(nsEnums)) {
|
|
333
|
-
allTypes[k] =
|
|
334
|
+
allTypes[k] = blindCast<
|
|
335
|
+
PostgresEnumStorageEntry | StorageTypeInstance,
|
|
336
|
+
'entries.type holds postgres-specific enum entries at runtime'
|
|
337
|
+
>(v);
|
|
334
338
|
}
|
|
335
339
|
}
|
|
336
340
|
}
|
|
337
341
|
const storageTypes = allTypes as ResolvedStorageTypes;
|
|
338
342
|
const tables: Record<string, SqlTableIR> = {};
|
|
339
343
|
for (const ns of Object.values(storage.namespaces)) {
|
|
340
|
-
for (const [tableName, tableDefRaw] of Object.entries(ns.
|
|
344
|
+
for (const [tableName, tableDefRaw] of Object.entries(ns.entries.table)) {
|
|
341
345
|
if (!(tableDefRaw instanceof StorageTable)) {
|
|
342
346
|
throw new Error(
|
|
343
|
-
`contractToSchemaIR: expected StorageTable at namespaces.${ns.id}.
|
|
347
|
+
`contractToSchemaIR: expected StorageTable at namespaces.${ns.id}.entries.table.${tableName}`,
|
|
344
348
|
);
|
|
345
349
|
}
|
|
346
350
|
const tableDef = tableDefRaw;
|
|
@@ -395,7 +399,7 @@ function deriveAnnotations(
|
|
|
395
399
|
|
|
396
400
|
// Top-level `storage.types`: codec-typed entries (vector, decimal, …) keyed
|
|
397
401
|
// by bare `nativeType` (unchanged). Post-S1.B enums live in
|
|
398
|
-
// `namespaces[*].
|
|
402
|
+
// `namespaces[*].entries.type`, not here; a defensive top-level enum is still
|
|
399
403
|
// namespace/schema-qualified via the resolver under the unbound coordinate
|
|
400
404
|
// so it never collides on a bare name.
|
|
401
405
|
for (const typeInstance of Object.values((storage.types ?? {}) as ResolvedStorageTypes)) {
|
|
@@ -415,7 +419,7 @@ function deriveAnnotations(
|
|
|
415
419
|
// `readExistingEnumValues` read side, so two namespaces sharing an enum name
|
|
416
420
|
// (or native type) resolve to distinct live-database types.
|
|
417
421
|
for (const [namespaceId, ns] of Object.entries(storage.namespaces)) {
|
|
418
|
-
const nsEnums =
|
|
422
|
+
const nsEnums = ns.entries['type'];
|
|
419
423
|
if (!nsEnums) continue;
|
|
420
424
|
for (const entry of Object.values(nsEnums)) {
|
|
421
425
|
if (!isPostgresEnumStorageEntry(entry)) continue;
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Contract,
|
|
3
|
+
type ControlPolicy,
|
|
4
|
+
effectiveControlPolicy,
|
|
5
|
+
} from '@prisma-next/contract/types';
|
|
6
|
+
import type { SqlStorage } from '@prisma-next/sql-contract/types';
|
|
7
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
8
|
+
import type { SqlPlannerConflict } from './types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The target object a control policy governs for a single planner call,
|
|
12
|
+
* resolved from the target's own IR. `undefined` means the call's target
|
|
13
|
+
* object could not be positively established — a fail-closed signal: any
|
|
14
|
+
* policy stricter than `managed` drops such a call rather than emitting it.
|
|
15
|
+
*/
|
|
16
|
+
export interface ControlPolicySubject {
|
|
17
|
+
readonly namespaceId: string;
|
|
18
|
+
readonly explicitNodeControlPolicy?: ControlPolicy;
|
|
19
|
+
readonly table?: string;
|
|
20
|
+
readonly column?: string;
|
|
21
|
+
readonly typeName?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Whether the call creates a whole, previously-absent top-level storage
|
|
24
|
+
* object (e.g. a table or an enum/type), as opposed to modifying an
|
|
25
|
+
* existing object. This is the only thing `tolerated` permits: it is a
|
|
26
|
+
* create-if-absent policy, so an op that touches an existing object — add
|
|
27
|
+
* column, add index/constraint, alter, drop — is never allowed under it.
|
|
28
|
+
*/
|
|
29
|
+
readonly createsNewObject: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The control policy that governs a single call. The `external` default is an
|
|
34
|
+
* un-overridable namespace floor: when the contract default is `external`, no
|
|
35
|
+
* per-object `managed` override can escalate DDL above the floor, so the
|
|
36
|
+
* policy is forced to `external` regardless of the node's own declaration.
|
|
37
|
+
* Every other default defers to the node's effective control policy.
|
|
38
|
+
*/
|
|
39
|
+
export function controlPolicyForCall(
|
|
40
|
+
subject: ControlPolicySubject | undefined,
|
|
41
|
+
defaultControlPolicy: ControlPolicy | undefined,
|
|
42
|
+
): ControlPolicy {
|
|
43
|
+
if (defaultControlPolicy === 'external') {
|
|
44
|
+
return 'external';
|
|
45
|
+
}
|
|
46
|
+
return effectiveControlPolicy(subject?.explicitNodeControlPolicy, defaultControlPolicy);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Whether a call is allowed to emit under a given control policy.
|
|
51
|
+
*
|
|
52
|
+
* - `managed` — full lifecycle, every op allowed.
|
|
53
|
+
* - `tolerated` — create-if-absent only: allowed iff the call creates a whole
|
|
54
|
+
* new top-level object (and its subject was positively resolved). Anything
|
|
55
|
+
* that modifies an existing object, and anything whose subject could not be
|
|
56
|
+
* resolved, is suppressed.
|
|
57
|
+
* - `external` / `observed` — no DDL at all.
|
|
58
|
+
*/
|
|
59
|
+
function callAllowedUnderControlPolicy(
|
|
60
|
+
policy: ControlPolicy,
|
|
61
|
+
subject: ControlPolicySubject | undefined,
|
|
62
|
+
): boolean {
|
|
63
|
+
switch (policy) {
|
|
64
|
+
case 'managed':
|
|
65
|
+
return true;
|
|
66
|
+
case 'tolerated':
|
|
67
|
+
return subject?.createsNewObject === true;
|
|
68
|
+
case 'external':
|
|
69
|
+
case 'observed':
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function defaultSubjectLabel(
|
|
75
|
+
factoryName: string,
|
|
76
|
+
subject: ControlPolicySubject | undefined,
|
|
77
|
+
): string {
|
|
78
|
+
if (subject?.table) {
|
|
79
|
+
return `${factoryName}(${subject.table})`;
|
|
80
|
+
}
|
|
81
|
+
if (subject?.typeName) {
|
|
82
|
+
return `${factoryName}(${subject.typeName})`;
|
|
83
|
+
}
|
|
84
|
+
return factoryName;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function suppressionSummary(
|
|
88
|
+
subjectLabel: string,
|
|
89
|
+
subject: ControlPolicySubject | undefined,
|
|
90
|
+
effectivePolicy: ControlPolicy,
|
|
91
|
+
): string {
|
|
92
|
+
const namespace = subject?.namespaceId ?? 'unknown';
|
|
93
|
+
const declared = subject?.explicitNodeControlPolicy;
|
|
94
|
+
if (effectivePolicy === 'external' && declared === 'managed') {
|
|
95
|
+
return `control policy suppressed: ${subjectLabel} — namespace '${namespace}' has effective control 'external' but table declared 'managed'`;
|
|
96
|
+
}
|
|
97
|
+
const declaredSuffix = declared ? ` but table declared '${declared}'` : '';
|
|
98
|
+
return `control policy suppressed: ${subjectLabel} — namespace '${namespace}' has effective control '${effectivePolicy}'${declaredSuffix}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function buildSubjectSuppressionWarning(
|
|
102
|
+
subject: ControlPolicySubject | undefined,
|
|
103
|
+
effectivePolicy: ControlPolicy,
|
|
104
|
+
factoryName: string,
|
|
105
|
+
formatSubjectLabel: (factoryName: string, subject: ControlPolicySubject | undefined) => string,
|
|
106
|
+
): SqlPlannerConflict {
|
|
107
|
+
const subjectLabel = formatSubjectLabel(factoryName, subject);
|
|
108
|
+
return {
|
|
109
|
+
kind: 'controlPolicySuppressedCall',
|
|
110
|
+
summary: suppressionSummary(subjectLabel, subject, effectivePolicy),
|
|
111
|
+
location: {
|
|
112
|
+
...ifDefined('namespace', subject?.namespaceId),
|
|
113
|
+
...ifDefined('table', subject?.table),
|
|
114
|
+
...ifDefined('column', subject?.column),
|
|
115
|
+
...ifDefined('type', subject?.typeName),
|
|
116
|
+
},
|
|
117
|
+
meta: {
|
|
118
|
+
controlPolicy: effectivePolicy,
|
|
119
|
+
factoryName,
|
|
120
|
+
...ifDefined('declaredControlPolicy', subject?.explicitNodeControlPolicy),
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function defaultModificationFactoryNameForSubject(subject: ControlPolicySubject): string {
|
|
126
|
+
if (subject.table) return 'alterTable';
|
|
127
|
+
if (subject.typeName) return 'alterType';
|
|
128
|
+
return 'alterSchema';
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Partition the calls produced for a single set of subjects into those the
|
|
133
|
+
* effective control policy permits (`kept`) and a list of
|
|
134
|
+
* {@link SqlPlannerConflict} warnings describing the suppressed calls.
|
|
135
|
+
*
|
|
136
|
+
* **Prefer {@link partitionIssuesByControlPolicy}** for the schema-issue
|
|
137
|
+
* pipeline: it filters subjects out of the planner's *input* so the planner
|
|
138
|
+
* never has to reason about un-modeled state on `external`/`observed`
|
|
139
|
+
* subjects. This call-level helper remains for paths that bypass the issue
|
|
140
|
+
* pipeline — currently the codec-emitted field-event ops, which originate
|
|
141
|
+
* from declared contract fields rather than from introspected schema state
|
|
142
|
+
* and therefore cannot trip the diff engine.
|
|
143
|
+
*/
|
|
144
|
+
export function partitionCallsByControlPolicy<TCall>(options: {
|
|
145
|
+
readonly calls: readonly TCall[];
|
|
146
|
+
readonly contract: Contract<SqlStorage>;
|
|
147
|
+
readonly resolveControlPolicySubject: (call: TCall) => ControlPolicySubject | undefined;
|
|
148
|
+
readonly resolveFactoryName: (call: TCall) => string;
|
|
149
|
+
readonly formatSubjectLabel?: (
|
|
150
|
+
factoryName: string,
|
|
151
|
+
subject: ControlPolicySubject | undefined,
|
|
152
|
+
) => string;
|
|
153
|
+
}): {
|
|
154
|
+
readonly kept: readonly TCall[];
|
|
155
|
+
readonly warnings: readonly SqlPlannerConflict[];
|
|
156
|
+
} {
|
|
157
|
+
const defaultControlPolicy = options.contract.defaultControlPolicy;
|
|
158
|
+
const formatSubjectLabel = options.formatSubjectLabel ?? defaultSubjectLabel;
|
|
159
|
+
const kept: TCall[] = [];
|
|
160
|
+
const warnings: SqlPlannerConflict[] = [];
|
|
161
|
+
|
|
162
|
+
for (const call of options.calls) {
|
|
163
|
+
const subject = options.resolveControlPolicySubject(call);
|
|
164
|
+
const policy = controlPolicyForCall(subject, defaultControlPolicy);
|
|
165
|
+
if (callAllowedUnderControlPolicy(policy, subject)) {
|
|
166
|
+
kept.push(call);
|
|
167
|
+
} else {
|
|
168
|
+
const factoryName = options.resolveFactoryName(call);
|
|
169
|
+
warnings.push(
|
|
170
|
+
buildSubjectSuppressionWarning(subject, policy, factoryName, formatSubjectLabel),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return Object.freeze({
|
|
176
|
+
kept: Object.freeze(kept),
|
|
177
|
+
warnings: Object.freeze(warnings),
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Partition a list of schema-issue-shaped inputs by the effective control
|
|
183
|
+
* policy of each issue's subject *before* the planner is invoked.
|
|
184
|
+
*
|
|
185
|
+
* `plannable` is the list of issues whose subject's effective policy permits
|
|
186
|
+
* the planner to act on them (`managed`, or `tolerated` for whole-object
|
|
187
|
+
* creation issues only). Issues for `external`/`observed` subjects, and
|
|
188
|
+
* non-creation issues for `tolerated` subjects, are dropped from the planner's
|
|
189
|
+
* input entirely — they never enter introspection-driven planning, never feed
|
|
190
|
+
* the diff engine, and never produce DDL calls that would have to be
|
|
191
|
+
* post-filtered. This sidesteps a class of failure where the diff engine
|
|
192
|
+
* cannot reason about the live shape of a subject the user marked as
|
|
193
|
+
* out-of-scope (`external`).
|
|
194
|
+
*
|
|
195
|
+
* `warnings` is one {@link SqlPlannerConflict} per suppressed subject (not per
|
|
196
|
+
* suppressed issue). `factoryName` is inferred from the subject's issue mix:
|
|
197
|
+
* if any of the subject's issues is whole-object creation, the warning takes
|
|
198
|
+
* the corresponding creation factoryName (e.g. `createTable`,
|
|
199
|
+
* `createEnumType`, `createSchema`); otherwise it falls back to
|
|
200
|
+
* `defaultModificationFactoryName(subject)` — a synthetic label that names
|
|
201
|
+
* the *kind* of mutation that would have run, since no concrete DDL call was
|
|
202
|
+
* generated.
|
|
203
|
+
*
|
|
204
|
+
* Unresolved-subject issues (`resolveControlPolicySubject` returns
|
|
205
|
+
* `undefined`) emit one warning each; they cannot be deduplicated because
|
|
206
|
+
* they carry no subject coordinate.
|
|
207
|
+
*/
|
|
208
|
+
export function partitionIssuesByControlPolicy<TIssue>(options: {
|
|
209
|
+
readonly issues: readonly TIssue[];
|
|
210
|
+
readonly contract: Contract<SqlStorage>;
|
|
211
|
+
/**
|
|
212
|
+
* Resolve the subject targeted by this issue (or `undefined` to fail-closed:
|
|
213
|
+
* any policy stricter than `managed` drops the issue).
|
|
214
|
+
*/
|
|
215
|
+
readonly resolveControlPolicySubject: (issue: TIssue) => ControlPolicySubject | undefined;
|
|
216
|
+
/**
|
|
217
|
+
* Resolve a creation factoryName for this issue if it represents the
|
|
218
|
+
* absence of the whole top-level object (e.g. `'createTable'` for a
|
|
219
|
+
* missing-table issue). When the issue describes a modification to an
|
|
220
|
+
* existing object, return `undefined`. Both decisions feed off this signal:
|
|
221
|
+
*
|
|
222
|
+
* 1. Under `tolerated`, only issues whose `resolveCreationFactoryName`
|
|
223
|
+
* returns a value flow into the planner (create-if-absent).
|
|
224
|
+
* 2. Subjects that have at least one creation-flavoured issue use the
|
|
225
|
+
* resolved creation factoryName for their suppression warning;
|
|
226
|
+
* otherwise they fall back to `defaultModificationFactoryName`.
|
|
227
|
+
*/
|
|
228
|
+
readonly resolveCreationFactoryName: (issue: TIssue) => string | undefined;
|
|
229
|
+
/**
|
|
230
|
+
* Default modification factoryName for a suppressed subject whose issues
|
|
231
|
+
* are all non-creation (the subject exists but has a different shape).
|
|
232
|
+
* Defaults to `'alterTable'` / `'alterType'` / `'alterSchema'` based on the
|
|
233
|
+
* subject's populated coordinates.
|
|
234
|
+
*/
|
|
235
|
+
readonly defaultModificationFactoryName?: (subject: ControlPolicySubject) => string;
|
|
236
|
+
readonly formatSubjectLabel?: (
|
|
237
|
+
factoryName: string,
|
|
238
|
+
subject: ControlPolicySubject | undefined,
|
|
239
|
+
) => string;
|
|
240
|
+
}): {
|
|
241
|
+
readonly plannable: readonly TIssue[];
|
|
242
|
+
readonly warnings: readonly SqlPlannerConflict[];
|
|
243
|
+
} {
|
|
244
|
+
const defaultControlPolicy = options.contract.defaultControlPolicy;
|
|
245
|
+
const formatSubjectLabel = options.formatSubjectLabel ?? defaultSubjectLabel;
|
|
246
|
+
const inferModificationFactoryName =
|
|
247
|
+
options.defaultModificationFactoryName ?? defaultModificationFactoryNameForSubject;
|
|
248
|
+
|
|
249
|
+
const plannable: TIssue[] = [];
|
|
250
|
+
// Resolved-subject suppressions are deduplicated by subject key so we emit
|
|
251
|
+
// one warning per suppressed subject, not one per suppressed issue.
|
|
252
|
+
// `creationFactoryName` upgrades from `undefined` to a concrete creation
|
|
253
|
+
// name the first time we see a creation-flavoured issue for the subject.
|
|
254
|
+
const suppressedSubjects = new Map<
|
|
255
|
+
string,
|
|
256
|
+
{
|
|
257
|
+
readonly subject: ControlPolicySubject;
|
|
258
|
+
readonly policy: ControlPolicy;
|
|
259
|
+
creationFactoryName?: string;
|
|
260
|
+
}
|
|
261
|
+
>();
|
|
262
|
+
const unresolvedSuppressions: SqlPlannerConflict[] = [];
|
|
263
|
+
|
|
264
|
+
for (const issue of options.issues) {
|
|
265
|
+
const subject = options.resolveControlPolicySubject(issue);
|
|
266
|
+
const policy = controlPolicyForCall(subject, defaultControlPolicy);
|
|
267
|
+
const creationFactoryName = options.resolveCreationFactoryName(issue);
|
|
268
|
+
|
|
269
|
+
if (policy === 'managed') {
|
|
270
|
+
plannable.push(issue);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (
|
|
274
|
+
policy === 'tolerated' &&
|
|
275
|
+
subject !== undefined &&
|
|
276
|
+
creationFactoryName !== undefined &&
|
|
277
|
+
subject.createsNewObject
|
|
278
|
+
) {
|
|
279
|
+
plannable.push(issue);
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (subject === undefined) {
|
|
284
|
+
const factoryName = creationFactoryName ?? 'unknown';
|
|
285
|
+
unresolvedSuppressions.push(
|
|
286
|
+
buildSubjectSuppressionWarning(undefined, policy, factoryName, formatSubjectLabel),
|
|
287
|
+
);
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const key = subjectKey(subject);
|
|
292
|
+
const existing = suppressedSubjects.get(key);
|
|
293
|
+
if (existing) {
|
|
294
|
+
if (existing.creationFactoryName === undefined && creationFactoryName !== undefined) {
|
|
295
|
+
existing.creationFactoryName = creationFactoryName;
|
|
296
|
+
}
|
|
297
|
+
} else {
|
|
298
|
+
suppressedSubjects.set(key, {
|
|
299
|
+
subject,
|
|
300
|
+
policy,
|
|
301
|
+
...ifDefined('creationFactoryName', creationFactoryName),
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const warnings: SqlPlannerConflict[] = [...unresolvedSuppressions];
|
|
307
|
+
for (const entry of suppressedSubjects.values()) {
|
|
308
|
+
const factoryName = entry.creationFactoryName ?? inferModificationFactoryName(entry.subject);
|
|
309
|
+
warnings.push(
|
|
310
|
+
buildSubjectSuppressionWarning(entry.subject, entry.policy, factoryName, formatSubjectLabel),
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return Object.freeze({
|
|
315
|
+
plannable: Object.freeze(plannable),
|
|
316
|
+
warnings: Object.freeze(warnings),
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function subjectKey(subject: ControlPolicySubject): string {
|
|
321
|
+
return `${subject.namespaceId}\u0000${subject.table ?? ''}\u0000${subject.typeName ?? ''}`;
|
|
322
|
+
}
|
|
@@ -77,8 +77,8 @@ export function planFieldEventOperations(
|
|
|
77
77
|
for (const namespaceId of namespaceIds) {
|
|
78
78
|
const priorNs = priorContract?.storage.namespaces[namespaceId];
|
|
79
79
|
const newNs = newContract.storage.namespaces[namespaceId];
|
|
80
|
-
const priorTables = priorNs?.
|
|
81
|
-
const newTables = newNs?.
|
|
80
|
+
const priorTables = priorNs?.entries.table;
|
|
81
|
+
const newTables = newNs?.entries.table;
|
|
82
82
|
|
|
83
83
|
const tableNames = unionSorted(
|
|
84
84
|
priorTables ? Object.keys(priorTables) : [],
|
|
@@ -111,10 +111,26 @@ export function createMigrationPlan<TTargetDetails>(
|
|
|
111
111
|
|
|
112
112
|
export function plannerSuccess<TTargetDetails>(
|
|
113
113
|
plan: SqlMigrationPlan<TTargetDetails>,
|
|
114
|
+
warnings?: readonly SqlPlannerConflict[],
|
|
114
115
|
): SqlPlannerSuccessResult<TTargetDetails> {
|
|
115
116
|
return Object.freeze({
|
|
116
117
|
kind: 'success',
|
|
117
118
|
plan,
|
|
119
|
+
...(warnings && warnings.length > 0
|
|
120
|
+
? {
|
|
121
|
+
warnings: Object.freeze(
|
|
122
|
+
warnings.map((conflict) =>
|
|
123
|
+
Object.freeze({
|
|
124
|
+
kind: conflict.kind,
|
|
125
|
+
summary: conflict.summary,
|
|
126
|
+
...(conflict.why ? { why: conflict.why } : {}),
|
|
127
|
+
...(conflict.location ? { location: Object.freeze({ ...conflict.location }) } : {}),
|
|
128
|
+
...(conflict.meta ? { meta: cloneRecord(conflict.meta) } : {}),
|
|
129
|
+
}),
|
|
130
|
+
),
|
|
131
|
+
),
|
|
132
|
+
}
|
|
133
|
+
: {}),
|
|
118
134
|
});
|
|
119
135
|
}
|
|
120
136
|
|
|
@@ -4,7 +4,6 @@ import type {
|
|
|
4
4
|
ContractSerializer,
|
|
5
5
|
ContractSpace,
|
|
6
6
|
ControlAdapterDescriptor,
|
|
7
|
-
ControlDriverInstance,
|
|
8
7
|
ControlExtensionDescriptor,
|
|
9
8
|
MigratableTargetDescriptor,
|
|
10
9
|
MigrationOperationPolicy,
|
|
@@ -22,7 +21,9 @@ import type {
|
|
|
22
21
|
SchemaIssue,
|
|
23
22
|
SchemaVerifier,
|
|
24
23
|
} from '@prisma-next/framework-components/control';
|
|
24
|
+
import type { AggregateMigrationEdgeRef } from '@prisma-next/migration-tools/aggregate';
|
|
25
25
|
import type {
|
|
26
|
+
SqlControlDriverInstance,
|
|
26
27
|
SqlStorage,
|
|
27
28
|
StorageColumn,
|
|
28
29
|
StorageTable,
|
|
@@ -31,6 +32,7 @@ import type {
|
|
|
31
32
|
import type { SqlOperationDescriptors } from '@prisma-next/sql-operations';
|
|
32
33
|
import type { SqlSchemaIR } from '@prisma-next/sql-schema-ir/types';
|
|
33
34
|
import type { Result } from '@prisma-next/utils/result';
|
|
35
|
+
import type { SqlControlAdapter } from '../control-adapter';
|
|
34
36
|
import type { SqlControlFamilyInstance } from '../control-instance';
|
|
35
37
|
|
|
36
38
|
export type AnyRecord = Readonly<Record<string, unknown>>;
|
|
@@ -115,7 +117,7 @@ export interface CodecControlHooks<TTargetDetails = unknown> {
|
|
|
115
117
|
readonly schemaName?: string;
|
|
116
118
|
}) => readonly SchemaIssue[];
|
|
117
119
|
introspectTypes?: (options: {
|
|
118
|
-
readonly driver:
|
|
120
|
+
readonly driver: SqlControlDriverInstance<string>;
|
|
119
121
|
readonly schemaName?: string;
|
|
120
122
|
}) => Promise<Record<string, StorageTypeInstance>>;
|
|
121
123
|
/**
|
|
@@ -174,7 +176,7 @@ export interface SqlControlExtensionDescriptor<TTargetId extends string>
|
|
|
174
176
|
}
|
|
175
177
|
|
|
176
178
|
export interface SqlControlAdapterDescriptor<TTargetId extends string>
|
|
177
|
-
extends ControlAdapterDescriptor<'sql', TTargetId
|
|
179
|
+
extends ControlAdapterDescriptor<'sql', TTargetId, SqlControlAdapter<TTargetId>> {
|
|
178
180
|
readonly queryOperations?: () => SqlOperationDescriptors;
|
|
179
181
|
}
|
|
180
182
|
|
|
@@ -268,9 +270,11 @@ export type SqlPlannerConflictKind =
|
|
|
268
270
|
| 'indexIncompatible'
|
|
269
271
|
| 'foreignKeyConflict'
|
|
270
272
|
| 'missingButNonAdditive'
|
|
271
|
-
| 'unsupportedOperation'
|
|
273
|
+
| 'unsupportedOperation'
|
|
274
|
+
| 'controlPolicySuppressedCall';
|
|
272
275
|
|
|
273
276
|
export interface SqlPlannerConflictLocation {
|
|
277
|
+
readonly namespace?: string;
|
|
274
278
|
readonly table?: string;
|
|
275
279
|
readonly column?: string;
|
|
276
280
|
readonly index?: string;
|
|
@@ -349,7 +353,7 @@ export interface SqlMigrationRunnerExecuteCallbacks<TTargetDetails> {
|
|
|
349
353
|
|
|
350
354
|
export interface SqlMigrationRunnerExecuteOptions<TTargetDetails> {
|
|
351
355
|
readonly plan: SqlMigrationPlan<TTargetDetails>;
|
|
352
|
-
readonly driver:
|
|
356
|
+
readonly driver: SqlControlDriverInstance<string>;
|
|
353
357
|
/**
|
|
354
358
|
* Logical contract space this plan applies to. When omitted the
|
|
355
359
|
* runner derives the space from {@link SqlMigrationPlan.spaceId};
|
|
@@ -384,12 +388,18 @@ export interface SqlMigrationRunnerExecuteOptions<TTargetDetails> {
|
|
|
384
388
|
* All components must have matching familyId ('sql') and targetId.
|
|
385
389
|
*/
|
|
386
390
|
readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'sql', string>>;
|
|
391
|
+
/**
|
|
392
|
+
* Per-edge breakdown from graph-walk planning. When present, the runner
|
|
393
|
+
* writes one ledger row per edge instead of one collapsed row per apply.
|
|
394
|
+
*/
|
|
395
|
+
readonly migrationEdges: readonly AggregateMigrationEdgeRef[];
|
|
387
396
|
}
|
|
388
397
|
|
|
389
398
|
export type SqlMigrationRunnerErrorCode =
|
|
390
399
|
| 'DESTINATION_CONTRACT_MISMATCH'
|
|
391
400
|
| 'LEGACY_MARKER_SHAPE'
|
|
392
401
|
| 'MARKER_ORIGIN_MISMATCH'
|
|
402
|
+
| 'MARKER_CAS_FAILURE'
|
|
393
403
|
| 'POLICY_VIOLATION'
|
|
394
404
|
| 'PRECHECK_FAILED'
|
|
395
405
|
| 'POSTCHECK_FAILED'
|
|
@@ -424,7 +434,7 @@ export interface SqlMigrationRunner<TTargetDetails> {
|
|
|
424
434
|
* (the connection the outer transaction is open on).
|
|
425
435
|
*/
|
|
426
436
|
execute(options: {
|
|
427
|
-
readonly driver:
|
|
437
|
+
readonly driver: SqlControlDriverInstance<string>;
|
|
428
438
|
readonly perSpaceOptions: ReadonlyArray<SqlMigrationRunnerExecuteOptions<TTargetDetails>>;
|
|
429
439
|
}): Promise<MigrationRunnerResult>;
|
|
430
440
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ControlPolicy } from '@prisma-next/contract/types';
|
|
2
|
+
import type {
|
|
3
|
+
SchemaIssue,
|
|
4
|
+
SchemaVerificationNode,
|
|
5
|
+
VerifierOutcome,
|
|
6
|
+
} from '@prisma-next/framework-components/control';
|
|
7
|
+
import { verifierDisposition } from './verifier-disposition';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Grades `issue` under `controlPolicy` and, unless suppressed, pushes both the
|
|
11
|
+
* issue and a status-stamped verification node. Returns the resolved outcome so
|
|
12
|
+
* the caller never re-grades the same issue.
|
|
13
|
+
*/
|
|
14
|
+
export function emitIssueAndNodeUnderControlPolicy(
|
|
15
|
+
controlPolicy: ControlPolicy,
|
|
16
|
+
issue: SchemaIssue,
|
|
17
|
+
node: SchemaVerificationNode,
|
|
18
|
+
issues: SchemaIssue[],
|
|
19
|
+
nodes: SchemaVerificationNode[],
|
|
20
|
+
): VerifierOutcome {
|
|
21
|
+
const disposition = verifierDisposition(controlPolicy, issue.kind);
|
|
22
|
+
if (disposition === 'suppress') {
|
|
23
|
+
return disposition;
|
|
24
|
+
}
|
|
25
|
+
issues.push(issue);
|
|
26
|
+
nodes.push({ ...node, status: disposition });
|
|
27
|
+
return disposition;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Grades `issue` under `controlPolicy` and, unless suppressed, pushes the issue
|
|
32
|
+
* (no verification node). Returns the resolved outcome so the caller maps it to
|
|
33
|
+
* a node status itself without re-grading.
|
|
34
|
+
*/
|
|
35
|
+
export function emitIssueUnderControlPolicy(
|
|
36
|
+
controlPolicy: ControlPolicy,
|
|
37
|
+
issue: SchemaIssue,
|
|
38
|
+
issues: SchemaIssue[],
|
|
39
|
+
): VerifierOutcome {
|
|
40
|
+
const disposition = verifierDisposition(controlPolicy, issue.kind);
|
|
41
|
+
if (disposition === 'suppress') {
|
|
42
|
+
return disposition;
|
|
43
|
+
}
|
|
44
|
+
issues.push(issue);
|
|
45
|
+
return disposition;
|
|
46
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { ControlPolicy } from '@prisma-next/contract/types';
|
|
2
|
+
import type {
|
|
3
|
+
SchemaIssue,
|
|
4
|
+
VerifierIssueCategory,
|
|
5
|
+
VerifierOutcome,
|
|
6
|
+
} from '@prisma-next/framework-components/control';
|
|
7
|
+
import { dispositionForCategory } from '@prisma-next/framework-components/control';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Classifies the relational verifier issue kinds the SQL family emits (tables,
|
|
11
|
+
* columns, constraints, indexes, defaults, enum types) into the target-neutral
|
|
12
|
+
* categories the framework grades. The relational vocabulary lives here, in the
|
|
13
|
+
* SQL domain — the framework never switches over `extra_foreign_key` and friends.
|
|
14
|
+
*/
|
|
15
|
+
export function classifySqlVerifierIssueKind(kind: SchemaIssue['kind']): VerifierIssueCategory {
|
|
16
|
+
switch (kind) {
|
|
17
|
+
case 'extra_column':
|
|
18
|
+
return 'extraNestedElement';
|
|
19
|
+
case 'extra_primary_key':
|
|
20
|
+
case 'extra_foreign_key':
|
|
21
|
+
case 'extra_unique_constraint':
|
|
22
|
+
case 'extra_index':
|
|
23
|
+
case 'extra_validator':
|
|
24
|
+
case 'extra_default':
|
|
25
|
+
return 'extraAuxiliary';
|
|
26
|
+
case 'extra_table':
|
|
27
|
+
return 'extraTopLevelObject';
|
|
28
|
+
case 'missing_schema':
|
|
29
|
+
case 'missing_table':
|
|
30
|
+
case 'missing_column':
|
|
31
|
+
case 'type_missing':
|
|
32
|
+
case 'default_missing':
|
|
33
|
+
return 'declaredMissing';
|
|
34
|
+
case 'type_values_mismatch':
|
|
35
|
+
case 'enum_values_changed':
|
|
36
|
+
return 'valueDrift';
|
|
37
|
+
case 'type_mismatch':
|
|
38
|
+
case 'nullability_mismatch':
|
|
39
|
+
case 'primary_key_mismatch':
|
|
40
|
+
case 'foreign_key_mismatch':
|
|
41
|
+
case 'unique_constraint_mismatch':
|
|
42
|
+
case 'index_mismatch':
|
|
43
|
+
case 'default_mismatch':
|
|
44
|
+
return 'declaredIncompatible';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function verifierDisposition(
|
|
49
|
+
controlPolicy: ControlPolicy,
|
|
50
|
+
issueKind: SchemaIssue['kind'],
|
|
51
|
+
): VerifierOutcome {
|
|
52
|
+
return dispositionForCategory(controlPolicy, classifySqlVerifierIssueKind(issueKind));
|
|
53
|
+
}
|