@everystack/cli 0.3.31 → 0.4.1

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 (43) hide show
  1. package/README.md +1 -1
  2. package/package.json +2 -2
  3. package/src/.pdr-tmp-20277-1783706384842.ts +59 -0
  4. package/src/.roundtrip-tmp-20275-1783706385459.ts +121 -0
  5. package/src/cli/authz-compile.ts +5 -2
  6. package/src/cli/aws.ts +12 -3
  7. package/src/cli/backfill.ts +1 -1
  8. package/src/cli/commands/db-authz.ts +3 -17
  9. package/src/cli/commands/db-branch.ts +28 -4
  10. package/src/cli/commands/db-check.ts +84 -33
  11. package/src/cli/commands/db-fingerprint.ts +7 -2
  12. package/src/cli/commands/db-generate.ts +102 -34
  13. package/src/cli/commands/db-pull.ts +82 -6
  14. package/src/cli/commands/db-reconcile.ts +132 -41
  15. package/src/cli/commands/db-sync.ts +54 -19
  16. package/src/cli/commands/db.ts +7 -6
  17. package/src/cli/commands/update.ts +2 -1
  18. package/src/cli/db-build.ts +9 -3
  19. package/src/cli/db-source.ts +5 -1
  20. package/src/cli/declared-derived.ts +173 -0
  21. package/src/cli/derived-apply.ts +40 -6
  22. package/src/cli/derived-compile.ts +377 -0
  23. package/src/cli/derived-grants.ts +96 -0
  24. package/src/cli/derived-introspect.ts +258 -21
  25. package/src/cli/derived-lint.ts +261 -0
  26. package/src/cli/derived-plan.ts +176 -10
  27. package/src/cli/derived-render.ts +366 -0
  28. package/src/cli/derived-source.ts +53 -125
  29. package/src/cli/edge-plan.ts +4 -1
  30. package/src/cli/git-descent.ts +15 -2
  31. package/src/cli/index.ts +6 -6
  32. package/src/cli/migration-compile.ts +38 -7
  33. package/src/cli/migration-generate.ts +43 -4
  34. package/src/cli/model-render.ts +125 -16
  35. package/src/cli/models-path.ts +1 -1
  36. package/src/cli/ops-advice.ts +66 -0
  37. package/src/cli/pipeline-path.ts +1 -1
  38. package/src/cli/schema-compile.ts +41 -10
  39. package/src/cli/schema-diff.ts +123 -17
  40. package/src/cli/schema-fingerprint.ts +28 -18
  41. package/src/cli/schema-introspect.ts +175 -8
  42. package/src/cli/schema-source.ts +75 -6
  43. package/src/cli/state-apply.ts +52 -1
@@ -19,12 +19,12 @@
19
19
  * Input is a `@everystack/model` ModelDescriptor (type-only — no runtime dep).
20
20
  */
21
21
 
22
- import type { ModelDescriptor, FieldSpec } from '@everystack/model';
23
- import type { TableSchema, EnumType, UniqueConstraint, CheckConstraint, IndexSchema, ForeignKey } from './schema-introspect.js';
24
- import type { RenameMap } from './schema-diff.js';
22
+ import type { ModelDescriptor, FieldSpec, SequenceDescriptor } from '@everystack/model';
23
+ import type { TableSchema, EnumType, SequenceSchema, UniqueConstraint, CheckConstraint, IndexSchema, ForeignKey } from './schema-introspect.js';
24
+ import { nextvalSequence, type RenameMap } from './schema-diff.js';
25
25
 
26
26
  /** `authorId` -> `author_id`. SQL identifiers are snake_case. */
27
- function toSnakeCase(name: string): string {
27
+ export function toSnakeCase(name: string): string {
28
28
  return name.replace(/[A-Z]/g, (c) => `_${c.toLowerCase()}`);
29
29
  }
30
30
 
@@ -48,20 +48,25 @@ export function modelConstraintSpecs(model: ModelDescriptor): { uniques: UniqueC
48
48
  } else if (con.kind === 'check') {
49
49
  checks.push({ name: `${model.table}_check_${checkIndex++}`, expr: con.predicate });
50
50
  } else if (con.kind === 'index') {
51
- const cols = con.columns.map(toSnakeCase);
51
+ // Plain entries are field keys → snake_cased; raw sql`` entries (expressions,
52
+ // DESC, opclass) pass verbatim — they are already SQL (Brick E).
53
+ const cols = con.columns.map((c, i) => (con.rawPositions?.[i] ? c : toSnakeCase(c)));
52
54
  // Disambiguate multiple indexes over the same columns — e.g. a full index plus a
53
55
  // partial one (`familyId` and `familyId WHERE status='active'`): the name must be
54
56
  // unique within the table for CREATE to succeed. The diff matches indexes by content
55
- // (columns + uniqueness + predicate), never by name, so the suffix never affects the
56
- // round-trip. Declaration order is deterministic, so the name is stable across calls.
57
- let name = `${model.table}_${cols.join('_')}_index`;
58
- for (let n = 2; usedIndexNames.has(name); n++) name = `${model.table}_${cols.join('_')}_index_${n}`;
57
+ // (entries + uniqueness + predicate + method + INCLUDE), never by name, so the suffix
58
+ // never affects the round-trip. Raw entries sanitize to identifier-safe name parts.
59
+ const nameParts = cols.map((c) => c.replace(/\W+/g, '_').replace(/^_+|_+$/g, ''));
60
+ let name = `${model.table}_${nameParts.join('_')}_index`;
61
+ for (let n = 2; usedIndexNames.has(name); n++) name = `${model.table}_${nameParts.join('_')}_index_${n}`;
59
62
  usedIndexNames.add(name);
60
63
  indexes.push({
61
64
  name,
62
65
  columns: cols,
63
66
  unique: con.isUnique,
64
67
  ...(con.predicate ? { where: con.predicate } : {}),
68
+ ...(con.method ? { using: con.method } : {}),
69
+ ...(con.includeColumns?.length ? { include: con.includeColumns.map(toSnakeCase) } : {}),
65
70
  });
66
71
  }
67
72
  }
@@ -209,6 +214,7 @@ function defaultExpr(spec: FieldSpec): string | null {
209
214
  switch (spec.defaultKind) {
210
215
  case 'now': return 'now()';
211
216
  case 'random': return 'gen_random_uuid()';
217
+ case 'sql': return String(spec.default); // .defaultSql() — the author's SQL, verbatim
212
218
  case 'value':
213
219
  return spec.isArray && Array.isArray(spec.default) ? arrayLiteral(spec.default) : literal(spec.default);
214
220
  default: return null;
@@ -251,7 +257,12 @@ export function fieldColumnSql(sqlName: string, spec: FieldSpec, inlinePrimaryKe
251
257
  let s = `"${sqlName}" ${serial ?? pgType(spec)}`;
252
258
  if (inlinePrimaryKey) s += ' PRIMARY KEY';
253
259
  const def = serial ? null : defaultExpr(spec);
254
- if (def) s += ` DEFAULT ${def}`;
260
+ // A `defaultSql` nextval draws from an EXISTING sequence, possibly owned by a table
261
+ // created later in the same migration — held out of the column line (compileMigration
262
+ // re-attaches it as a trailing SET DEFAULT). Never the serial shorthand: that would
263
+ // mint a NEW sequence and silently detach the column from the one it declared.
264
+ const heldSequenceDraw = spec.defaultKind === 'sql' && nextvalSequence(def) != null;
265
+ if (def && !heldSequenceDraw) s += ` DEFAULT ${def}`;
255
266
  if (spec.isNotNull) s += ' NOT NULL';
256
267
  return s;
257
268
  }
@@ -485,6 +496,26 @@ export function compileEnums(models: ModelDescriptor[]): EnumType[] {
485
496
  return [...byName.values()].sort((a, b) => a.name.localeCompare(b.name));
486
497
  }
487
498
 
499
+ /**
500
+ * Standalone-sequence descriptors → the snapshot shape the diff and fingerprint consume.
501
+ * PostgreSQL's defaults (start/increment of 1) are omitted, so a declaration that omits
502
+ * them reaches the same canonical form a live default-configured sequence introspects to.
503
+ */
504
+ export function compileSequences(sequences: readonly SequenceDescriptor[]): SequenceSchema[] {
505
+ return sequences
506
+ .map((s) => ({
507
+ name: s.name,
508
+ as: s.as,
509
+ ...(s.start != null && s.start !== 1 ? { start: s.start } : {}),
510
+ ...(s.increment != null && s.increment !== 1 ? { increment: s.increment } : {}),
511
+ }))
512
+ .sort((a, b) => a.name.localeCompare(b.name));
513
+ }
514
+
515
+ // The DDL renderer lives in schema-diff (emitOne needs it; this file already imports
516
+ // schema-diff, so defining it there avoids a cycle) — re-exported here beside its compiler.
517
+ export { sequenceCreateSql } from './schema-diff.js';
518
+
488
519
  /**
489
520
  * Foreign-key constraints for a model — its `.references()` fields and its table-level
490
521
  * `foreignKey(...)` constraints, via `modelForeignKeys` — as `ALTER TABLE` statements. These
@@ -30,6 +30,7 @@ import type {
30
30
  ColumnSchema,
31
31
  EnumType,
32
32
  IndexSchema,
33
+ SequenceSchema,
33
34
  } from './schema-introspect.js';
34
35
 
35
36
  // ---------------------------------------------------------------------------
@@ -79,10 +80,14 @@ export type SchemaChange =
79
80
  | { kind: 'dropType'; name: string }
80
81
  /** An enum value the DB has but the Model dropped/renamed — Postgres can't drop one in place (a notice). */
81
82
  | { kind: 'enumValuesChanged'; name: string; from: string[]; to: string[] }
82
- /** A standalone index to create → `CREATE [UNIQUE] INDEX … [WHERE …]`. */
83
- | { kind: 'createIndex'; table: string; name: string; columns: string[]; unique: boolean; where?: string }
83
+ /** A standalone index to create → `CREATE [UNIQUE] INDEX … [USING …] (…) [INCLUDE …] [WHERE …]`. */
84
+ | { kind: 'createIndex'; table: string; name: string; columns: string[]; unique: boolean; where?: string; using?: string; include?: string[] }
84
85
  /** A standalone index present in the DB but no longer declared → `DROP INDEX` (a removal — gated). */
85
- | { kind: 'dropIndex'; table: string; name: string };
86
+ | { kind: 'dropIndex'; table: string; name: string }
87
+ /** A declared standalone sequence missing live → `CREATE SEQUENCE`, before any table. */
88
+ | { kind: 'createSequence'; sequence: SequenceSchema }
89
+ /** A declared sequence whose live properties differ — a NOTICE, never a silent ALTER (it holds state). */
90
+ | { kind: 'sequenceDrift'; name: string; declared: SequenceSchema; live: SequenceSchema };
86
91
 
87
92
  /**
88
93
  * Rename intent, keyed like the snapshot's tables (`public.posts`) → { newColumn: oldColumn }.
@@ -131,9 +136,48 @@ export function normalizeDefault(expr: string | null): string | null {
131
136
  // `nextval('public.x_seq')` on another. public is always on the search_path — strip it.
132
137
  s = s.replace(/^nextval\('public\.([^']+)'/, "nextval('$1'");
133
138
 
139
+ // The deparser also casts the sequence literal (`nextval('x_seq'::regclass)`); an
140
+ // authored `defaultSql` may spell it bare. One canonical (castless) form for both —
141
+ // a normalization change, so FINGERPRINT_VERSION was bumped with it.
142
+ s = s.replace(/^nextval\('([^']+)'::regclass\)$/, "nextval('$1')");
143
+
134
144
  return s;
135
145
  }
136
146
 
147
+ /**
148
+ * The sequence a `nextval` default draws from (schema qualification and the `::regclass`
149
+ * cast stripped), or null when the expression isn't a plain nextval. Drives two decisions:
150
+ * the `serial` shorthand fires ONLY for a column's own conventional `<table>_<col>_seq`
151
+ * (anything else would mint a NEW sequence), and a foreign-sequence default is held out
152
+ * of CREATE/ADD and re-attached as a trailing SET DEFAULT (its sequence may live on a
153
+ * table created later).
154
+ */
155
+ export function nextvalSequence(expr: string | null): string | null {
156
+ if (expr == null) return null;
157
+ const m = expr.trim().match(/^nextval\('(?:[^'.]+\.)?([^'.]+)'(?:::regclass)?\)$/);
158
+ return m ? m[1] : null;
159
+ }
160
+
161
+ /** `public.contracts` / `contracts` → `contracts` (the bare table name for sequence conventions). */
162
+ function bareTable(table: string): string {
163
+ return table.replace(/^[^.]+\./, '');
164
+ }
165
+
166
+ /** `CREATE SEQUENCE` DDL for a standalone sequence — emitted BEFORE any table, so a
167
+ * column's `defaultSql(nextval(…))` can draw on it from the first CREATE. */
168
+ export function sequenceCreateSql(seq: SequenceSchema): string {
169
+ let s = `CREATE SEQUENCE ${seq.name} AS ${seq.as}`;
170
+ if (seq.start != null) s += ` START WITH ${seq.start}`;
171
+ if (seq.increment != null) s += ` INCREMENT BY ${seq.increment}`;
172
+ return `${s};`;
173
+ }
174
+
175
+ /** True when a column's default draws a sequence that is NOT its own conventional one. */
176
+ function isForeignSequenceDefault(table: string, col: ColumnSchema): boolean {
177
+ const seq = nextvalSequence(col.default);
178
+ return seq != null && seq !== `${bareTable(table)}_${col.name}_seq`;
179
+ }
180
+
137
181
  // ---------------------------------------------------------------------------
138
182
  // Type-change classification — does the conversion need a USING cast and a warning?
139
183
  // ---------------------------------------------------------------------------
@@ -318,29 +362,76 @@ export function diffSchema(desired: SchemaSnapshot, current: SchemaSnapshot, ren
318
362
 
319
363
  const e = diffEnums(desired.enums ?? [], current.enums ?? []);
320
364
 
365
+ // Standalone sequences — existence-diffed, DECLARED-SCOPED (a live sequence the models
366
+ // never declared is invisible: it holds state, and undeclared ≠ ours to drop). A declared
367
+ // one missing live is a create, before any table (a defaultSql may draw on it from the
368
+ // first CREATE); live property drift is a NOTICE — never a silent ALTER on a counter.
369
+ const sequenceCreates: SchemaChange[] = [];
370
+ const sequenceNotices: SchemaChange[] = [];
371
+ const currentSeqs = new Map((current.sequences ?? []).map((s) => [s.name, s]));
372
+ for (const declared of desired.sequences ?? []) {
373
+ const liveSeq = currentSeqs.get(declared.name);
374
+ if (!liveSeq) {
375
+ sequenceCreates.push({ kind: 'createSequence', sequence: declared });
376
+ } else if (liveSeq.as !== declared.as || (liveSeq.increment ?? 1) !== (declared.increment ?? 1)) {
377
+ sequenceNotices.push({ kind: 'sequenceDrift', name: declared.name, declared, live: liveSeq });
378
+ }
379
+ }
380
+
381
+ // Foreign-sequence defaults are held out of CREATE/ADD by the emitters (the sequence
382
+ // may live on a table created later in the same batch) and re-attached here as
383
+ // SET DEFAULT changes ordered after every create and add.
384
+ const trailingDefaults: SchemaChange[] = [];
385
+ for (const c of creates) {
386
+ if (c.kind !== 'createTable') continue;
387
+ for (const col of c.schema.columns) {
388
+ if (isForeignSequenceDefault(c.table, col)) {
389
+ trailingDefaults.push({ kind: 'setDefault', table: c.table, column: col.name, expr: col.default! });
390
+ }
391
+ }
392
+ }
393
+ for (const c of addColumns) {
394
+ if (c.kind !== 'addColumn') continue;
395
+ if (isForeignSequenceDefault(c.table, c.column)) {
396
+ trailingDefaults.push({ kind: 'setDefault', table: c.table, column: c.column.name, expr: c.column.default! });
397
+ }
398
+ }
399
+
321
400
  // Enum types come first (a table or column may reference one); value adds before the
322
401
  // columns that use them. Table renames run before creates (a rename frees a name a
323
402
  // create may reuse) and before every per-table change (which all target the new name).
324
403
  // Column renames run before adds and alters for the same reason. Enum drops sit with
325
404
  // the other removals; notices are comments — emitted last, after the DDL.
326
405
  return [
327
- ...e.creates, ...e.adds,
406
+ ...e.creates, ...e.adds, ...sequenceCreates,
328
407
  ...renameTables,
329
- ...creates, ...renameColumns, ...addColumns, ...alters,
408
+ ...creates, ...renameColumns, ...addColumns, ...alters, ...trailingDefaults,
330
409
  ...dropConstraints, ...addConstraints, ...createIndexes,
331
410
  ...dropColumns, ...dropIndexes, ...dropTables,
332
- ...e.drops, ...notices, ...e.notices,
411
+ ...e.drops, ...notices, ...e.notices, ...sequenceNotices,
333
412
  ];
334
413
  }
335
414
 
336
415
  /** A `createIndex` change from an index spec (the new-table path and the per-table diff share it). */
337
416
  function createIndexChange(table: string, ix: IndexSchema): SchemaChange {
338
- return { kind: 'createIndex', table, name: ix.name, columns: ix.columns, unique: ix.unique, ...(ix.where ? { where: ix.where } : {}) };
417
+ return {
418
+ kind: 'createIndex', table, name: ix.name, columns: ix.columns, unique: ix.unique,
419
+ ...(ix.where ? { where: ix.where } : {}),
420
+ ...(ix.using ? { using: ix.using } : {}),
421
+ ...(ix.include?.length ? { include: ix.include } : {}),
422
+ };
339
423
  }
340
424
 
341
- /** An index's content identity — columns, uniqueness, and partial predicate, name-independent (like checks). */
425
+ /**
426
+ * An index's content identity — key entries (expressions/direction/opclass are canonical
427
+ * text, normalized like checks), uniqueness, predicate, access method, and INCLUDE set:
428
+ * name-independent, and no longer blind to anything `CREATE INDEX` can say (Brick E —
429
+ * the consumer's 31 phantom drop/creates). Plain btree indexes key exactly as before
430
+ * plus a constant tail — both sides compute the same key, so nothing churns.
431
+ */
342
432
  function indexKey(ix: IndexSchema): string {
343
- return `${ix.unique ? 'u' : ''}|${ix.columns.join(',')}|${normalizeCheck(ix.where ?? '')}`;
433
+ const entries = ix.columns.map((c) => normalizeCheck(c)).join(',');
434
+ return `${ix.unique ? 'u' : ''}|${entries}|${normalizeCheck(ix.where ?? '')}|${ix.using ?? 'btree'}|${(ix.include ?? []).join(',')}`;
344
435
  }
345
436
 
346
437
  /**
@@ -555,25 +646,31 @@ const enumLiteral = (value: string): string => `'${value.replace(/'/g, "''")}'`;
555
646
  * column), or null. CREATE/ADD COLUMN must use the shorthand — it creates the backing
556
647
  * sequence, which a bare `DEFAULT nextval(...)` would reference before it exists.
557
648
  */
558
- function serialSpelling(col: ColumnSchema): string | null {
559
- if (col.default == null || !/^nextval\('[^']+'::regclass\)$/.test(col.default)) return null;
649
+ function serialSpelling(col: ColumnSchema, table: string): string | null {
650
+ // ONLY the column's own conventional sequence is a serial declaration — the shorthand
651
+ // creates a sequence, so spelling it for a foreign `nextval` would mint a duplicate
652
+ // and silently detach the column from the sequence it declared.
653
+ const seq = nextvalSequence(col.default);
654
+ if (seq == null || seq !== `${bareTable(table)}_${col.name}_seq`) return null;
560
655
  if (col.type === 'integer') return 'serial';
561
656
  if (col.type === 'bigint') return 'bigserial';
562
657
  return null;
563
658
  }
564
659
 
565
660
  /** A column definition body (no leading indent): `"name" type [DEFAULT expr] [NOT NULL]`. */
566
- function columnSql(col: ColumnSchema): string {
567
- const serial = serialSpelling(col);
661
+ function columnSql(col: ColumnSchema, table: string): string {
662
+ const serial = serialSpelling(col, table);
568
663
  let s = `${quote(col.name)} ${serial ?? col.type}`;
569
- if (!serial && col.default != null) s += ` DEFAULT ${col.default}`;
664
+ // A foreign-sequence default is held out diffSchema appends the trailing SET DEFAULT
665
+ // (after every create), so the CREATE never references a sequence that doesn't exist yet.
666
+ if (!serial && col.default != null && !isForeignSequenceDefault(table, col)) s += ` DEFAULT ${col.default}`;
570
667
  if (col.notNull) s += ' NOT NULL';
571
668
  return s;
572
669
  }
573
670
 
574
671
  /** `CREATE TABLE` from the IR: columns + PK/unique/check inline; FKs are emitted separately. */
575
672
  function createTableSql(t: TableSchema): string {
576
- const lines = t.columns.map((c) => `\t${columnSql(c)}`);
673
+ const lines = t.columns.map((c) => `\t${columnSql(c, t.table)}`);
577
674
  if (t.primaryKey.length) lines.push(`\tPRIMARY KEY (${colList(t.primaryKey)})`);
578
675
  for (const u of t.uniques) lines.push(`\tCONSTRAINT ${quote(u.name)} UNIQUE (${colList(u.columns)})`);
579
676
  for (const ck of t.checks) lines.push(`\tCONSTRAINT ${quote(ck.name)} CHECK ${wrapOnce(ck.expr)}`);
@@ -613,7 +710,7 @@ function emitOne(change: SchemaChange, opts: EmitOptions): string {
613
710
  case 'dropTable':
614
711
  return `DROP TABLE ${change.table};`;
615
712
  case 'addColumn': {
616
- const ddl = `ALTER TABLE ${change.table} ADD COLUMN ${columnSql(change.column)};`;
713
+ const ddl = `ALTER TABLE ${change.table} ADD COLUMN ${columnSql(change.column, change.table)};`;
617
714
  // Adding a NOT NULL column with no default fails on a table that already has rows.
618
715
  if (change.column.notNull && change.column.default == null) {
619
716
  return `-- WARNING: ADD COLUMN ${quote(change.column.name)} NOT NULL on ${change.table} has no default and FAILS if the table has rows; add a default or backfill first.\n${ddl}`;
@@ -674,8 +771,13 @@ function emitOne(change: SchemaChange, opts: EmitOptions): string {
674
771
  return `DROP TYPE ${quote(change.name)};`;
675
772
  case 'createIndex': {
676
773
  const unique = change.unique ? 'UNIQUE ' : '';
774
+ const using = change.using ? ` USING ${change.using}` : '';
775
+ // A plain identifier entry quotes as before; a canonical-text entry (expression,
776
+ // DESC, opclass) passes verbatim — quoting the whole thing would mangle it.
777
+ const entry = (c: string): string => (/^[a-z_][a-z0-9_$]*$/i.test(c) ? quote(c) : c);
778
+ const include = change.include?.length ? ` INCLUDE (${colList(change.include)})` : '';
677
779
  const where = change.where ? ` WHERE ${change.where}` : '';
678
- return `CREATE ${unique}INDEX ${quote(change.name)} ON ${change.table} (${colList(change.columns)})${where};`;
780
+ return `CREATE ${unique}INDEX ${quote(change.name)} ON ${change.table}${using} (${change.columns.map(entry).join(', ')})${include}${where};`;
679
781
  }
680
782
  case 'dropIndex': {
681
783
  const schema = change.table.includes('.') ? `${change.table.split('.')[0]}.` : '';
@@ -683,6 +785,10 @@ function emitOne(change: SchemaChange, opts: EmitOptions): string {
683
785
  }
684
786
  case 'enumValuesChanged':
685
787
  return `-- NOTICE: enum ${quote(change.name)} values changed ([${change.from.join(', ')}] → [${change.to.join(', ')}]) in a way Postgres can't apply in place (a removal, reorder, or rename). ADD VALUE only appends; reshaping an enum needs a manual type swap (create new type, ALTER COLUMN ... TYPE using a cast, drop old).`;
788
+ case 'createSequence':
789
+ return sequenceCreateSql(change.sequence);
790
+ case 'sequenceDrift':
791
+ return `-- NOTICE: sequence ${change.name} differs from its declaration (declared AS ${change.declared.as}${change.declared.increment != null ? ` INCREMENT ${change.declared.increment}` : ''}; live AS ${change.live.as}${change.live.increment != null ? ` INCREMENT ${change.live.increment}` : ''}). A sequence holds a counter — reshape it by hand (ALTER SEQUENCE) after deciding what the counter should be.`;
686
792
  }
687
793
  }
688
794
 
@@ -39,23 +39,30 @@
39
39
  * fingerprints. The version prefix plus the re-diff path keep that honest.
40
40
  *
41
41
  * Coverage is partial and SAYS SO: `UNFINGERPRINTED_SQL` enumerates what the
42
- * base fingerprint does not see (standalone sequences, triggers, domains,
43
- * composite types, foreign/partitioned tables, extensions) so partial
44
- * coverage reads as a report, never as "covered everything".
42
+ * base fingerprint does not see (triggers drift-checked separately by the
43
+ * reconciler's provenance — domains, composite types, foreign/partitioned
44
+ * tables, extensions) so partial coverage reads as a report, never as
45
+ * "covered everything". Standalone sequences left this list in v3: covered.
45
46
  */
46
47
 
47
48
  import { createHash } from 'node:crypto';
48
- import type { ModelDescriptor } from '@everystack/model';
49
+ import type { ModelDescriptor, SequenceDescriptor } from '@everystack/model';
49
50
  import type { SchemaSnapshot, TableSchema } from './schema-introspect.js';
50
51
  import type { AuthzContract, TableContract } from './authz-contract.js';
51
- import { compileTableSchema, compileEnums } from './schema-compile.js';
52
+ import { compileTableSchema, compileEnums, compileSequences } from './schema-compile.js';
52
53
  import { compileTableContract } from './authz-compile.js';
53
54
  import { normalizeDefault, normalizeCheck } from './schema-diff.js';
54
55
 
55
56
  // v2: expression fields (defaults, checks, partial-index WHERE) normalize through
56
57
  // the diff's pg-deparse normalizers before hashing, so the model form and the live
57
58
  // deparsed form agree — the identity `fingerprintModels === fingerprintLive` holds.
58
- export const FINGERPRINT_VERSION = 2;
59
+ // v3: nextval defaults canonicalize to the castless form (`nextval('x_seq')`), so an
60
+ // authored `defaultSql` and the deparser's `nextval('x_seq'::regclass)` hash equal
61
+ // (canonical forms changed for every serial column's default → the bump). ALSO in v3:
62
+ // STANDALONE SEQUENCES enter the canonical form (declared via defineSequence,
63
+ // introspected from pg_sequence minus serial-owned) — a coverage expansion; the
64
+ // `sequences` key appears only when any exist, so sequence-free states hash unchanged.
65
+ export const FINGERPRINT_VERSION = 3;
59
66
 
60
67
  // ---------------------------------------------------------------------------
61
68
  // Canonical form.
@@ -149,12 +156,23 @@ export interface CanonicalState {
149
156
  tables: Array<Record<string, unknown>>;
150
157
  enums: Array<{ name: string; values: string[] }>;
151
158
  authz: Array<Record<string, unknown>>;
159
+ /** Standalone sequences — present ONLY when any exist, so a sequence-free state's
160
+ * canonical form (and hash) is byte-identical to the pre-coverage form. */
161
+ sequences?: Array<Record<string, unknown>>;
152
162
  }
153
163
 
154
164
  export function canonicalizeState(
155
165
  snapshot: SchemaSnapshot,
156
166
  authzTables: TableContract[],
157
167
  ): CanonicalState {
168
+ const sequences = byKey(
169
+ (snapshot.sequences ?? []).map((s) => ({
170
+ name: s.name, as: s.as,
171
+ ...(s.start != null ? { start: s.start } : {}),
172
+ ...(s.increment != null ? { increment: s.increment } : {}),
173
+ })),
174
+ (s) => String(s.name),
175
+ );
158
176
  return {
159
177
  v: FINGERPRINT_VERSION,
160
178
  tables: byKey(snapshot.tables.map(canonicalTable), (t) => String(t.table)),
@@ -163,6 +181,7 @@ export function canonicalizeState(
163
181
  (e) => e.name,
164
182
  ),
165
183
  authz: byKey(authzTables.map(canonicalAuthzTable), (t) => String(t.table)),
184
+ ...(sequences.length ? { sequences } : {}),
166
185
  };
167
186
  }
168
187
 
@@ -184,11 +203,12 @@ export function fingerprintState(
184
203
  /** The DECLARED state: compile the models to the same canonical shape and hash it. */
185
204
  export function fingerprintModels(
186
205
  models: ModelDescriptor[],
187
- opts: { schema?: string } = {},
206
+ opts: { schema?: string; sequences?: SequenceDescriptor[] } = {},
188
207
  ): Fingerprint {
189
208
  const snapshot: SchemaSnapshot = {
190
209
  tables: models.map((m) => compileTableSchema(m, opts)),
191
210
  enums: compileEnums(models),
211
+ ...(opts.sequences?.length ? { sequences: compileSequences(opts.sequences) } : {}),
192
212
  };
193
213
  const authzTables = models.map((m) => compileTableContract(m, opts));
194
214
  return fingerprintState(snapshot, authzTables);
@@ -210,17 +230,7 @@ export function fingerprintLive(snapshot: SchemaSnapshot, contract: AuthzContrac
210
230
  * types, foreign tables, partitioned tables, and installed extensions.
211
231
  */
212
232
  export const UNFINGERPRINTED_SQL = `
213
- SELECT 'sequence' AS kind, n.nspname || '.' || c.relname AS identity
214
- FROM pg_class c
215
- JOIN pg_namespace n ON n.oid = c.relnamespace
216
- WHERE c.relkind = 'S'
217
- AND n.nspname NOT IN ('pg_catalog', 'information_schema') AND n.nspname NOT LIKE 'pg_%'
218
- AND NOT EXISTS (
219
- SELECT 1 FROM pg_depend d
220
- WHERE d.objid = c.oid AND d.deptype IN ('a', 'i') AND d.refobjsubid > 0
221
- )
222
- UNION ALL
223
- SELECT 'trigger', n.nspname || '.' || c.relname || '.' || t.tgname
233
+ SELECT 'trigger' AS kind, n.nspname || '.' || c.relname || '.' || t.tgname AS identity
224
234
  FROM pg_trigger t
225
235
  JOIN pg_class c ON c.oid = t.tgrelid
226
236
  JOIN pg_namespace n ON n.oid = c.relnamespace