@everystack/cli 0.4.85 → 0.4.86
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/CHANGELOG.md +11 -0
- package/package.json +2 -2
- package/src/cli/apply-execute.ts +80 -19
- package/src/cli/apply-refusals.ts +47 -9
- package/src/cli/commands/db-apply.ts +85 -4
- package/src/cli/commands/db-plan.ts +29 -0
- package/src/cli/edge-plan.ts +55 -13
- package/src/cli/fk-orphan-advisory.ts +170 -0
- package/src/cli/fk-preflight.ts +126 -43
- package/src/cli/model-render.ts +10 -0
- package/src/cli/schema-compile.ts +14 -1
- package/src/cli/schema-diff.ts +47 -10
- package/src/cli/schema-fingerprint.ts +26 -0
- package/src/cli/schema-introspect.ts +20 -7
- package/src/cli/state-apply.ts +84 -17
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,17 @@ All notable changes to `@everystack/cli` are documented here.
|
|
|
4
4
|
|
|
5
5
|
Format based on [Keep a Changelog](https://keepachangelog.com/).
|
|
6
6
|
|
|
7
|
+
## [0.4.86] - 2026-09-16
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- validate: false on a foreign key, and a per-key apply preflight
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
|
|
15
|
+
- non-blocking validation-pending was blind to real anomalies, and its receipt overstated what ran
|
|
16
|
+
- bump APPLY_CONTRACT_VERSION for the client-side exit-code field, close a sibling overstatement bug, prove the producer side
|
|
17
|
+
|
|
7
18
|
## [0.4.85] - 2026-09-16
|
|
8
19
|
|
|
9
20
|
### Added
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@everystack/cli",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.86",
|
|
4
4
|
"description": "CLI and OTA updates for Expo apps on everystack",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"author": "Scalable Technology, Inc. <licensing@scalable.technology>",
|
|
@@ -135,7 +135,7 @@
|
|
|
135
135
|
"structured-headers": "1.0.1",
|
|
136
136
|
"tsx": "4.21.0",
|
|
137
137
|
"typescript": "5.9.3",
|
|
138
|
-
"@everystack/model": "0.4.
|
|
138
|
+
"@everystack/model": "0.4.19"
|
|
139
139
|
},
|
|
140
140
|
"peerDependencies": {
|
|
141
141
|
"@everystack/server": ">=0.4.0",
|
package/src/cli/apply-execute.ts
CHANGED
|
@@ -57,8 +57,16 @@ import {
|
|
|
57
57
|
import { FUNCTIONS_SQL, contractFunctionRow } from './security-catalog';
|
|
58
58
|
import { fingerprintLive, FINGERPRINT_VERSION } from './schema-fingerprint';
|
|
59
59
|
import { applyGeneratedStatements } from './state-apply';
|
|
60
|
-
import {
|
|
61
|
-
|
|
60
|
+
import {
|
|
61
|
+
fkOrphanChecks,
|
|
62
|
+
fkOrphanSql,
|
|
63
|
+
fkOrphanFindings,
|
|
64
|
+
withheldValidations,
|
|
65
|
+
fkPreflightUnavailableRefusal,
|
|
66
|
+
checkOrphanRowShape,
|
|
67
|
+
type FkOrphanFinding,
|
|
68
|
+
} from './fk-preflight';
|
|
69
|
+
import { validationPendingReason, pendingValidationsAreSafe } from './apply-refusals';
|
|
62
70
|
import { ENSURE_RECONCILER_SQL, renderSchemaLogInsert, renderSchemaLogFingerprintUpdate } from './derived-apply';
|
|
63
71
|
import { checkPlanPrecondition, planHash, defaultAclHash, type EdgePlan } from './edge-plan';
|
|
64
72
|
import type { DescentLedgerRow } from './git-descent';
|
|
@@ -150,14 +158,49 @@ export interface ApplyPlanResult {
|
|
|
150
158
|
| 'authority-refused'
|
|
151
159
|
| 'snapshot-refused'
|
|
152
160
|
| 'authz-refused'
|
|
153
|
-
/**
|
|
161
|
+
/**
|
|
162
|
+
* The foreign-key row check itself could not RUN — no privilege, a lock timeout, a dropped
|
|
163
|
+
* connection. Nothing ran: a check that did not run has not passed.
|
|
164
|
+
*
|
|
165
|
+
* It is no longer what "some rows do not satisfy a key" produces. That is a per-key finding
|
|
166
|
+
* now: the offending key's `VALIDATE` is withheld and the rest of the edge commits as
|
|
167
|
+
* `validation-pending`. Refusing a whole edge — unrelated grants, unrelated clean keys —
|
|
168
|
+
* over legacy rows nobody was changing was the defect, not the safety.
|
|
169
|
+
*/
|
|
154
170
|
| 'preflight-refused'
|
|
155
|
-
/**
|
|
171
|
+
/**
|
|
172
|
+
* The edge COMMITTED and at least one `VALIDATE CONSTRAINT` is outstanding — because it
|
|
173
|
+
* failed, or because the row check counted the offenders first and it was withheld. Not a
|
|
174
|
+
* rollback, in either case.
|
|
175
|
+
*/
|
|
156
176
|
| 'validation-pending'
|
|
157
177
|
| 'verify-failed';
|
|
158
178
|
liveFingerprint: string;
|
|
159
179
|
reason?: string;
|
|
160
180
|
logId?: number;
|
|
181
|
+
/**
|
|
182
|
+
* The keys the row check found orphans on, if any — data, for the caller's advisory (see
|
|
183
|
+
* fk-orphan-advisory). Present on the paths that got as far as running the check, empty
|
|
184
|
+
* otherwise. Crosses the ops-Lambda wire with the rest of the result.
|
|
185
|
+
*/
|
|
186
|
+
fkOrphans?: FkOrphanFinding[];
|
|
187
|
+
/**
|
|
188
|
+
* Only present when `status` is `'validation-pending'`. True iff every outstanding key's cause
|
|
189
|
+
* is understood to be the data (withheld by the preflight, or a live failure with the ordinary
|
|
190
|
+
* orphan-row error) — never a check that was cut off or failed for an unrecognised reason. The
|
|
191
|
+
* CLI reports the safe case as a non-blocking success (the whole point of this status) and the
|
|
192
|
+
* unsafe case exactly like any other refusal, exit code included — a real operational anomaly
|
|
193
|
+
* must still reach automation gating on this command's exit status.
|
|
194
|
+
*/
|
|
195
|
+
pendingIsSafe?: boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Only present when `status` is `'validation-pending'`. The exact statement text of every
|
|
198
|
+
* `VALIDATE CONSTRAINT` that did not land — withheld before it ran, or attempted and failed —
|
|
199
|
+
* so a receipt built from `plan.statements` (which is never rewritten; see below) can exclude
|
|
200
|
+
* them. Without this, a receipt naively re-derived from the full plan reports a withheld or
|
|
201
|
+
* failed VALIDATE as an applied change, which it never was.
|
|
202
|
+
*/
|
|
203
|
+
unappliedStatements?: string[];
|
|
161
204
|
}
|
|
162
205
|
|
|
163
206
|
/**
|
|
@@ -373,32 +416,45 @@ export async function executeApplyPlan(
|
|
|
373
416
|
}
|
|
374
417
|
}
|
|
375
418
|
|
|
376
|
-
// The LAST
|
|
377
|
-
//
|
|
378
|
-
// answer that during the VALIDATE, one row at a time, with the lock already
|
|
379
|
-
// the answer arrives with
|
|
380
|
-
// lock was just computed from, so the question and the lock describe one instant.
|
|
419
|
+
// The LAST step before any DDL, and the only one that asks about DATA. A foreign key this plan
|
|
420
|
+
// asks to be VALIDATED can only be validated if the rows already in the table satisfy it;
|
|
421
|
+
// PostgreSQL would answer that during the VALIDATE, one row at a time, with the lock already
|
|
422
|
+
// taken. Asked here, the answer arrives with an exact count and a sample. The snapshot is the
|
|
423
|
+
// one the lock was just computed from, so the question and the lock describe one instant.
|
|
424
|
+
//
|
|
425
|
+
// WHAT THE ANSWER DOES, and this is the fix. It used to REFUSE the whole edge if any key had
|
|
426
|
+
// one orphan — the unrelated grants, the clean keys, everything, held behind a data audit of
|
|
427
|
+
// rows nobody was touching. Now the answer is read PER KEY: the offending key's `VALIDATE` is
|
|
428
|
+
// withheld (the key is still installed by the batch, NOT VALID, enforced on every new write),
|
|
429
|
+
// recorded as a pending validation, and the rest of the edge commits. The apply's outcome is
|
|
430
|
+
// `validation-pending`, which already means exactly this.
|
|
431
|
+
//
|
|
432
|
+
// TWO THINGS STILL REFUSE, and only these two: the check could not RUN, and the read answered
|
|
433
|
+
// in a shape nobody can read. Both are the gate not having run, which is not the same as
|
|
434
|
+
// passing, so both fail closed with nothing applied.
|
|
381
435
|
const orphanChecks = fkOrphanChecks(plan.statements, before);
|
|
436
|
+
let orphans: FkOrphanFinding[] = [];
|
|
382
437
|
if (orphanChecks.length > 0) {
|
|
383
|
-
let refusal: string | null;
|
|
384
438
|
try {
|
|
385
439
|
const rows = (await runner(fkOrphanSql(orphanChecks))) as unknown;
|
|
386
440
|
// The read answers with exactly one row per key, zero orphans included. A short or
|
|
387
|
-
// missing answer is not "clean" — it is the gate not having run
|
|
388
|
-
// same fail-closed path as a read that could not run at all.
|
|
441
|
+
// missing answer is not "clean" — it is the gate not having run.
|
|
389
442
|
checkOrphanRowShape(rows, orphanChecks.length);
|
|
390
|
-
|
|
443
|
+
orphans = fkOrphanFindings(rows);
|
|
391
444
|
} catch (err: any) {
|
|
392
|
-
|
|
393
|
-
refusal = fkPreflightUnavailableRefusal(String(err?.message ?? err));
|
|
394
|
-
}
|
|
395
|
-
if (refusal) {
|
|
445
|
+
const refusal = fkPreflightUnavailableRefusal(String(err?.message ?? err));
|
|
396
446
|
await recordRefusal(runner, plan, live, 'foreign key preflight', refusal, options);
|
|
397
|
-
return { status: 'preflight-refused', liveFingerprint: live, reason: refusal };
|
|
447
|
+
return { status: 'preflight-refused', liveFingerprint: live, reason: refusal, fkOrphans: [] };
|
|
398
448
|
}
|
|
399
449
|
}
|
|
400
450
|
|
|
401
|
-
|
|
451
|
+
// The edge minus the checks whose answer is already known. `plan.statements` itself is never
|
|
452
|
+
// rewritten — the plan is an artifact under a content hash, and `planHash(plan)` below has to
|
|
453
|
+
// keep naming the file the operator reviewed.
|
|
454
|
+
const { statements: toRun, withheld } = withheldValidations(plan.statements, orphans);
|
|
455
|
+
|
|
456
|
+
const result = await applyGeneratedStatements(runner, toRun, {
|
|
457
|
+
...(withheld.length > 0 ? { skippedValidations: withheld } : {}),
|
|
402
458
|
actor: options.actor,
|
|
403
459
|
gitRef: options.gitRef ?? plan.gitRef,
|
|
404
460
|
planRef: planHash(plan),
|
|
@@ -434,6 +490,9 @@ export async function executeApplyPlan(
|
|
|
434
490
|
// clock is a lane change. See validationPendingReason.
|
|
435
491
|
reason: validationPendingReason(result.pendingValidations),
|
|
436
492
|
...(result.logId !== undefined ? { logId: result.logId } : {}),
|
|
493
|
+
fkOrphans: orphans,
|
|
494
|
+
pendingIsSafe: pendingValidationsAreSafe(result.pendingValidations),
|
|
495
|
+
unappliedStatements: result.pendingValidations.map((p) => p.sql),
|
|
437
496
|
};
|
|
438
497
|
}
|
|
439
498
|
|
|
@@ -444,12 +503,14 @@ export async function executeApplyPlan(
|
|
|
444
503
|
liveFingerprint: landed,
|
|
445
504
|
reason: `applied, but the target landed on ${landed.slice(0, 12)} — the plan predicted ${plan.toFingerprint.slice(0, 12)}. Investigate before touching this database again (db:fingerprint, db:generate).`,
|
|
446
505
|
...(result.logId !== undefined ? { logId: result.logId } : {}),
|
|
506
|
+
fkOrphans: orphans,
|
|
447
507
|
};
|
|
448
508
|
}
|
|
449
509
|
return {
|
|
450
510
|
status: 'applied',
|
|
451
511
|
liveFingerprint: landed,
|
|
452
512
|
...(result.logId !== undefined ? { logId: result.logId } : {}),
|
|
513
|
+
fkOrphans: orphans,
|
|
453
514
|
};
|
|
454
515
|
}
|
|
455
516
|
|
|
@@ -31,10 +31,13 @@ export function snapshotMovedRefusal(ref: string, captured: string, planFrom: st
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
* VALIDATION PENDING: the edge committed and a post-commit `VALIDATE CONSTRAINT`
|
|
34
|
+
* VALIDATION PENDING: the edge committed and a post-commit `VALIDATE CONSTRAINT` is outstanding.
|
|
35
35
|
* Neither class above — nothing was refused and nothing is in an unknown state — but it still has
|
|
36
|
-
* to name a next move, and WHICH move depends on why the check
|
|
36
|
+
* to name a next move, and WHICH move depends on why the check has not passed:
|
|
37
37
|
*
|
|
38
|
+
* WITHHELD — the row preflight counted the offending rows BEFORE any DDL, so the check was
|
|
39
|
+
* never attempted and the rest of the edge committed around it. The rows are the
|
|
40
|
+
* known cause, so this is the ORPHAN DATA move with no guessing involved.
|
|
38
41
|
* ORPHAN DATA — the rows genuinely do not satisfy the key. The key is the truth and the data
|
|
39
42
|
* is wrong, so the fix is the DATA (db:exec), then re-plan; the next plan emits
|
|
40
43
|
* the VALIDATE alone.
|
|
@@ -47,10 +50,16 @@ export function snapshotMovedRefusal(ref: string, captured: string, planFrom: st
|
|
|
47
50
|
* the rest of this file follows about guessing.
|
|
48
51
|
*/
|
|
49
52
|
export interface PendingValidationRecord {
|
|
50
|
-
/** The exact `VALIDATE CONSTRAINT` statement that did not pass. */
|
|
53
|
+
/** The exact `VALIDATE CONSTRAINT` statement that did not pass, or was never attempted. */
|
|
51
54
|
sql: string;
|
|
52
|
-
/** PostgreSQL's (or the driver's) message. */
|
|
55
|
+
/** PostgreSQL's (or the driver's) message — or why the check was withheld. */
|
|
53
56
|
error: string;
|
|
57
|
+
/**
|
|
58
|
+
* The check was WITHHELD by the row preflight, never run. A structural flag rather than one
|
|
59
|
+
* more regex, because the text it would be matched against is text this CLI wrote itself: a
|
|
60
|
+
* pattern over our own message is a pattern that breaks the next time somebody rewords it.
|
|
61
|
+
*/
|
|
62
|
+
skipped?: boolean;
|
|
54
63
|
}
|
|
55
64
|
|
|
56
65
|
/** The rows really do violate the key: SQLSTATE 23503 and the message pg renders for it. */
|
|
@@ -60,17 +69,45 @@ const CUT_OFF = /timeout|canceling statement|terminat(ed|ing)|connection (closed
|
|
|
60
69
|
|
|
61
70
|
type PendingCause = 'data' | 'cut-off' | 'unknown';
|
|
62
71
|
|
|
63
|
-
function causeOf(
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
function causeOf(pending: PendingValidationRecord): PendingCause {
|
|
73
|
+
// A withheld check is the ONE case where the cause is known rather than inferred: the
|
|
74
|
+
// preflight counted the rows itself. No pattern is consulted.
|
|
75
|
+
if (pending.skipped === true) return 'data';
|
|
76
|
+
if (ORPHAN_DATA.test(pending.error)) return 'data';
|
|
77
|
+
if (CUT_OFF.test(pending.error)) return 'cut-off';
|
|
66
78
|
return 'unknown';
|
|
67
79
|
}
|
|
68
80
|
|
|
81
|
+
/**
|
|
82
|
+
* True iff every outstanding key's cause is the DATA — withheld by the preflight, or a live
|
|
83
|
+
* `VALIDATE` that failed with the ordinary orphan-row error. False the moment any cause is CUT
|
|
84
|
+
* OFF or UNKNOWN: a check that did not answer, or failed for a reason this command does not
|
|
85
|
+
* recognise, is a real operational anomaly, not the known, harmless fact this status otherwise
|
|
86
|
+
* describes — the caller must not report the two the same way (a non-blocking success either
|
|
87
|
+
* way would swallow a race condition or an infra hiccup from any automation gating on exit code).
|
|
88
|
+
*/
|
|
89
|
+
export function pendingValidationsAreSafe(pending: readonly PendingValidationRecord[]): boolean {
|
|
90
|
+
return pending.every((p) => causeOf(p) === 'data');
|
|
91
|
+
}
|
|
92
|
+
|
|
69
93
|
export function validationPendingReason(pending: readonly PendingValidationRecord[]): string {
|
|
70
|
-
const causes = new Set(pending.map(
|
|
94
|
+
const causes = new Set(pending.map(causeOf));
|
|
71
95
|
const unknown = causes.has('unknown');
|
|
96
|
+
const withheld = pending.filter((p) => p.skipped === true).length;
|
|
72
97
|
|
|
73
98
|
const moves: string[] = [];
|
|
99
|
+
if (withheld > 0) {
|
|
100
|
+
moves.push(
|
|
101
|
+
`${withheld} of them ${withheld === 1 ? 'was' : 'were'} never attempted: the row check counted the `
|
|
102
|
+
+ 'offending rows before any DDL ran, so the apply skipped that scan and committed everything '
|
|
103
|
+
+ 'else in the edge. Declare `validate: false` on the reference to keep the key enforced '
|
|
104
|
+
+ 'forward and stop being asked.',
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
// Named for BOTH a withheld key and one that ran and failed with the ordinary orphan-row error
|
|
108
|
+
// — deliberately, not a duplicate of the withheld move above: they are two DIFFERENT valid
|
|
109
|
+
// resolutions (accept it forever vs. clean the data and re-plan), not two descriptions of one
|
|
110
|
+
// fact, so an operator choosing between them is owed both options in the same message.
|
|
74
111
|
if (causes.has('data') || unknown) {
|
|
75
112
|
moves.push(
|
|
76
113
|
'Rows already in the table do not satisfy the key: fix the data (everystack db:exec runs a '
|
|
@@ -87,6 +124,7 @@ export function validationPendingReason(pending: readonly PendingValidationRecor
|
|
|
87
124
|
}
|
|
88
125
|
if (unknown) moves.unshift('The error below is not one this command recognises, so both moves are named:');
|
|
89
126
|
|
|
127
|
+
|
|
90
128
|
const names = pending.map((p) => p.sql.replace(/\s+/g, ' ').trim()).join(' | ');
|
|
91
129
|
return [
|
|
92
130
|
`the edge applied, but ${pending.length} constraint(s) could not be validated against the rows `
|
|
@@ -140,7 +178,7 @@ export function formatApplyRefusal(status: ApplyRefusalStatus, reason: string):
|
|
|
140
178
|
case 'authority-refused': // the destructive-approver set — hand the plan to an approver
|
|
141
179
|
case 'snapshot-refused': // no confirmed rollback point before a destructive statement
|
|
142
180
|
case 'authz-refused': // a foreign grantee arrived after the plan was minted
|
|
143
|
-
case 'preflight-refused': //
|
|
181
|
+
case 'preflight-refused': // the foreign-key row check could not RUN — nothing ran (orphaned rows no longer come here)
|
|
144
182
|
return `REFUSED: ${reason}`;
|
|
145
183
|
case 'validation-pending':
|
|
146
184
|
// NOT a refusal and NOT a fault: the edge committed, and a constraint is installed but
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
32
|
import fs from 'node:fs/promises';
|
|
33
|
+
import type { ModelDescriptor } from '@everystack/model';
|
|
33
34
|
import { type QueryRunner } from '../authz-contract';
|
|
34
35
|
import { currentGitRef, worktreeDirty, classifyGeneratedStatements, classifyDestructive } from '../state-apply';
|
|
35
36
|
import { planHash, PLAN_VERSION, APPLY_CONTRACT_VERSION, declaredGranteesOf, type EdgePlan } from '../edge-plan';
|
|
@@ -73,6 +74,7 @@ import { resolveOperatorUrlViaStage } from '../direct-venue';
|
|
|
73
74
|
import { withMutationLease, MutationLeaseError } from '../mutation-lease';
|
|
74
75
|
import { readStageStateTwice } from '../stage-read-consistency';
|
|
75
76
|
import { renderApplyReceipt, renderCatchUpFailure } from '../plain-output';
|
|
77
|
+
import { renderFkOrphanAdvisory, ownershipColumns } from '../fk-orphan-advisory';
|
|
76
78
|
import { catchUpDerived, type CatchUpVenue } from '../derived-catchup';
|
|
77
79
|
import { buildReconcileReport } from './db-reconcile';
|
|
78
80
|
import { step, success, fail, info, warn, line as printLine } from '../output';
|
|
@@ -110,12 +112,27 @@ export function reportApplyResult(
|
|
|
110
112
|
* the lesser evil against guessing, and the gate that actually decides already ran.
|
|
111
113
|
*/
|
|
112
114
|
declaredGrantees?: ReadonlySet<string>;
|
|
115
|
+
/**
|
|
116
|
+
* The models, for the advisory's row-security cross-reference. Absent means the barrel could
|
|
117
|
+
* not be read — the notice then prints without the escalation heading rather than not at all.
|
|
118
|
+
*/
|
|
119
|
+
models?: readonly ModelDescriptor[];
|
|
113
120
|
} = {},
|
|
114
121
|
): void {
|
|
115
122
|
// What the function/view catch-up ran on the same target. It joins the receipt's own change
|
|
116
123
|
// list — one command, one list, one count — so a caught-up view is never a second report the
|
|
117
124
|
// reader has to reconcile with the first.
|
|
118
125
|
const catchUp = opts.catchUp ?? [];
|
|
126
|
+
|
|
127
|
+
// The recurring notice about keys that stay unproven — BEFORE the outcome, at every verbosity,
|
|
128
|
+
// on every status that got as far as running the row check. It blocks nothing and it never
|
|
129
|
+
// proposes a row change; it is the debt this apply committed around, stated out loud. Printed
|
|
130
|
+
// here, in the one place both the stage lane and the direct lane converge.
|
|
131
|
+
for (const line of renderFkOrphanAdvisory(
|
|
132
|
+
result.fkOrphans ?? [],
|
|
133
|
+
ownershipColumns(opts.models ?? []),
|
|
134
|
+
{ verbose: opts.verbose === true },
|
|
135
|
+
)) warn(line);
|
|
119
136
|
switch (result.status) {
|
|
120
137
|
case 'already-applied':
|
|
121
138
|
if (opts.verbose) success(`Already at ${plan.toFingerprint.slice(0, 12)} — nothing to do (idempotent re-apply).`);
|
|
@@ -149,11 +166,47 @@ export function reportApplyResult(
|
|
|
149
166
|
success(lines[lines.length - 1]);
|
|
150
167
|
return;
|
|
151
168
|
}
|
|
169
|
+
case 'validation-pending': {
|
|
170
|
+
// NOT every validation-pending outcome is safe to treat as a success. `pendingIsSafe` is
|
|
171
|
+
// false the moment any outstanding key's cause is a check that was cut off or failed for a
|
|
172
|
+
// reason this command does not recognise — a real operational anomaly, not the withheld-
|
|
173
|
+
// by-design case this status otherwise describes. That still exits non-zero, exactly like
|
|
174
|
+
// any other refusal: a CI pipeline gating a release on this command's exit code must see it.
|
|
175
|
+
if (result.pendingIsSafe !== true) {
|
|
176
|
+
fail(formatApplyRefusal(result.status, result.reason ?? ''));
|
|
177
|
+
process.exit(1);
|
|
178
|
+
}
|
|
179
|
+
// LANDED, not refused: the table and column changes are committed (see the LANDED set
|
|
180
|
+
// below) — only a VALIDATE CONSTRAINT is outstanding on keys the preflight already
|
|
181
|
+
// withheld. Exiting non-zero here would tell a CI pipeline gating on this command's exit
|
|
182
|
+
// code that the apply FAILED, when nothing did: the grant, the clean FKs, everything else
|
|
183
|
+
// landed exactly as planned. That false failure is the whole reason this status exists —
|
|
184
|
+
// undoing it with a non-zero exit here would silently reintroduce the block this feature
|
|
185
|
+
// was built to remove. The receipt below is the same one `applied` prints, minus every
|
|
186
|
+
// statement `unappliedStatements` names — a withheld or failed VALIDATE never ran, so
|
|
187
|
+
// `plan.statements` (still the full, un-rewritten plan) must not be classified as-is or
|
|
188
|
+
// it reads as an applied change that never happened. The pending notice rode in already,
|
|
189
|
+
// via renderFkOrphanAdvisory above.
|
|
190
|
+
if (opts.verbose) {
|
|
191
|
+
success(`Applied, validation PENDING — the target is at ${result.liveFingerprint.slice(0, 12)}. ${formatApplyRefusal(result.status, result.reason ?? '')}`);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const unapplied = new Set(result.unappliedStatements ?? []);
|
|
195
|
+
const executable = classifyGeneratedStatements((plan.statements ?? []).filter((s) => !unapplied.has(s))).executable;
|
|
196
|
+
const { drops, narrowings, strips } = classifyDestructive(executable, {
|
|
197
|
+
...(opts.declaredGrantees ? { declaredGrantees: opts.declaredGrantees } : {}),
|
|
198
|
+
});
|
|
199
|
+
const lines = renderApplyReceipt({ executable, destructive: [...drops, ...narrowings, ...strips], catchUp });
|
|
200
|
+
for (const text of lines.slice(0, -1)) printLine(text);
|
|
201
|
+
success(lines[lines.length - 1]);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
152
204
|
default:
|
|
153
205
|
// Every remaining status is a refusal or a verify fault. TS narrows result.status here to
|
|
154
|
-
// exactly ApplyRefusalStatus, so a future status that is
|
|
155
|
-
//
|
|
156
|
-
// cannot come back. The message is an
|
|
206
|
+
// exactly ApplyRefusalStatus minus 'validation-pending', so a future status that is
|
|
207
|
+
// neither success, pending, nor a known refusal stops this compiling — the fallthrough
|
|
208
|
+
// that let `authz-refused` print nothing and exit 0 cannot come back. The message is an
|
|
209
|
+
// action; the internal cause code stays in schema_log.
|
|
157
210
|
fail(formatApplyRefusal(result.status, result.reason ?? ''));
|
|
158
211
|
process.exit(1);
|
|
159
212
|
}
|
|
@@ -262,9 +315,12 @@ async function catchUpAfterApply(
|
|
|
262
315
|
): Promise<CatchUpReport> {
|
|
263
316
|
if (!LANDED.has(result.status)) return { ok: true, statements: [] };
|
|
264
317
|
if (flags['state-only'] === 'true') return { ok: true, statements: [] };
|
|
318
|
+
// Same exclusion the receipt applies (reportApplyResult): a withheld or failed VALIDATE never
|
|
319
|
+
// ran, so it must not inflate the count a catch-up FAILURE report names as already safe.
|
|
320
|
+
const unapplied = new Set(result.unappliedStatements ?? []);
|
|
265
321
|
const applied = result.status === 'already-applied'
|
|
266
322
|
? 0
|
|
267
|
-
: classifyGeneratedStatements(plan.statements ?? []).executable.length;
|
|
323
|
+
: classifyGeneratedStatements((plan.statements ?? []).filter((s) => !unapplied.has(s))).executable.length;
|
|
268
324
|
return runCatchUp(venue, flags, applied, 'throw');
|
|
269
325
|
}
|
|
270
326
|
|
|
@@ -299,6 +355,27 @@ async function declaredGranteesForReceipt(
|
|
|
299
355
|
}
|
|
300
356
|
}
|
|
301
357
|
|
|
358
|
+
/**
|
|
359
|
+
* The models, for the orphan advisory's row-security cross-reference — and ONLY when there is
|
|
360
|
+
* something to cross-reference, so a clean apply never pays to load a barrel.
|
|
361
|
+
*
|
|
362
|
+
* Silently best-effort, unlike its sibling above. The advisory's own lines are complete without
|
|
363
|
+
* it (the generic heading, the counts, the samples); what is missing is the escalation to the
|
|
364
|
+
* row-security heading. Warning about a degraded sub-heading, next to a notice that is itself
|
|
365
|
+
* advisory, would be noise about noise.
|
|
366
|
+
*/
|
|
367
|
+
async function modelsForAdvisory(
|
|
368
|
+
result: ApplyPlanResult,
|
|
369
|
+
flags: Record<string, string>,
|
|
370
|
+
): Promise<readonly ModelDescriptor[] | undefined> {
|
|
371
|
+
if (!result.fkOrphans?.length) return undefined;
|
|
372
|
+
try {
|
|
373
|
+
return await loadModels(resolveModelsPath(flags.models));
|
|
374
|
+
} catch {
|
|
375
|
+
return undefined;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
302
379
|
/**
|
|
303
380
|
* Print a failed catch-up and exit: the report line, then the cause as it was written.
|
|
304
381
|
*
|
|
@@ -563,10 +640,12 @@ async function applyPlanViaStage(
|
|
|
563
640
|
const catchUp = await catchUpAfterApply(landedResult!, plan, { kind: 'stage', stage: flags.stage }, flags);
|
|
564
641
|
exitOnCatchUpFailure(catchUp);
|
|
565
642
|
const declaredGrantees = await declaredGranteesForReceipt(plan, flags);
|
|
643
|
+
const advisoryModels = await modelsForAdvisory(landedResult!, flags);
|
|
566
644
|
reportApplyResult(landedResult!, plan, {
|
|
567
645
|
verbose: flags.verbose === 'true',
|
|
568
646
|
catchUp: catchUp.statements,
|
|
569
647
|
...(declaredGrantees ? { declaredGrantees } : {}),
|
|
648
|
+
...(advisoryModels ? { models: advisoryModels } : {}),
|
|
570
649
|
});
|
|
571
650
|
}
|
|
572
651
|
|
|
@@ -1065,9 +1144,11 @@ export async function dbApplyCommand(flags: Record<string, string>): Promise<voi
|
|
|
1065
1144
|
|
|
1066
1145
|
exitOnCatchUpFailure(catchUp);
|
|
1067
1146
|
const declaredGrantees = await declaredGranteesForReceipt(plan, flags);
|
|
1147
|
+
const advisoryModels = await modelsForAdvisory(landed!, flags);
|
|
1068
1148
|
reportApplyResult(landed!, plan, {
|
|
1069
1149
|
verbose: flags.verbose === 'true',
|
|
1070
1150
|
catchUp: catchUp.statements,
|
|
1071
1151
|
...(declaredGrantees ? { declaredGrantees } : {}),
|
|
1152
|
+
...(advisoryModels ? { models: advisoryModels } : {}),
|
|
1072
1153
|
});
|
|
1073
1154
|
}
|
|
@@ -71,6 +71,8 @@ import { alterTypeStatementTargets, findAlterTypeDependents, renderAlterTypeRefu
|
|
|
71
71
|
import { compileTableContract } from '../authz-compile';
|
|
72
72
|
import { reportPipelineLastRun } from './pipeline-run';
|
|
73
73
|
import { renderPlanReport } from '../plain-output';
|
|
74
|
+
import { fkOrphanChecks, fkOrphanSql, fkOrphanFindings, checkOrphanRowShape } from '../fk-preflight';
|
|
75
|
+
import { renderFkOrphanAdvisory, ownershipColumns } from '../fk-orphan-advisory';
|
|
74
76
|
import { step, success, fail, info, warn, line as printLine, reserveStdoutForData } from '../output';
|
|
75
77
|
|
|
76
78
|
/**
|
|
@@ -389,6 +391,33 @@ export async function dbPlanCommand(flags: Record<string, string>): Promise<void
|
|
|
389
391
|
else if (verbose) info(line);
|
|
390
392
|
}
|
|
391
393
|
|
|
394
|
+
// WHICH foreign keys this plan would ask to be PROVEN, and whether the rows can prove them.
|
|
395
|
+
//
|
|
396
|
+
// db:plan asks now so the review surface carries it: an operator reading the plan learns
|
|
397
|
+
// that N keys will stay NOT VALID BEFORE they apply, rather than from the apply's outcome.
|
|
398
|
+
// It is the same read the apply runs (one anti-join per key), against the same snapshot the
|
|
399
|
+
// fingerprint was taken from.
|
|
400
|
+
//
|
|
401
|
+
// IT NEVER BLOCKS, and a read that fails costs the plan nothing: minting is read-only, the
|
|
402
|
+
// plan file is already written, and the apply asks the question again with the answer that
|
|
403
|
+
// actually decides anything. A failure here is one line, not an exit.
|
|
404
|
+
//
|
|
405
|
+
// NONE OF IT IS IN THE PLAN FILE. Row counts go stale between minting and applying, and a
|
|
406
|
+
// plan is a content-addressed artifact that must not assert something it cannot still be
|
|
407
|
+
// true days later.
|
|
408
|
+
const orphanChecks = fkOrphanChecks(plan.statements, snapshot);
|
|
409
|
+
if (orphanChecks.length > 0) {
|
|
410
|
+
try {
|
|
411
|
+
const rows = (await runner(fkOrphanSql(orphanChecks))) as unknown;
|
|
412
|
+
checkOrphanRowShape(rows, orphanChecks.length);
|
|
413
|
+
for (const text of renderFkOrphanAdvisory(fkOrphanFindings(rows), ownershipColumns(models), { verbose })) {
|
|
414
|
+
warn(text);
|
|
415
|
+
}
|
|
416
|
+
} catch (err: any) {
|
|
417
|
+
warn(`the foreign-key row check could not run against this target (${err?.message ?? err}) — the plan is unaffected, but it cannot say here whether ${orphanChecks.length} key(s) will validate. db:apply asks again and fails closed if it still cannot.`);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
392
421
|
// The fast-forward rule, warn-only at mint time (db:apply enforces it):
|
|
393
422
|
// a stale checkout mints a legal-looking plan whose edge REVERTS merged
|
|
394
423
|
// work — say so on the review surface, where a human still reads it.
|
package/src/cli/edge-plan.ts
CHANGED
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
|
|
27
27
|
import { createHash } from 'node:crypto';
|
|
28
28
|
import type { ModelDescriptor } from '@everystack/model';
|
|
29
|
-
import type { SchemaSnapshot } from './schema-introspect';
|
|
29
|
+
import type { SchemaSnapshot, TableSchema } from './schema-introspect';
|
|
30
30
|
import type { AuthzContract, SequenceGrantContract, DefaultAclScope } from './authz-contract';
|
|
31
|
-
import { ownSequenceName } from './schema-diff';
|
|
31
|
+
import { ownSequenceName, foreignKeyContentKey } from './schema-diff';
|
|
32
32
|
import { generateMigrationSql, unmodeledTables } from './migration-generate';
|
|
33
33
|
import { compileTableContract } from './authz-compile';
|
|
34
34
|
import { carryUngovernedAuthz, inheritedSequenceGrants, type GrantExemption } from './authz-reconcile';
|
|
@@ -73,8 +73,19 @@ export const PLAN_VERSION = 2;
|
|
|
73
73
|
* stage would skip the preflight and run the VALIDATE inside the edge — a silently dropped gate
|
|
74
74
|
* and a different failure mode, both of which the BUMP RULE covers. Peers refuse until the stage
|
|
75
75
|
* is redeployed.
|
|
76
|
+
*
|
|
77
|
+
* v5 (validation-pending stops refusing the whole edge): `ApplyPlanResult` gained `pendingIsSafe`
|
|
78
|
+
* and `unappliedStatements` — the CLIENT now decides, from `pendingIsSafe`, whether a
|
|
79
|
+
* `validation-pending` result exits 0 (a withheld or ordinary-orphan cause) or 1 (a cut-off or
|
|
80
|
+
* unrecognised cause, a real anomaly). A v4 ops Lambda's `executeApplyPlan` never sets
|
|
81
|
+
* `pendingIsSafe` at all, so a v5 client reading its response would see it absent, treat every
|
|
82
|
+
* pending result as UNSAFE by the same fail-closed rule that protects the in-version case, and
|
|
83
|
+
* exit 1 on a perfectly safe withheld key — the exact false CI failure this whole feature exists
|
|
84
|
+
* to remove, reintroduced silently across a version skew instead of refused cleanly. The BUMP
|
|
85
|
+
* RULE covers exactly this: a field the CLIENT now depends on to decide something is part of the
|
|
86
|
+
* contract. Peers refuse until the stage is redeployed.
|
|
76
87
|
*/
|
|
77
|
-
export const APPLY_CONTRACT_VERSION =
|
|
88
|
+
export const APPLY_CONTRACT_VERSION = 5;
|
|
78
89
|
|
|
79
90
|
/** Classification counts (brick 9, decision 11): destructive = drops + narrowings + strips. */
|
|
80
91
|
export interface PlanClassification {
|
|
@@ -225,6 +236,36 @@ export function predictLiveFingerprint(
|
|
|
225
236
|
return predictLiveState(models, snapshot, contract, opts).hash;
|
|
226
237
|
}
|
|
227
238
|
|
|
239
|
+
/**
|
|
240
|
+
* One declared table with its `validate: false` foreign keys' VALIDITY taken from live.
|
|
241
|
+
*
|
|
242
|
+
* A key the model declared `validate: false` compiles to `notValid: true`, and that is right for
|
|
243
|
+
* a from-scratch build: nothing validates it, so the built key really is NOT VALID. But the
|
|
244
|
+
* PREDICTED endpoint is not a build, it is "the live database after this edge" — and the edge
|
|
245
|
+
* emits nothing at all for such a key when the live one already content-matches (see
|
|
246
|
+
* `diffConstraints`: a live NOT VALID key needs no statement, and a live FULLY-VALID key is the
|
|
247
|
+
* accept-either case). Predicting NOT VALID over a key the database has already validated would
|
|
248
|
+
* make the plan's own verify-after fail on an edge that touched nothing, on every apply, forever.
|
|
249
|
+
*
|
|
250
|
+
* So the prediction copies what the key IS. Untouched for every other key: absent live table,
|
|
251
|
+
* absent live counterpart, or a declaration that never said `validate: false` — those predict
|
|
252
|
+
* exactly what they always did.
|
|
253
|
+
*/
|
|
254
|
+
function carryFkValidityThrough(declared: TableSchema, live: TableSchema | undefined): TableSchema {
|
|
255
|
+
if (!live || !declared.foreignKeys.some((fk) => fk.notValid)) return declared;
|
|
256
|
+
const liveByContent = new Map(live.foreignKeys.map((fk) => [foreignKeyContentKey(fk), fk]));
|
|
257
|
+
return {
|
|
258
|
+
...declared,
|
|
259
|
+
foreignKeys: declared.foreignKeys.map((fk) => {
|
|
260
|
+
if (!fk.notValid) return fk;
|
|
261
|
+
const counterpart = liveByContent.get(foreignKeyContentKey(fk));
|
|
262
|
+
if (!counterpart || counterpart.notValid) return fk;
|
|
263
|
+
const { notValid: _dropped, ...rest } = fk;
|
|
264
|
+
return rest;
|
|
265
|
+
}),
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
228
269
|
/**
|
|
229
270
|
* The predicted endpoint as a full {@link Fingerprint} (hash AND canonical form). The hash is
|
|
230
271
|
* the plan's `to` and the declares-test; the canonical form lets a MISMATCH report name WHICH
|
|
@@ -268,16 +309,6 @@ export function predictLiveState(
|
|
|
268
309
|
!declaredTables.has(table) && !pendingRenameSources.has(table) && !pendingMoveSources.has(table);
|
|
269
310
|
const declaredEnumList = declared.snapshot.enums ?? [];
|
|
270
311
|
const declaredEnums = new Set(declaredEnumList.map((e) => e.name));
|
|
271
|
-
const mergedSnapshot: SchemaSnapshot = {
|
|
272
|
-
tables: [
|
|
273
|
-
...declared.snapshot.tables,
|
|
274
|
-
...snapshot.tables.filter((t) => ridesThrough(t.table)),
|
|
275
|
-
],
|
|
276
|
-
enums: [
|
|
277
|
-
...declaredEnumList,
|
|
278
|
-
...(snapshot.enums ?? []).filter((e) => !declaredEnums.has(e.name)),
|
|
279
|
-
],
|
|
280
|
-
};
|
|
281
312
|
// The live counterpart of a declared table: its rename or move source when the edge brings it
|
|
282
313
|
// in under a new name, else its own name. Grants, policies and owned sequences survive RENAME
|
|
283
314
|
// TABLE and SET SCHEMA alike (they attach by OID), so the foreign authz carries across both.
|
|
@@ -290,6 +321,17 @@ export function predictLiveState(
|
|
|
290
321
|
!currentNames.has(declaredTable) && currentNames.has(priorNameOf[declaredTable] ?? '')
|
|
291
322
|
? priorNameOf[declaredTable]
|
|
292
323
|
: declaredTable;
|
|
324
|
+
const liveSnapshotByTable = new Map(snapshot.tables.map((t) => [t.table, t]));
|
|
325
|
+
const mergedSnapshot: SchemaSnapshot = {
|
|
326
|
+
tables: [
|
|
327
|
+
...declared.snapshot.tables.map((t) => carryFkValidityThrough(t, liveSnapshotByTable.get(liveNameFor(t.table)))),
|
|
328
|
+
...snapshot.tables.filter((t) => ridesThrough(t.table)),
|
|
329
|
+
],
|
|
330
|
+
enums: [
|
|
331
|
+
...declaredEnumList,
|
|
332
|
+
...(snapshot.enums ?? []).filter((e) => !declaredEnums.has(e.name)),
|
|
333
|
+
],
|
|
334
|
+
};
|
|
293
335
|
const liveByTable = new Map(contract.tables.map((t) => [t.table, t]));
|
|
294
336
|
const declaredColumnsByTable = new Map(
|
|
295
337
|
declared.snapshot.tables.map((t) => [t.table, new Set(t.columns.map((c) => c.name))]),
|