@pgxsinkit/contracts 0.1.32 → 0.1.36
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/README.md +13 -0
- package/dist/apply-strategy.d.ts +35 -0
- package/dist/apply-strategy.d.ts.map +1 -0
- package/dist/config.d.ts +210 -32
- package/dist/config.d.ts.map +1 -1
- package/dist/convergence-model.d.ts +106 -0
- package/dist/convergence-model.d.ts.map +1 -0
- package/dist/fingerprint.d.ts +136 -0
- package/dist/fingerprint.d.ts.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5222 -3494
- package/dist/index.js.map +37 -30
- package/dist/mutation.d.ts +78 -0
- package/dist/mutation.d.ts.map +1 -1
- package/dist/projection.d.ts +88 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/registry-diff.d.ts +50 -0
- package/dist/registry-diff.d.ts.map +1 -0
- package/dist/registry.d.ts +253 -2
- package/dist/registry.d.ts.map +1 -1
- package/dist/runtime.d.ts +21 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/sql-defaults.d.ts +27 -0
- package/dist/sql-defaults.d.ts.map +1 -0
- package/dist/sql-identifier.d.ts +26 -0
- package/dist/sql-identifier.d.ts.map +1 -0
- package/dist/supabase-rls.d.ts +99 -3
- package/dist/supabase-rls.d.ts.map +1 -1
- package/package.json +8 -3
- package/skills/registry-authoring/SKILL.md +376 -0
- package/src/apply-strategy.ts +124 -0
- package/src/config.ts +254 -106
- package/src/convergence-model.ts +190 -0
- package/src/fingerprint.ts +265 -0
- package/src/index.ts +7 -0
- package/src/mutation.ts +94 -2
- package/src/projection.ts +150 -0
- package/src/registry-diff.ts +215 -0
- package/src/registry.ts +751 -23
- package/src/runtime.ts +21 -1
- package/src/sql-defaults.ts +29 -0
- package/src/sql-identifier.ts +130 -0
- package/src/supabase-rls.ts +515 -37
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { canonicalizeRegistry, fingerprintRegistry, type CanonicalTable } from "./fingerprint";
|
|
2
|
+
import type { SyncTableRegistry } from "./registry";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The registry-diff gate (ADR-0006): classify a registry change as
|
|
6
|
+
* `compatible | risky | breaking` so loss-detection happens at *authoring* time, not
|
|
7
|
+
* as a runtime surprise. A breaking diff is a conscious release decision — rework to
|
|
8
|
+
* expand/contract, or accept-and-notify. This catches the one case the runtime cannot:
|
|
9
|
+
* silent column *repurposing* (a same-named column whose type/meaning changed).
|
|
10
|
+
*
|
|
11
|
+
* pgxsinkit ships this mechanism; enforcement (whether a non-zero check blocks CI) is
|
|
12
|
+
* the consumer's, via a committed lock that makes a breaking change a reviewable diff.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export type RegistryChangeSeverity = "compatible" | "risky" | "breaking";
|
|
16
|
+
|
|
17
|
+
export interface RegistryChange {
|
|
18
|
+
severity: RegistryChangeSeverity;
|
|
19
|
+
table: string;
|
|
20
|
+
detail: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface RegistryDiff {
|
|
24
|
+
severity: RegistryChangeSeverity;
|
|
25
|
+
changes: RegistryChange[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A committed baseline of a registry's shape — the enforcement surface for the gate. */
|
|
29
|
+
export interface RegistryLock {
|
|
30
|
+
version: string;
|
|
31
|
+
tables: CanonicalTable[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const SEVERITY_RANK: Record<RegistryChangeSeverity, number> = { compatible: 0, risky: 1, breaking: 2 };
|
|
35
|
+
|
|
36
|
+
function maxSeverity(changes: readonly RegistryChange[]): RegistryChangeSeverity {
|
|
37
|
+
return changes.reduce<RegistryChangeSeverity>(
|
|
38
|
+
(worst, change) => (SEVERITY_RANK[change.severity] > SEVERITY_RANK[worst] ? change.severity : worst),
|
|
39
|
+
"compatible",
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Build the committed lock (fingerprint + canonical shape) for a registry. */
|
|
44
|
+
export function buildRegistryLock(registry: SyncTableRegistry): RegistryLock {
|
|
45
|
+
return { version: fingerprintRegistry(registry), tables: canonicalizeRegistry(registry) };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function diffTable(table: string, previous: CanonicalTable, next: CanonicalTable, changes: RegistryChange[]): void {
|
|
49
|
+
const prevColumns = new Map(previous.columns.map((column) => [column.name, column]));
|
|
50
|
+
const nextColumns = new Map(next.columns.map((column) => [column.name, column]));
|
|
51
|
+
|
|
52
|
+
for (const [name, prevColumn] of prevColumns) {
|
|
53
|
+
const nextColumn = nextColumns.get(name);
|
|
54
|
+
if (!nextColumn) {
|
|
55
|
+
// A rename appears as remove + add; both are breaking, which is the safe read.
|
|
56
|
+
changes.push({ severity: "breaking", table, detail: `column removed: ${name}` });
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (prevColumn.type !== nextColumn.type) {
|
|
60
|
+
// Same name, different type: the silent-repurposing case the runtime cannot see.
|
|
61
|
+
changes.push({
|
|
62
|
+
severity: "breaking",
|
|
63
|
+
table,
|
|
64
|
+
detail: `column type changed: ${name} (${prevColumn.type} -> ${nextColumn.type})`,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (!prevColumn.notNull && nextColumn.notNull) {
|
|
68
|
+
changes.push({ severity: "breaking", table, detail: `column became NOT NULL: ${name}` });
|
|
69
|
+
} else if (prevColumn.notNull && !nextColumn.notNull) {
|
|
70
|
+
changes.push({ severity: "compatible", table, detail: `column relaxed to nullable: ${name}` });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const [name, nextColumn] of nextColumns) {
|
|
75
|
+
if (prevColumns.has(name)) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const safe = !nextColumn.notNull || nextColumn.hasDefault;
|
|
79
|
+
changes.push({
|
|
80
|
+
severity: safe ? "compatible" : "breaking",
|
|
81
|
+
table,
|
|
82
|
+
detail: safe ? `column added: ${name}` : `NOT NULL column added without default: ${name}`,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (JSON.stringify(previous.primaryKey) !== JSON.stringify(next.primaryKey)) {
|
|
87
|
+
changes.push({ severity: "breaking", table, detail: "primary key changed" });
|
|
88
|
+
}
|
|
89
|
+
if (JSON.stringify(previous.localPrimaryKey) !== JSON.stringify(next.localPrimaryKey)) {
|
|
90
|
+
// The local identity the overlay + journal rows are keyed on; changing it orphans
|
|
91
|
+
// any owed (un-acked) writes — the runtime cannot re-key them.
|
|
92
|
+
changes.push({ severity: "breaking", table, detail: "local primary key changed" });
|
|
93
|
+
}
|
|
94
|
+
if (previous.mode !== next.mode) {
|
|
95
|
+
changes.push({ severity: "risky", table, detail: `mode changed: ${previous.mode} -> ${next.mode}` });
|
|
96
|
+
}
|
|
97
|
+
diffProjection(table, previous.projection, next.projection, changes);
|
|
98
|
+
diffShape(table, previous.shape, next.shape, changes);
|
|
99
|
+
if ((previous.consistencyGroup ?? null) !== (next.consistencyGroup ?? null)) {
|
|
100
|
+
// A group move re-keys the table's subscription-state row (ADR-0009 decision 2): the local cache
|
|
101
|
+
// columns survive, but the subscription must reset so the table re-streams under its new group.
|
|
102
|
+
changes.push({
|
|
103
|
+
severity: "risky",
|
|
104
|
+
table,
|
|
105
|
+
detail: `consistency group changed: ${previous.consistencyGroup ?? "(singleton)"} -> ${next.consistencyGroup ?? "(singleton)"} (re-sync required)`,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (JSON.stringify(previous.managedFields) !== JSON.stringify(next.managedFields)) {
|
|
109
|
+
// Managed-field governance changes how writes are constructed/owned; a returning client
|
|
110
|
+
// with owed writes authored under the old governance needs a conscious re-sync.
|
|
111
|
+
changes.push({ severity: "risky", table, detail: "managed fields changed (re-sync required)" });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Projection changes. Renaming the synced/overlay/journal tables orphans their
|
|
117
|
+
* name-coupled local data (ADR-0006) → breaking; changing the omitted-column set changes
|
|
118
|
+
* which columns exist locally → a re-sync (risky).
|
|
119
|
+
*/
|
|
120
|
+
function diffProjection(
|
|
121
|
+
table: string,
|
|
122
|
+
previous: CanonicalTable["projection"],
|
|
123
|
+
next: CanonicalTable["projection"],
|
|
124
|
+
changes: RegistryChange[],
|
|
125
|
+
): void {
|
|
126
|
+
for (const key of ["syncedTable", "overlayTable", "journalTable"] as const) {
|
|
127
|
+
if ((previous?.[key] ?? null) !== (next?.[key] ?? null)) {
|
|
128
|
+
changes.push({ severity: "breaking", table, detail: `projection ${key} changed (orphans local data)` });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (JSON.stringify(previous?.omitColumns ?? null) !== JSON.stringify(next?.omitColumns ?? null)) {
|
|
132
|
+
changes.push({ severity: "risky", table, detail: "omitted columns changed (re-sync required)" });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Shape changes. The Electric target (table/shapeKey/electricTable) and the row filter both
|
|
138
|
+
* govern which rows stream; a change to either needs a re-sync so the local cache is not left
|
|
139
|
+
* holding rows selected under the old definition (risky). The row filter's `customWhere` body
|
|
140
|
+
* is invisible to the fingerprint, so a change confined to it is not detectable here.
|
|
141
|
+
*/
|
|
142
|
+
function diffShape(
|
|
143
|
+
table: string,
|
|
144
|
+
previous: CanonicalTable["shape"],
|
|
145
|
+
next: CanonicalTable["shape"],
|
|
146
|
+
changes: RegistryChange[],
|
|
147
|
+
): void {
|
|
148
|
+
const target = (shape: CanonicalTable["shape"]): string | null =>
|
|
149
|
+
shape ? `${shape.tableName}|${shape.shapeKey}|${shape.electricTable ?? ""}` : null;
|
|
150
|
+
|
|
151
|
+
if (target(previous) !== target(next)) {
|
|
152
|
+
changes.push({ severity: "risky", table, detail: "shape target changed (re-sync required)" });
|
|
153
|
+
}
|
|
154
|
+
if (JSON.stringify(previous?.rowFilter ?? null) !== JSON.stringify(next?.rowFilter ?? null)) {
|
|
155
|
+
changes.push({ severity: "risky", table, detail: "row filter changed (re-sync required)" });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Classify the change from one canonical registry shape to another. */
|
|
160
|
+
export function diffCanonicalRegistries(
|
|
161
|
+
previous: readonly CanonicalTable[],
|
|
162
|
+
next: readonly CanonicalTable[],
|
|
163
|
+
): RegistryDiff {
|
|
164
|
+
const changes: RegistryChange[] = [];
|
|
165
|
+
const prevByKey = new Map(previous.map((table) => [table.key, table]));
|
|
166
|
+
const nextByKey = new Map(next.map((table) => [table.key, table]));
|
|
167
|
+
|
|
168
|
+
for (const [key, prevTable] of prevByKey) {
|
|
169
|
+
const nextTable = nextByKey.get(key);
|
|
170
|
+
if (!nextTable) {
|
|
171
|
+
changes.push({ severity: "breaking", table: key, detail: "table removed" });
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
diffTable(key, prevTable, nextTable, changes);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
for (const key of nextByKey.keys()) {
|
|
178
|
+
if (!prevByKey.has(key)) {
|
|
179
|
+
changes.push({ severity: "compatible", table: key, detail: "table added" });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return { changes, severity: maxSeverity(changes) };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Classify the change between two registries. */
|
|
187
|
+
export function compareRegistries(previous: SyncTableRegistry, next: SyncTableRegistry): RegistryDiff {
|
|
188
|
+
return diffCanonicalRegistries(canonicalizeRegistry(previous), canonicalizeRegistry(next));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Classify a registry against a committed lock baseline. */
|
|
192
|
+
export function diffRegistryAgainstLock(registry: SyncTableRegistry, lock: RegistryLock): RegistryDiff {
|
|
193
|
+
return diffCanonicalRegistries(lock.tables, canonicalizeRegistry(registry));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The consumer-facing check: `ok` is false on a breaking diff. The consumer wires the
|
|
198
|
+
* exit code (e.g. `process.exit(result.ok ? 0 : 1)`) into their own CI — pgxsinkit does
|
|
199
|
+
* not reach into anyone's pipeline.
|
|
200
|
+
*/
|
|
201
|
+
export function runRegistryCheck(input: { registry: SyncTableRegistry; lock: RegistryLock }): {
|
|
202
|
+
ok: boolean;
|
|
203
|
+
diff: RegistryDiff;
|
|
204
|
+
} {
|
|
205
|
+
const diff = diffRegistryAgainstLock(input.registry, input.lock);
|
|
206
|
+
return { ok: diff.severity !== "breaking", diff };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** A human-readable, stable summary of a diff (one line per change). */
|
|
210
|
+
export function summarizeRegistryDiff(diff: RegistryDiff): string {
|
|
211
|
+
if (diff.changes.length === 0) {
|
|
212
|
+
return "no registry changes";
|
|
213
|
+
}
|
|
214
|
+
return diff.changes.map((change) => `[${change.severity}] ${change.table}: ${change.detail}`).join("\n");
|
|
215
|
+
}
|