@everystack/cli 0.4.100 → 0.4.103

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +112 -0
  2. package/package.json +3 -3
  3. package/src/cli/authz-baseline.ts +8 -4
  4. package/src/cli/authz-compile.ts +91 -14
  5. package/src/cli/authz-contract.ts +29 -14
  6. package/src/cli/authz-derive.ts +284 -30
  7. package/src/cli/authz-reconcile.ts +6 -4
  8. package/src/cli/commands/db-check.ts +40 -0
  9. package/src/cli/commands/db-generate.ts +10 -9
  10. package/src/cli/commands/db-plan.ts +32 -13
  11. package/src/cli/commands/db-pull.ts +155 -63
  12. package/src/cli/commands/db-reconcile.ts +22 -3
  13. package/src/cli/commands/db-sync.ts +6 -5
  14. package/src/cli/commands/db.ts +91 -8
  15. package/src/cli/commands/update.ts +3 -2
  16. package/src/cli/db-build.ts +1 -0
  17. package/src/cli/declared-derived.ts +3 -1
  18. package/src/cli/declared-diff.ts +5 -1
  19. package/src/cli/derived-apply.ts +116 -10
  20. package/src/cli/derived-grants.ts +4 -2
  21. package/src/cli/derived-introspect.ts +23 -3
  22. package/src/cli/derived-plan.ts +37 -1
  23. package/src/cli/derived-render.ts +33 -34
  24. package/src/cli/edge-plan.ts +92 -9
  25. package/src/cli/generated-artifacts.ts +22 -0
  26. package/src/cli/generated-names.ts +108 -0
  27. package/src/cli/host-url.ts +48 -27
  28. package/src/cli/index-lock.ts +65 -0
  29. package/src/cli/index.ts +5 -5
  30. package/src/cli/lifecycle-canon.ts +37 -0
  31. package/src/cli/lifecycle-derived.ts +787 -0
  32. package/src/cli/lifecycle-pull.ts +252 -0
  33. package/src/cli/lifecycle-verify.ts +238 -0
  34. package/src/cli/migration-compile.ts +11 -5
  35. package/src/cli/migration-generate.ts +24 -3
  36. package/src/cli/model-api.ts +9 -0
  37. package/src/cli/model-map.ts +255 -0
  38. package/src/cli/model-render.ts +232 -76
  39. package/src/cli/plain-output.ts +211 -10
  40. package/src/cli/runbook/model-section.ts +8 -5
  41. package/src/cli/schema-compile.ts +64 -18
  42. package/src/cli/schema-diff.ts +166 -31
  43. package/src/cli/schema-fingerprint.ts +40 -6
  44. package/src/cli/schema-introspect.ts +97 -4
  45. package/src/cli/schema-source.ts +40 -9
  46. package/src/cli/schema-swap.ts +3 -2
  47. package/src/cli/state-apply.ts +10 -4
  48. package/src/cli/ts-template.ts +52 -0
@@ -74,8 +74,14 @@ export type SchemaChange =
74
74
  * from `from`, so it can only ever emit the one statement that applies.
75
75
  */
76
76
  | { kind: 'setIdentity'; table: string; column: string; identity: 'always' | 'byDefault' | null; from: 'always' | 'byDefault' | null }
77
- /** A type change → real `ALTER COLUMN ... TYPE`. `risk` decides USING + a data-loss warning. */
78
- | { kind: 'alterType'; table: string; column: string; from: string; to: string; risk: TypeChangeRisk }
77
+ /**
78
+ * A type change → real `ALTER COLUMN ... TYPE`. `risk` decides USING + a data-loss warning.
79
+ * `toEnum`: the target is a declared enum. PostgreSQL has no cast between two enum types, so
80
+ * the USING goes through text. `dropDefault`: the live column has a default, which cannot be
81
+ * cast automatically either, so it is dropped first; a `setDefault` after the retype restores
82
+ * the declared one. Both are absent, never false, so every other retype keeps its shape.
83
+ */
84
+ | { kind: 'alterType'; table: string; column: string; from: string; to: string; risk: TypeChangeRisk; toEnum?: true; dropDefault?: true }
79
85
  | { kind: 'addConstraint'; table: string; constraint: ConstraintSpec; note?: string }
80
86
  /**
81
87
  * A foreign key that is already installed on the live table but has never been checked
@@ -86,9 +92,23 @@ export type SchemaChange =
86
92
  * This is what the state after an interrupted two-step add re-plans to — VALIDATE only.
87
93
  */
88
94
  | { kind: 'validateConstraint'; table: string; name: string }
89
- | { kind: 'dropConstraint'; table: string; name: string }
90
- | { kind: 'addPrimaryKey'; table: string; columns: string[] }
91
- /** A PK change other than first-time add — a notice (the drop needs its constraint name). */
95
+ /**
96
+ * `replacesPrimaryKey`: this is the LIVE primary key, dropped so the declared one (these
97
+ * columns) can take its place. The structured marker for a key replacement — it exempts the
98
+ * drop from the held-removal gate (an `addPrimaryKey` follows it in the same edge) and it is
99
+ * what the emitter writes the `-- WARNING: primary key …` note from. Absent on every other drop.
100
+ */
101
+ | { kind: 'dropConstraint'; table: string; name: string; replacesPrimaryKey?: string[] }
102
+ /**
103
+ * `name` is set when the key REPLACES a live one, and is what a from-scratch build names it
104
+ * ({@link primaryKeyConstraintName}). A first-time add carries none and PostgreSQL names it.
105
+ */
106
+ | { kind: 'addPrimaryKey'; table: string; columns: string[]; name?: string }
107
+ /**
108
+ * A PK change the differ cannot plan: the live key's name is unknown (a snapshot read before
109
+ * introspection carried it), so there is nothing to drop it by. A notice — and db:plan refuses
110
+ * to mint over one, because the fingerprint sees the key the plan would leave unchanged.
111
+ */
92
112
  | { kind: 'primaryKeyChange'; table: string; from: string[]; to: string[] }
93
113
  /** A `renamedFrom` marker satisfied: old column present, new absent → preserve the data. */
94
114
  | { kind: 'renameColumn'; table: string; from: string; to: string }
@@ -227,6 +247,67 @@ function bareTable(table: string): string {
227
247
  return table.replace(/^[^.]+\./, '');
228
248
  }
229
249
 
250
+ /** The longest identifier PostgreSQL keeps (NAMEDATALEN - 1), in bytes. */
251
+ const MAX_IDENTIFIER_BYTES = 63;
252
+
253
+ /** `s` clipped to at most `max` UTF-8 bytes, never splitting a character. */
254
+ function clipBytes(s: string, max: number): string {
255
+ const enc = new TextEncoder();
256
+ let out = s;
257
+ while (enc.encode(out).length > max) out = out.slice(0, -1);
258
+ return out;
259
+ }
260
+
261
+ /**
262
+ * The name a from-scratch build gives a table's primary key — ONE definition, read by the
263
+ * greenfield compiler and by the differ when it replaces a live key, so an evolved database and
264
+ * a fresh build of the same models name the key the same way.
265
+ *
266
+ * - one column: the compiler writes it inline (`"id" uuid PRIMARY KEY`) and PostgreSQL names
267
+ * it `<table>_pkey`, clipping the TABLE part (never the suffix) to fit 63 bytes.
268
+ * - several columns: the compiler names the table constraint `<table>_<cols>_pk`, the way
269
+ * drizzle does.
270
+ *
271
+ * `table` is the bare table name, never schema-qualified.
272
+ */
273
+ export function primaryKeyConstraintName(table: string, columns: readonly string[]): string {
274
+ if (columns.length > 1) return `${table}_${columns.join('_')}_pk`;
275
+ const suffix = '_pkey';
276
+ return `${clipBytes(table, MAX_IDENTIFIER_BYTES - suffix.length)}${suffix}`;
277
+ }
278
+
279
+ /**
280
+ * The note a primary key REPLACEMENT carries on its DROP, and its parser. Rendered from the
281
+ * change's structured `replacesPrimaryKey` marker and read back by the plain plan report for its
282
+ * `⚠ CONSTRAINT` line; the two live side by side so they cannot drift, and the parser accepts
283
+ * exactly the line the renderer writes. `WARNING`, not `NOTICE`: the statement under it runs
284
+ * (NOTICE is the reserved prefix of a pure comment).
285
+ */
286
+ export interface PrimaryKeyReplacement {
287
+ /** Schema-qualified, as the snapshot spells it (`public.likes`). */
288
+ table: string;
289
+ /** The live key being dropped. */
290
+ name: string;
291
+ /** The declared key's columns. */
292
+ columns: string[];
293
+ }
294
+
295
+ export function renderPrimaryKeyReplacement(r: PrimaryKeyReplacement): string {
296
+ return `-- WARNING: primary key ${quote(r.name)} on ${r.table} is replaced by (${colList(r.columns)}).`;
297
+ }
298
+
299
+ // Assumes a whitespace-free table name (`\S+`); the plan file carries statements only, so this note is the one record.
300
+ const PK_REPLACEMENT_RE = /^-- WARNING: primary key "([^"]*)" on (\S+) is replaced by \(((?:"[^"]*"(?:, )?)+)\)\.$/;
301
+
302
+ /** The replacement a statement's note records, or null when it carries none. */
303
+ export function parsePrimaryKeyReplacement(statement: string): PrimaryKeyReplacement | null {
304
+ for (const line of statement.split('\n')) {
305
+ const m = PK_REPLACEMENT_RE.exec(line.trim());
306
+ if (m) return { name: m[1], table: m[2], columns: [...m[3].matchAll(/"([^"]*)"/g)].map((c) => c[1]) };
307
+ }
308
+ return null;
309
+ }
310
+
230
311
  /** `CREATE SEQUENCE` DDL for a standalone sequence — emitted BEFORE any table, so a
231
312
  * column's `defaultSql(nextval(…))` can draw on it from the first CREATE. */
232
313
  export function sequenceCreateSql(seq: SequenceSchema): string {
@@ -413,8 +494,8 @@ function parenOnlyNote(kind: string, name: string, liveName: string, liveExpr: s
413
494
  /**
414
495
  * Diff desired (compiled Model) against current (introspected DB) into an ordered,
415
496
  * dependency-safe change set. The order each change is emitted in is the order it must
416
- * be applied: new tables, then new columns, then column alters, then dropped
417
- * constraints, then added constraints, then dropped columns, then dropped tables — so a
497
+ * be applied: new tables, then new columns, then dropped constraints and primary keys,
498
+ * then column alters, then added constraints, then dropped columns, then dropped tables — so a
418
499
  * replaced constraint never clashes with its old self and a column is never dropped out
419
500
  * from under a constraint that still references it.
420
501
  */
@@ -427,6 +508,10 @@ export function diffSchema(desired: SchemaSnapshot, current: SchemaSnapshot, ren
427
508
  const alters: SchemaChange[] = [];
428
509
  const dropConstraints: SchemaChange[] = [];
429
510
  const addConstraints: SchemaChange[] = [];
511
+ /** Primary keys: the live key's drop after every other constraint drop (a foreign key on it
512
+ * goes first), the declared key's add before every other add (a foreign key may need it). */
513
+ const dropPrimaryKeys: SchemaChange[] = [];
514
+ const addPrimaryKeys: SchemaChange[] = [];
430
515
  const createIndexes: SchemaChange[] = [];
431
516
  /** Catalog-only renames — indexes (B3) and FK constraints (A8). */
432
517
  const catalogRenames: SchemaChange[] = [];
@@ -511,6 +596,7 @@ export function diffSchema(desired: SchemaSnapshot, current: SchemaSnapshot, ren
511
596
  currentByName.set(to, { ...source, table: to });
512
597
  }
513
598
 
599
+ const declaredEnumNames = new Set((desired.enums ?? []).map((e) => e.name));
514
600
  for (const d of desired.tables) {
515
601
  const c = currentByName.get(d.table);
516
602
  if (!c) {
@@ -527,13 +613,14 @@ export function diffSchema(desired: SchemaSnapshot, current: SchemaSnapshot, ren
527
613
  for (const ix of d.indexes) createIndexes.push(createIndexChange(d.table, ix));
528
614
  continue;
529
615
  }
530
- const cd = diffColumns(d, c, renames[d.table] ?? {});
616
+ const cd = diffColumns(d, c, renames[d.table] ?? {}, declaredEnumNames);
531
617
  renameColumns.push(...cd.renameColumns);
532
618
  addColumns.push(...cd.addColumns);
533
619
  alters.push(...cd.alters);
534
620
  dropColumns.push(...cd.dropColumns);
535
621
  notices.push(...cd.notices);
536
622
  diffConstraints(d, c, dropConstraints, addConstraints, alters, catalogRenames);
623
+ diffPrimaryKey(d, c, dropPrimaryKeys, addPrimaryKeys, alters);
537
624
  diffIndexes(d, c, createIndexes, dropIndexes, catalogRenames, redefineIndexes);
538
625
  }
539
626
 
@@ -588,12 +675,15 @@ export function diffSchema(desired: SchemaSnapshot, current: SchemaSnapshot, ren
588
675
  ...e.creates, ...e.adds, ...sequenceCreates,
589
676
  ...moveTables,
590
677
  ...renameTables,
591
- ...creates, ...renameColumns, ...addColumns, ...alters, ...trailingDefaults,
678
+ ...creates, ...renameColumns, ...addColumns,
592
679
  // Renames are catalog-only and precede the creates, so a declared name freed by renaming an
593
680
  // existing object is never claimed by a fresh CREATE first.
594
681
  // redefineIndexes immediately precede createIndexes: PostgreSQL has no CREATE OR REPLACE
595
682
  // INDEX, so the old one must be gone before its replacement claims the name.
596
- ...catalogRenames, ...dropConstraints, ...addConstraints, ...redefineIndexes, ...createIndexes,
683
+ // A replaced primary key: the old one is dropped after every other constraint drop and the
684
+ // new one added after column alters and before every other add — so a partial unique index or a foreign key that
685
+ // needs the new key comes after it, and nothing still references the old one when it goes.
686
+ ...catalogRenames, ...dropConstraints, ...dropPrimaryKeys, ...alters, ...trailingDefaults, ...addPrimaryKeys, ...addConstraints, ...redefineIndexes, ...createIndexes,
597
687
  ...dropColumns, ...dropIndexes, ...dropTables,
598
688
  ...e.drops, ...notices, ...e.notices, ...sequenceNotices,
599
689
  ];
@@ -746,10 +836,21 @@ interface ColumnDiff {
746
836
  }
747
837
 
748
838
  /** Column-level type/nullability/default changes between a renamed-or-matched column and its source. */
749
- function columnAlters(table: string, desired: ColumnSchema, source: ColumnSchema, into: SchemaChange[]): void {
750
- if (desired.type !== source.type) into.push({ kind: 'alterType', table, column: desired.name, from: source.type, to: desired.type, risk: classifyTypeChange(source.type, desired.type) });
839
+ function columnAlters(table: string, desired: ColumnSchema, source: ColumnSchema, into: SchemaChange[], enumNames: ReadonlySet<string>): void {
840
+ // A retype onto an enum drops the live default first (see `alterType`), so the declared
841
+ // default must be set again even when it reads the same on both sides.
842
+ const dropsDefault = desired.type !== source.type && enumNames.has(desired.type) && source.default != null;
843
+ if (desired.type !== source.type) {
844
+ into.push({
845
+ kind: 'alterType', table, column: desired.name, from: source.type, to: desired.type, risk: classifyTypeChange(source.type, desired.type),
846
+ ...(enumNames.has(desired.type) ? { toEnum: true as const } : {}),
847
+ ...(dropsDefault ? { dropDefault: true as const } : {}),
848
+ });
849
+ }
751
850
  if (desired.notNull !== source.notNull) into.push({ kind: 'setNotNull', table, column: desired.name, notNull: desired.notNull });
752
- if (normalizeDefault(desired.default) !== normalizeDefault(source.default)) into.push({ kind: 'setDefault', table, column: desired.name, expr: desired.default });
851
+ if (normalizeDefault(desired.default) !== normalizeDefault(source.default) || (dropsDefault && desired.default != null)) {
852
+ into.push({ kind: 'setDefault', table, column: desired.name, expr: desired.default });
853
+ }
753
854
  // Emitted AFTER setDefault deliberately: Postgres refuses ADD GENERATED on a column that
754
855
  // still holds a default, which is exactly the shape of a serial → identity migration.
755
856
  if ((desired.identity ?? null) !== (source.identity ?? null)) {
@@ -757,7 +858,7 @@ function columnAlters(table: string, desired: ColumnSchema, source: ColumnSchema
757
858
  }
758
859
  }
759
860
 
760
- function diffColumns(d: TableSchema, c: TableSchema, tableRenames: Record<string, string>): ColumnDiff {
861
+ function diffColumns(d: TableSchema, c: TableSchema, tableRenames: Record<string, string>, enumNames: ReadonlySet<string>): ColumnDiff {
761
862
  const out: ColumnDiff = { renameColumns: [], addColumns: [], alters: [], dropColumns: [], notices: [] };
762
863
  const currentByName = new Map(c.columns.map((col) => [col.name, col]));
763
864
  const desiredNames = new Set(d.columns.map((col) => col.name));
@@ -770,7 +871,7 @@ function diffColumns(d: TableSchema, c: TableSchema, tableRenames: Record<string
770
871
  if (cur) {
771
872
  // New name already present. A marker here means the rename is already applied → inert.
772
873
  if (oldName) out.notices.push({ kind: 'renameSatisfied', table: d.table, column: col.name, from: oldName });
773
- columnAlters(d.table, col, cur, out.alters);
874
+ columnAlters(d.table, col, cur, out.alters, enumNames);
774
875
  continue;
775
876
  }
776
877
 
@@ -779,7 +880,7 @@ function diffColumns(d: TableSchema, c: TableSchema, tableRenames: Record<string
779
880
  if (oldName && source) {
780
881
  out.renameColumns.push({ kind: 'renameColumn', table: d.table, from: oldName, to: col.name });
781
882
  consumedByRename.add(oldName);
782
- columnAlters(d.table, col, source, out.alters); // a rename may also change type/default/nullability
883
+ columnAlters(d.table, col, source, out.alters, enumNames); // a rename may also change type/default/nullability
783
884
  } else if (oldName) {
784
885
  // Marker points at a column the database doesn't have — fall back to an honest add.
785
886
  out.addColumns.push({ kind: 'addColumn', table: d.table, column: col });
@@ -931,15 +1032,34 @@ function diffConstraints(d: TableSchema, c: TableSchema, dropConstraints: Schema
931
1032
  }
932
1033
 
933
1034
  diffChecks(d, c, dropConstraints, addConstraints, renames);
1035
+ }
934
1036
 
935
- // Primary key: a first-time add is safe; any other change needs the existing
936
- // constraint's name to drop it (which the snapshot discards), so it is surfaced.
937
- const dPk = d.primaryKey.join(',');
938
- const cPk = c.primaryKey.join(',');
939
- if (dPk !== cPk) {
940
- if (c.primaryKey.length === 0) addConstraints.push({ kind: 'addPrimaryKey', table: d.table, columns: d.primaryKey });
941
- else alters.push({ kind: 'primaryKeyChange', table: d.table, from: c.primaryKey, to: d.primaryKey });
1037
+ /**
1038
+ * Primary key. Compared by columns only — the name is not state, so a live key under another
1039
+ * name with the same columns is no change at all.
1040
+ *
1041
+ * - first-time add: `ADD PRIMARY KEY`, PostgreSQL names it.
1042
+ * - removed (declared has none): a plain drop of the live key, held like any other removal.
1043
+ * - replaced: drop the live key BY ITS LIVE NAME, add the declared one under the name a
1044
+ * from-scratch build gives it. Never CASCADE: a foreign key on the old key fails the apply.
1045
+ * - the live name is unknown (an old snapshot): a `primaryKeyChange` notice, never a guess.
1046
+ */
1047
+ function diffPrimaryKey(d: TableSchema, c: TableSchema, drops: SchemaChange[], adds: SchemaChange[], alters: SchemaChange[]): void {
1048
+ if (d.primaryKey.join(',') === c.primaryKey.join(',')) return;
1049
+ if (c.primaryKey.length === 0) {
1050
+ adds.push({ kind: 'addPrimaryKey', table: d.table, columns: d.primaryKey });
1051
+ return;
1052
+ }
1053
+ if (!c.primaryKeyName) {
1054
+ alters.push({ kind: 'primaryKeyChange', table: d.table, from: c.primaryKey, to: d.primaryKey });
1055
+ return;
1056
+ }
1057
+ if (d.primaryKey.length === 0) {
1058
+ drops.push({ kind: 'dropConstraint', table: d.table, name: c.primaryKeyName });
1059
+ return;
942
1060
  }
1061
+ drops.push({ kind: 'dropConstraint', table: d.table, name: c.primaryKeyName, replacesPrimaryKey: d.primaryKey });
1062
+ adds.push({ kind: 'addPrimaryKey', table: d.table, columns: d.primaryKey, name: primaryKeyConstraintName(bareTable(d.table), d.primaryKey) });
943
1063
  }
944
1064
 
945
1065
  // ---------------------------------------------------------------------------
@@ -947,6 +1067,11 @@ function diffConstraints(d: TableSchema, c: TableSchema, dropConstraints: Schema
947
1067
  // ---------------------------------------------------------------------------
948
1068
 
949
1069
  const quote = (name: string): string => `"${name}"`;
1070
+ /** A type identity — `post_status` or `analytics.post_status` — quoted part by part. */
1071
+ const quoteType = (name: string): string => {
1072
+ const dot = name.indexOf('.');
1073
+ return dot === -1 ? quote(name) : `${quote(name.slice(0, dot))}.${quote(name.slice(dot + 1))}`;
1074
+ };
950
1075
  const colList = (cols: string[]): string => cols.map(quote).join(', ');
951
1076
  /** A single-quoted enum value literal, doubling any embedded quote. */
952
1077
  const enumLiteral = (value: string): string => `'${value.replace(/'/g, "''")}'`;
@@ -1110,12 +1235,15 @@ function emitOne(change: SchemaChange, opts: EmitOptions): string | string[] {
1110
1235
  }
1111
1236
  case 'alterType': {
1112
1237
  const setType = `ALTER TABLE ${change.table} ALTER COLUMN ${quote(change.column)} SET DATA TYPE ${change.to}`;
1113
- if (change.risk === 'safe') return `${setType};`;
1114
- const ddl = `${setType} USING ${quote(change.column)}::${change.to};`;
1238
+ if (change.risk === 'safe' && !change.toEnum) return `${setType};`;
1239
+ const ddl = `${setType} USING ${quote(change.column)}::${change.toEnum ? 'text::' : ''}${change.to};`;
1115
1240
  const warn = change.risk === 'lossy'
1116
1241
  ? `-- WARNING: ${change.from} → ${change.to} on column ${quote(change.column)} (${change.table}) may truncate or lose data; review before running.`
1117
1242
  : `-- WARNING: ${change.from} → ${change.to} on column ${quote(change.column)} (${change.table}) casts with USING and may FAIL on rows that don't convert; review before running.`;
1118
- return `${warn}\n${ddl}`;
1243
+ const retype = change.risk === 'safe' ? ddl : `${warn}\n${ddl}`;
1244
+ return change.dropDefault
1245
+ ? [`ALTER TABLE ${change.table} ALTER COLUMN ${quote(change.column)} DROP DEFAULT;`, retype]
1246
+ : retype;
1119
1247
  }
1120
1248
  case 'addConstraint': {
1121
1249
  const [first, ...rest] = addConstraintSql(change.table, change.constraint);
@@ -1126,9 +1254,16 @@ function emitOne(change: SchemaChange, opts: EmitOptions): string | string[] {
1126
1254
  case 'validateConstraint':
1127
1255
  return validateConstraintSql(change.table, change.name);
1128
1256
  case 'dropConstraint':
1129
- return `ALTER TABLE ${change.table} DROP CONSTRAINT IF EXISTS ${quote(change.name)};`;
1257
+ // A replaced primary key drops WITHOUT `IF EXISTS`: a key renamed between plan and apply
1258
+ // fails on the DROP itself, never silently skips it and fails later on the ADD.
1259
+ return change.replacesPrimaryKey
1260
+ ? `${renderPrimaryKeyReplacement({ table: change.table, name: change.name, columns: change.replacesPrimaryKey })}\n` +
1261
+ `ALTER TABLE ${change.table} DROP CONSTRAINT ${quote(change.name)};`
1262
+ : `ALTER TABLE ${change.table} DROP CONSTRAINT IF EXISTS ${quote(change.name)};`;
1130
1263
  case 'addPrimaryKey':
1131
- return `ALTER TABLE ${change.table} ADD PRIMARY KEY (${colList(change.columns)});`;
1264
+ return change.name
1265
+ ? `ALTER TABLE ${change.table} ADD CONSTRAINT ${quote(change.name)} PRIMARY KEY (${colList(change.columns)});`
1266
+ : `ALTER TABLE ${change.table} ADD PRIMARY KEY (${colList(change.columns)});`;
1132
1267
  case 'primaryKeyChange':
1133
1268
  return `-- NOTICE: primary key on ${change.table} changed (${change.from.join(', ')} → ${change.to.join(', ')}); dropping a primary key needs its constraint name — regenerate this change manually.`;
1134
1269
  case 'renameColumn':
@@ -1157,11 +1292,11 @@ function emitOne(change: SchemaChange, opts: EmitOptions): string | string[] {
1157
1292
  case 'renameHint':
1158
1293
  return `-- NOTICE: dropping ${quote(change.from)} and adding ${quote(change.to)} on ${change.table}; if this is a rename, replace those two with: ALTER TABLE ${change.table} RENAME COLUMN ${quote(change.from)} TO ${quote(change.to)}; (or mark the field .renamedFrom('${change.from}')).`;
1159
1294
  case 'createType':
1160
- return `CREATE TYPE ${quote(change.name)} AS ENUM (${change.values.map(enumLiteral).join(', ')});`;
1295
+ return `CREATE TYPE ${quoteType(change.name)} AS ENUM (${change.values.map(enumLiteral).join(', ')});`;
1161
1296
  case 'addEnumValue':
1162
- return `-- WARNING: ADD VALUE to enum ${quote(change.type)} cannot run inside a transaction on PostgreSQL < 12, and the new value can't be used until the statement commits; apply it on its own.\nALTER TYPE ${quote(change.type)} ADD VALUE ${enumLiteral(change.value)};`;
1297
+ return `-- WARNING: ADD VALUE to enum ${quoteType(change.type)} cannot run inside a transaction on PostgreSQL < 12, and the new value can't be used until the statement commits; apply it on its own.\nALTER TYPE ${quoteType(change.type)} ADD VALUE ${enumLiteral(change.value)};`;
1163
1298
  case 'dropType':
1164
- return `DROP TYPE ${quote(change.name)};`;
1299
+ return `DROP TYPE ${quoteType(change.name)};`;
1165
1300
  case 'createIndex': {
1166
1301
  const unique = change.unique ? 'UNIQUE ' : '';
1167
1302
  const using = change.using ? ` USING ${change.using}` : '';
@@ -53,7 +53,8 @@
53
53
  import { createHash } from 'node:crypto';
54
54
  import { canonicalAuthz } from './authz-canonical';
55
55
  import type { ModelDescriptor, SequenceDescriptor } from '@everystack/model';
56
- import type { SchemaSnapshot, TableSchema } from './schema-introspect';
56
+ import { assertExternalTargets, lifecycleDependentIndexes, relationDeleteActions } from '@everystack/model';
57
+ import type { EnumType, SchemaSnapshot, TableSchema } from './schema-introspect';
57
58
  import type { AuthzContract, TableContract, SequenceGrantContract } from './authz-contract';
58
59
  import { compileTableSchema, compileEnums, compileSequences } from './schema-compile';
59
60
  import { compileTableContract } from './authz-compile';
@@ -340,6 +341,27 @@ export function schemaOf(qualifiedName: string): string {
340
341
  return dot === -1 ? 'public' : qualifiedName.slice(0, dot);
341
342
  }
342
343
 
344
+ /**
345
+ * `stats.status` → `status`; a bare (public) name is returned as is.
346
+ * Live-side hash inputs are unchanged. The models side now also strips a qualifier a model
347
+ * declared (`field.enum('stats.status', …)`), which never converged against live anyway.
348
+ */
349
+ const bareEnumName = (name: string): string => (schemaOf(name) === 'public' ? name : name.slice(name.indexOf('.') + 1));
350
+
351
+ /**
352
+ * The enum list every canon hashes: bare names, in the order the introspection read used to
353
+ * give them (bare name, then schema). Two schemas holding the same bare name still hash both,
354
+ * as before, and in a fixed order.
355
+ */
356
+ function bareEnums(enums: readonly EnumType[]): EnumType[] {
357
+ return [...enums]
358
+ .sort((a, b) => {
359
+ const [an, bn, as, bs] = [bareEnumName(a.name), bareEnumName(b.name), schemaOf(a.name), schemaOf(b.name)];
360
+ return an < bn ? -1 : an > bn ? 1 : as < bs ? -1 : as > bs ? 1 : 0;
361
+ })
362
+ .map((e) => ({ ...e, name: bareEnumName(e.name) }));
363
+ }
364
+
343
365
  export interface CanonicalizeOptions {
344
366
  /** Restrict the state to these schemas — the content address of ONE schema (e.g. a
345
367
  * schema-scoped artifact) instead of the whole database. Omitted = every schema. */
@@ -376,12 +398,15 @@ export function canonicalizeState(
376
398
  const keepTable = (name: string) => inScope === null || inScope.has(schemaOf(name));
377
399
 
378
400
  const tables = snapshot.tables.filter((t) => keepTable(t.table));
379
- // Enums carry no schema in the descriptor (keyed by bare name, like the model side).
401
+ // The canon keys enums by BARE name. The snapshot now qualifies a non-public enum
402
+ // (`stats.status`) so the diff cannot mistake it for a public one; the canon strips that
403
+ // back off, so every hash input is exactly what it was. Keying the canon by the qualified
404
+ // identity is a canon change, and needs a FINGERPRINT_VERSION bump of its own.
380
405
  // Scope them by USE: an enum is in-scope when a kept table has a column of that type
381
406
  // (the type may be qualified under canonical introspection — `stats.status` — so match
382
407
  // on the bare tail). This keeps the exact set the scoped models declare.
383
408
  const usedEnum = new Set<string>();
384
- for (const t of tables) for (const c of t.columns) usedEnum.add(schemaOf(c.type) === 'public' ? c.type : c.type.slice(c.type.indexOf('.') + 1));
409
+ for (const t of tables) for (const c of t.columns) usedEnum.add(bareEnumName(c.type));
385
410
 
386
411
  const sequences = byKey(
387
412
  (snapshot.sequences ?? [])
@@ -397,7 +422,7 @@ export function canonicalizeState(
397
422
  v: FINGERPRINT_VERSION,
398
423
  tables: byKey(tables.map(canonicalTable), (t) => String(t.table)),
399
424
  enums: byKey(
400
- (snapshot.enums ?? [])
425
+ bareEnums(snapshot.enums ?? [])
401
426
  .filter((e) => inScope === null || usedEnum.has(e.name))
402
427
  .map((e) => ({ name: e.name, values: e.values })),
403
428
  (e) => e.name,
@@ -468,9 +493,15 @@ export function fingerprintModels(
468
493
  models: ModelDescriptor[],
469
494
  opts: { schema?: string; sequences?: SequenceDescriptor[]; schemas?: string[] } = {},
470
495
  ): Fingerprint {
496
+ // Over the WHOLE set, before scoping: an external target a model declares is refused wherever
497
+ // that model lives. Nothing else here knows an external FK from a model FK.
498
+ assertExternalTargets(models);
499
+ // Also over the whole set: a parent's hasMany `dependent` sets the ON DELETE of a child's FK.
500
+ const dependents = relationDeleteActions(models);
501
+ const dependentIndexes = lifecycleDependentIndexes(models);
471
502
  const scoped = opts.schemas ? models.filter((m) => opts.schemas!.includes(m.schema)) : models;
472
503
  const snapshot: SchemaSnapshot = {
473
- tables: scoped.map((m) => compileTableSchema(m, opts)),
504
+ tables: scoped.map((m) => compileTableSchema(m, { schema: opts.schema, dependents, dependentIndexes })),
474
505
  enums: compileEnums(scoped),
475
506
  ...(opts.sequences?.length ? { sequences: compileSequences(opts.sequences) } : {}),
476
507
  };
@@ -577,7 +608,10 @@ export function fingerprintLiveAtVersion(
577
608
  // Thread the measured serial-sequence ACLs the way `fingerprintLive` does, so a v11 canon
578
609
  // recomputes the SAME hash the apply recorded. A retired canon that predates sequence ACLs
579
610
  // (v10) simply ignores the field — its self-contained copy never reads `sequenceGrants`.
580
- return canon ? canon(snapshot, contract.tables, { ...opts, sequenceGrants: contract.sequenceGrants }) : null;
611
+ // The retired canons are frozen copies that key enums by the name they are handed, so they get
612
+ // the bare names they were written against (see `bareEnums`).
613
+ const bare: SchemaSnapshot = snapshot.enums ? { ...snapshot, enums: bareEnums(snapshot.enums) } : snapshot;
614
+ return canon ? canon(bare, contract.tables, { ...opts, sequenceGrants: contract.sequenceGrants }) : null;
581
615
  }
582
616
 
583
617
  // ---------------------------------------------------------------------------
@@ -125,6 +125,13 @@ export interface TableSchema {
125
125
  columns: ColumnSchema[];
126
126
  /** Primary-key column names (empty when the table has no PK). */
127
127
  primaryKey: string[];
128
+ /**
129
+ * The live primary key constraint's NAME — introspection's observation, absent on a compiled
130
+ * (declared) table and on a snapshot read before this field existed. The differ needs it to
131
+ * drop a key that is being replaced. It is NOT state: no fingerprint version hashes it, so a
132
+ * key under another name with the same columns is the same database.
133
+ */
134
+ primaryKeyName?: string;
128
135
  uniques: UniqueConstraint[];
129
136
  checks: CheckConstraint[];
130
137
  foreignKeys: ForeignKey[];
@@ -133,6 +140,7 @@ export interface TableSchema {
133
140
 
134
141
  /** A Postgres enum type — its name and its values in `enumsortorder` (the declared order). */
135
142
  export interface EnumType {
143
+ /** Bare for public (`post_status`), qualified otherwise (`analytics.post_status`). */
136
144
  name: string;
137
145
  values: string[];
138
146
  }
@@ -158,12 +166,35 @@ export interface SchemaSnapshot {
158
166
  sequences?: SequenceSchema[];
159
167
  /** Installed extensions, `plpgsql` excluded. Absent when the read predates this. */
160
168
  extensions?: string[];
169
+ /**
170
+ * The catalog's row estimate per table (`pg_class.reltuples`): > 0 an estimate, 0 empty, -1 a
171
+ * table with data that was never analyzed. For the plan's ⚠ LOCK line only. Beside the tables,
172
+ * never inside one, so no fingerprint or diff reads it. Absent when the read predates it.
173
+ */
174
+ rowEstimates?: Record<string, number>;
161
175
  }
162
176
 
163
177
  // ---------------------------------------------------------------------------
164
178
  // Introspection SQL.
165
179
  // ---------------------------------------------------------------------------
166
180
 
181
+ /**
182
+ * Row estimates for the plan's ⚠ LOCK line — catalog statistics, no table scan. reltuples is -1
183
+ * before the first VACUUM/ANALYZE; a table that has pages then is reported -1 (data, unknown count).
184
+ */
185
+ export const ROW_ESTIMATES_SQL = `
186
+ SELECT n.nspname AS schema, c.relname AS "table",
187
+ CASE WHEN c.reltuples > 0 THEN c.reltuples::bigint
188
+ WHEN pg_catalog.pg_relation_size(c.oid) > 0 THEN -1
189
+ ELSE 0 END AS estimate
190
+ FROM pg_class c
191
+ JOIN pg_namespace n ON n.oid = c.relnamespace
192
+ WHERE c.relkind = 'r'
193
+ AND n.nspname NOT IN ('pg_catalog', 'information_schema')
194
+ AND n.nspname NOT LIKE 'pg_%'
195
+ ORDER BY 1, 2;
196
+ `.trim();
197
+
167
198
  /**
168
199
  * Every column of every base table, with its precise type (`format_type`),
169
200
  * nullability, and default. Extension-owned tables are excluded via pg_depend,
@@ -245,6 +276,55 @@ WHERE con.contype IN ('p', 'u', 'c', 'f')
245
276
  ORDER BY n.nspname, c.relname, con.conname;
246
277
  `.trim();
247
278
 
279
+ /**
280
+ * Every foreign key's target, read from the CATALOG by `confrelid` (schema and table as separate
281
+ * names). db:pull reads it to declare an external target exactly. The FK IR carries the target as
282
+ * one dotted, quote-stripped string from the deparse (`refTable`), and that string is ambiguous:
283
+ * public."a.b" and a.b both read as `a.b`. The IR and the fingerprint keep that string; only the
284
+ * renderer, which has to split it into schema and table, needs this. Same scope as CONSTRAINTS_SQL.
285
+ */
286
+ export const FK_TARGETS_SQL = `
287
+ SELECT
288
+ n.nspname AS schema,
289
+ c.relname AS "table",
290
+ con.conname AS name,
291
+ rn.nspname AS ref_schema,
292
+ r.relname AS ref_table
293
+ FROM pg_constraint con
294
+ JOIN pg_class c ON c.oid = con.conrelid
295
+ JOIN pg_namespace n ON n.oid = c.relnamespace
296
+ JOIN pg_class r ON r.oid = con.confrelid
297
+ JOIN pg_namespace rn ON rn.oid = r.relnamespace
298
+ WHERE con.contype = 'f'
299
+ AND n.nspname NOT IN ('pg_catalog', 'information_schema')
300
+ AND n.nspname NOT LIKE 'pg_%'
301
+ ORDER BY n.nspname, c.relname, con.conname;
302
+ `.trim();
303
+
304
+ export interface ForeignKeyTargetRow {
305
+ schema: unknown;
306
+ table: unknown;
307
+ name: unknown;
308
+ ref_schema: unknown;
309
+ ref_table: unknown;
310
+ }
311
+
312
+ /** The key a {@link foreignKeyTargets} map is read with: the owning table (qualified) and the FK name. */
313
+ export function foreignKeyTargetKey(table: string, constraint: string): string {
314
+ const dot = table.indexOf('.');
315
+ const qualified = dot === -1 ? `public.${table}` : table;
316
+ return `${qualified}\u0000${constraint}`;
317
+ }
318
+
319
+ /** FK_TARGETS_SQL rows → (owning table, FK name) → the target's exact schema and table. */
320
+ export function foreignKeyTargets(rows: readonly ForeignKeyTargetRow[]): Map<string, { schema: string; table: string }> {
321
+ const out = new Map<string, { schema: string; table: string }>();
322
+ for (const r of rows) {
323
+ out.set(foreignKeyTargetKey(`${String(r.schema)}.${String(r.table)}`, String(r.name)), { schema: String(r.ref_schema), table: String(r.ref_table) });
324
+ }
325
+ return out;
326
+ }
327
+
248
328
  /**
249
329
  * Every enum type and its values, in `enumsortorder` (the order the values were declared,
250
330
  * which `CREATE TYPE` must reproduce). Extension-owned types are excluded via pg_depend,
@@ -617,9 +697,14 @@ export interface EnumRow {
617
697
  values: unknown;
618
698
  }
619
699
 
700
+ /**
701
+ * The same identity rule as sequences and tables: public bare, non-public qualified. A bare
702
+ * name for every schema let a stray `analytics.post_status` satisfy the declared public
703
+ * `post_status`, so the plan never created the type the column actually needs.
704
+ */
620
705
  export function enumRowToDescriptor(row: EnumRow): EnumType {
621
706
  const values = Array.isArray(row.values) ? row.values.map(String) : [];
622
- return { name: row.name, values };
707
+ return { name: row.schema === 'public' ? row.name : `${row.schema}.${row.name}`, values };
623
708
  }
624
709
 
625
710
  export interface SequenceRow {
@@ -649,6 +734,8 @@ export interface SchemaRows {
649
734
  indexes?: IndexRow[];
650
735
  sequences?: SequenceRow[];
651
736
  extensions?: ExtensionRow[];
737
+ /** Absent on a read that predates ROW_ESTIMATES_SQL (an older ops Lambda reply). */
738
+ estimates?: Array<{ schema: string; table: string; estimate: unknown }>;
652
739
  }
653
740
 
654
741
  /**
@@ -689,7 +776,7 @@ export function assembleSchema(rows: SchemaRows): SchemaSnapshot {
689
776
  const t = tables.get(d.table);
690
777
  if (!t) continue; // a constraint on a table we didn't enumerate (e.g. a view)
691
778
  switch (d.kind) {
692
- case 'primaryKey': t.primaryKey = d.columns ?? []; break;
779
+ case 'primaryKey': t.primaryKey = d.columns ?? []; t.primaryKeyName = d.name; break;
693
780
  case 'unique': t.uniques.push({ name: d.name, columns: d.columns ?? [] }); break;
694
781
  case 'check': t.checks.push({ name: d.name, expr: d.expr ?? '' }); break;
695
782
  case 'foreignKey':
@@ -742,6 +829,9 @@ export function assembleSchema(rows: SchemaRows): SchemaSnapshot {
742
829
  enums,
743
830
  ...(sequences.length ? { sequences } : {}),
744
831
  ...(extensions !== undefined ? { extensions } : {}),
832
+ ...(rows.estimates !== undefined
833
+ ? { rowEstimates: Object.fromEntries(rows.estimates.map((r) => [`${r.schema}.${r.table}`, Number(r.estimate)])) }
834
+ : {}),
745
835
  };
746
836
  }
747
837
 
@@ -758,8 +848,10 @@ export async function introspectSchema(session: SessionRunner): Promise<SchemaSn
758
848
  // search_path. Column defaults, CHECK constraints and index expressions all deparse
759
849
  // relative to that path — spread across separate connections these five rows can render
760
850
  // the same schema two ways, and a fingerprint minted from the mix describes nothing.
761
- const [columns, constraints, enums, indexes, sequences, extensions] = await session(
762
- [COLUMNS_SQL, CONSTRAINTS_SQL, ENUMS_SQL, INDEXES_SQL, SEQUENCES_SQL, EXTENSIONS_SQL],
851
+ // The row estimates ride the same session and snapshot; a reply without them (fewer result
852
+ // sets) assembles without `rowEstimates`, and the plan then warns without a count.
853
+ const [columns, constraints, enums, indexes, sequences, extensions, estimates] = await session(
854
+ [COLUMNS_SQL, CONSTRAINTS_SQL, ENUMS_SQL, INDEXES_SQL, SEQUENCES_SQL, EXTENSIONS_SQL, ROW_ESTIMATES_SQL],
763
855
  INTROSPECTION_SESSION,
764
856
  );
765
857
  return assembleSchema({
@@ -769,5 +861,6 @@ export async function introspectSchema(session: SessionRunner): Promise<SchemaSn
769
861
  indexes: indexes as IndexRow[],
770
862
  sequences: sequences as SequenceRow[],
771
863
  extensions: extensions as ExtensionRow[],
864
+ ...(Array.isArray(estimates) ? { estimates: estimates as SchemaRows['estimates'] } : {}),
772
865
  });
773
866
  }