@akal/supaforge 0.0.48 → 0.0.49

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 (33) hide show
  1. package/README.md +33 -8
  2. package/dist/base-command.js +2 -2
  3. package/dist/{chunk-HZ2FKMY3.js → chunk-4EEDB7Q3.js} +4 -3
  4. package/dist/{chunk-AMG4PVQS.js → chunk-56U2BYRZ.js} +2 -2
  5. package/dist/{chunk-FKL3TQB7.js → chunk-56UP3KK3.js} +17 -10
  6. package/dist/{chunk-S2GQI7ZO.js → chunk-EI7EP5IU.js} +4 -4
  7. package/dist/{chunk-VEXHBRNW.js → chunk-EJBEEWGI.js} +7 -7
  8. package/dist/{chunk-NCMERJNX.js → chunk-EJMRK6LG.js} +2 -2
  9. package/dist/{chunk-W42J6U7R.js → chunk-GKYJA56H.js} +15 -4
  10. package/dist/{chunk-PSSMJRMM.js → chunk-JLY4IG73.js} +53 -8
  11. package/dist/{chunk-Z3SB66RS.js → chunk-JPZZL3KJ.js} +5 -5
  12. package/dist/{chunk-HKG4KGDV.js → chunk-OZW36QCK.js} +2 -2
  13. package/dist/{chunk-D3JEYV4M.js → chunk-P66NJJ6I.js} +6 -6
  14. package/dist/{chunk-EPGKCP3K.js → chunk-SQHEF5NQ.js} +2 -2
  15. package/dist/{chunk-X4L3JT4R.js → chunk-TREPSQWG.js} +3 -3
  16. package/dist/{chunk-C7VBY54T.js → chunk-VCNTXXEC.js} +202 -22
  17. package/dist/{chunk-RXPYUFJR.js → chunk-Y6JGEFSW.js} +6 -6
  18. package/dist/{chunk-3XQHSJOJ.js → chunk-YCJXGDVL.js} +3 -3
  19. package/dist/commands/clone.js +5 -5
  20. package/dist/commands/diff.js +9 -9
  21. package/dist/commands/hukam.js +9 -9
  22. package/dist/commands/mcp.js +8 -8
  23. package/dist/commands/migrate/baseline.js +3 -3
  24. package/dist/commands/migrate/create.js +8 -8
  25. package/dist/commands/migrate/list.js +3 -3
  26. package/dist/commands/migrate/run.js +7 -7
  27. package/dist/commands/report.js +2 -2
  28. package/dist/commands/restore.js +8 -8
  29. package/dist/commands/snapshot.js +5 -5
  30. package/dist/commands/sync.js +9 -9
  31. package/dist/index.d.ts +7 -1
  32. package/dist/index.js +13 -13
  33. package/package.json +2 -2
package/README.md CHANGED
@@ -69,7 +69,7 @@ Docker image needs nothing at all, and carries the PostgreSQL client tools
69
69
  | Check | What it reads | Fixed with |
70
70
  | --- | --- | --- |
71
71
  | Schema | [`@dbdiff/cli`](https://github.com/DBDiff/DBDiff): every schema you own | SQL |
72
- | Reference data | Rows of the tables in `checks.data.tables` | SQL |
72
+ | Reference data | Rows of the tables in `checks.data.tables`, and the sequences behind their ids | SQL |
73
73
  | RLS policies | `pg_policies`, compared by meaning, not text | SQL |
74
74
  | RLS coverage | Tables in the target with RLS off (target only) | SQL, opt-in |
75
75
  | Edge Functions | Management API, Studio, or a functions directory | Deploy command |
@@ -134,7 +134,7 @@ Useful `diff` flags:
134
134
  | `--source-dir <dir>` | Compare a directory of schema files (such as `supabase/schemas`) with the target instead of a source database |
135
135
  | `--allow-destructive` | Also apply drops and deletes |
136
136
  | `--apply-posture` | Also apply the target-only checks' fixes |
137
- | `--no-transaction` | Apply fix by fix, keeping what works |
137
+ | `--no-transaction` | Apply fix by fix, keeping what works. Synced rows then fire the target's triggers |
138
138
  | `--no-snapshot` | Read each query on its own. By default each database is read as of one instant, so a change made while the scan runs is seen by all of it or none; as with `pg_dump`, what it reads stays locked until the scan ends |
139
139
  | `--json` / `--ci` | Machine-readable output; CI annotations and exit codes |
140
140
 
@@ -146,13 +146,36 @@ Useful `diff` flags:
146
146
  column, deleting or truncating rows, and removing an RLS policy are reported
147
147
  but skipped until you add `--allow-destructive`. `migrate run` uses the same
148
148
  gate.
149
+ - **Roles and grants.** What the target grants a role the source doesn't have
150
+ is left alone, since the source says nothing about it. Syncing plain
151
+ PostgreSQL into a Supabase project keeps the Data API roles' access.
152
+ Grants to `anon`, `authenticated` or `service_role` are skipped on a target
153
+ without them. Roles belong to the whole server, not one database, so a role
154
+ only the target has is dropped only with `--allow-destructive`, and one
155
+ that can log in is never dropped for you.
149
156
  - **Dependency order.** Fixes run in dependency order: functions before the
150
157
  triggers that call them, columns before their indexes, drops last. A changed
151
158
  object that has to be dropped and recreated is a single fix.
152
159
  - **One transaction.** If a fix fails, the whole set rolls back and the target
153
- is unchanged. A fix the target can't hold, because it needs an extension or
154
- a Supabase schema the target lacks, is skipped with the reason, and the rest
155
- still applies.
160
+ is unchanged. An extension the server doesn't ship, such as Supabase's own
161
+ on plain PostgreSQL, is skipped with the reason, and the rest still
162
+ applies. So is a fix that needs something the target lacks, such as that
163
+ extension or a Supabase schema, but that one counts as a failure, since
164
+ something of yours didn't arrive.
165
+ - **Rows into a live target.** Reference rows are written the way pg_dump's
166
+ `--disable-triggers` and logical replication write them: the target's own
167
+ triggers on those tables are held off while their rows are synced and put
168
+ back before commit, so an `updated_at` or audit trigger doesn't rewrite
169
+ what was copied, and a webhook doesn't fire once per row. A trigger set
170
+ `ENABLE ALWAYS` still fires. A row that needs one written after it in the
171
+ same table, such as a node moved under a newer one, is checked against the
172
+ table's foreign key at the end instead, and an orphan still rolls
173
+ everything back. Both need the connecting role to own the table; when it
174
+ doesn't, the triggers fire and the run says so.
175
+ - **Sequences follow the rows.** After syncing rows, each serial or identity
176
+ sequence on those tables is moved past the largest id, so the app's next
177
+ insert doesn't collide with a synced row. A sequence is only ever moved
178
+ forward, and one left behind by an earlier sync is reported too.
156
179
  - **`--prove`.** The fixes are replayed on a copy of the target's structure,
157
180
  made on the target's own server, the way the apply will run them, with a
158
181
  sample of each table's rows. Nothing is applied unless the result matches
@@ -189,6 +212,8 @@ A snapshot records a project's structure and configuration: schema, RLS
189
212
  policies, cron, webhooks, extensions, storage, auth, Edge Functions,
190
213
  reference data, Realtime, Vault names and grants. **It is not a
191
214
  backup.** It holds no rows beyond `checks.data.tables`, and no stored files.
215
+ Those rows are written in one fixed text format, so they restore exactly into
216
+ a server whose date, time zone or number settings differ from the source's.
192
217
  Its schema is written as SQL by `pg_dump`, which has to be at least as new as
193
218
  the server; without one the snapshot can still be diffed but not restored, and
194
219
  `snapshot` exits 1 unless `--allow-schema-json-only` says that is enough.
@@ -308,11 +333,11 @@ checks run.
308
333
  ## Diffing a clone
309
334
 
310
335
  A local clone is plain PostgreSQL. Storage, auth, Edge Functions, Vault,
311
- Realtime and Supabase's roles have nothing to compare there, so skip those
312
- checks:
336
+ Realtime, webhooks (which need Supabase's `pg_net`) and Supabase's roles have
337
+ nothing to compare there, so skip those checks:
313
338
 
314
339
  ```json
315
- { "checks": { "exclude": ["storage", "auth", "edge-functions", "vault", "realtime", "roles"] } }
340
+ { "checks": { "exclude": ["storage", "auth", "edge-functions", "vault", "realtime", "webhooks", "roles"] } }
316
341
  ```
317
342
 
318
343
  When the source is a clone and the target isn't, `diff` warns that `--apply`
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  BaseCommand
3
- } from "./chunk-EPGKCP3K.js";
4
- import "./chunk-W42J6U7R.js";
3
+ } from "./chunk-SQHEF5NQ.js";
4
+ import "./chunk-GKYJA56H.js";
5
5
  import "./chunk-KHXEES2M.js";
6
6
  import "./chunk-SQ52YBKK.js";
7
7
  export {
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  pgQuery,
3
+ pgQueryAsWritten,
3
4
  readLocalMigrations
4
- } from "./chunk-W42J6U7R.js";
5
+ } from "./chunk-GKYJA56H.js";
5
6
  import {
6
7
  MIGRATIONS_SCHEMA,
7
8
  MIGRATIONS_TABLE
@@ -55,7 +56,7 @@ async function getPendingMigrations(dir, applied, readDirFn) {
55
56
  return local.filter((m) => !applied.has(m.version)).map((m) => ({ ...m, path: join(dir, m.filename) }));
56
57
  }
57
58
  var defaultReadFile = (path) => readFile(path, "utf8");
58
- async function runMigration(dbUrl, migration, queryFn = pgQuery, readFileFn = defaultReadFile) {
59
+ async function runMigration(dbUrl, migration, queryFn = pgQueryAsWritten, readFileFn = defaultReadFile) {
59
60
  const sql = await readFileFn(migration.path);
60
61
  const start = performance.now();
61
62
  await queryFn(dbUrl, sql);
@@ -119,4 +120,4 @@ export {
119
120
  runMigration,
120
121
  baselineMigrations
121
122
  };
122
- //# sourceMappingURL=chunk-HZ2FKMY3.js.map
123
+ //# sourceMappingURL=chunk-4EEDB7Q3.js.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  newPgClient
3
- } from "./chunk-W42J6U7R.js";
3
+ } from "./chunk-GKYJA56H.js";
4
4
 
5
5
  // src/utils/sql-split.ts
6
6
  var ROUTINE_HEAD = /^CREATE\s+(?:OR\s+REPLACE\s+)?(?:FUNCTION|PROCEDURE)\b/i;
@@ -393,4 +393,4 @@ export {
393
393
  checkPgDumpCompat,
394
394
  pgToolConnection
395
395
  };
396
- //# sourceMappingURL=chunk-AMG4PVQS.js.map
396
+ //# sourceMappingURL=chunk-56U2BYRZ.js.map
@@ -6,13 +6,13 @@ import {
6
6
  formatSkips,
7
7
  renderDetailed,
8
8
  renderSummary
9
- } from "./chunk-HKG4KGDV.js";
9
+ } from "./chunk-OZW36QCK.js";
10
10
  import {
11
11
  createDefaultRegistry,
12
12
  proveBeforeApply,
13
13
  scan,
14
14
  summarizeByKind
15
- } from "./chunk-PSSMJRMM.js";
15
+ } from "./chunk-JLY4IG73.js";
16
16
  import {
17
17
  CHECK_META,
18
18
  CHECK_NAMES,
@@ -24,17 +24,17 @@ import {
24
24
  psqlBeside,
25
25
  resolveTableFilter,
26
26
  withDatabase
27
- } from "./chunk-C7VBY54T.js";
27
+ } from "./chunk-VCNTXXEC.js";
28
28
  import {
29
29
  renderTip
30
30
  } from "./chunk-RQCXGO37.js";
31
31
  import {
32
32
  BaseCommand
33
- } from "./chunk-EPGKCP3K.js";
33
+ } from "./chunk-SQHEF5NQ.js";
34
34
  import {
35
35
  isCloneDatabase,
36
36
  sourceLooksLikeCloneOf
37
- } from "./chunk-S2GQI7ZO.js";
37
+ } from "./chunk-EI7EP5IU.js";
38
38
  import {
39
39
  describeMissingPgDump,
40
40
  getServerMajorVersion,
@@ -42,12 +42,13 @@ import {
42
42
  parseFlagList,
43
43
  resolvePgDumpPath,
44
44
  splitSqlStatements
45
- } from "./chunk-AMG4PVQS.js";
45
+ } from "./chunk-56U2BYRZ.js";
46
46
  import {
47
47
  Preflight,
48
48
  pgQuery,
49
+ pgQueryAsWritten,
49
50
  quoteName
50
- } from "./chunk-W42J6U7R.js";
51
+ } from "./chunk-GKYJA56H.js";
51
52
  import {
52
53
  CLONE_SKIP_FLAGS,
53
54
  DEFAULT_IGNORE_SCHEMAS,
@@ -188,7 +189,7 @@ async function loadStatements(url, files) {
188
189
  const failed = [];
189
190
  for (const p of pending) {
190
191
  try {
191
- await pgQuery(url, p.sql);
192
+ await pgQueryAsWritten(url, p.sql);
192
193
  } catch (err) {
193
194
  failed.push({ ...p, error: errMsg(err) });
194
195
  }
@@ -218,6 +219,7 @@ async function unavailableExtensions(targetUrl, copyUrl) {
218
219
 
219
220
  // src/commands/diff.ts
220
221
  var SCHEMA_FILES_ENV = "schema files";
222
+ var ROLLED_BACK_SHOWN = 20;
221
223
  var SCHEMA_FILE_CHECKS = ["schema", "rls", "extensions"];
222
224
  function describeCheckOutcome(status, issueCount, skipReason) {
223
225
  if (status === "error") return { glyph: warn("\u2717"), text: warn("error") };
@@ -376,6 +378,8 @@ var Diff = class _Diff extends BaseCommand {
376
378
  const verb = dryRun ? "Would apply" : "Applied";
377
379
  for (const warning of result.warnings ?? []) this.log(`${warn(warning)}
378
380
  `);
381
+ for (const note of result.notes ?? []) this.log(`${dim(note)}`);
382
+ if (result.notes?.length) this.log("");
379
383
  if (result.applied.length > 0) {
380
384
  const heading = `${verb} ${result.applied.length} fix(es)${dryRun ? ", in this order" : ""}:`;
381
385
  this.log(dryRun ? bold(heading) : ok(heading));
@@ -394,9 +398,12 @@ ${dim(`Skipped ${result.skipped.length} issue(s):`)}`);
394
398
  if (result.rolledBack?.length) {
395
399
  this.log(`
396
400
  ${warn(`Rolled back ${result.rolledBack.length} fix(es)`)} \u2014 the target is unchanged:`);
397
- for (const item of result.rolledBack) {
401
+ for (const item of result.rolledBack.slice(0, ROLLED_BACK_SHOWN)) {
398
402
  this.log(` ${dim("\u21A9")} ${dim(`[${item.check}]`)} ${item.issueId}`);
399
403
  }
404
+ if (result.rolledBack.length > ROLLED_BACK_SHOWN) {
405
+ this.log(` ${dim(`\u21A9 \u2026 and ${result.rolledBack.length - ROLLED_BACK_SHOWN} more (all of them under --json)`)}`);
406
+ }
400
407
  }
401
408
  if (result.errors.length > 0) {
402
409
  this.log(`
@@ -754,4 +761,4 @@ export {
754
761
  inertApplyFlags,
755
762
  Diff
756
763
  };
757
- //# sourceMappingURL=chunk-FKL3TQB7.js.map
764
+ //# sourceMappingURL=chunk-56UP3KK3.js.map
@@ -1,16 +1,16 @@
1
1
  import {
2
2
  captureSnapshot
3
- } from "./chunk-X4L3JT4R.js";
3
+ } from "./chunk-TREPSQWG.js";
4
4
  import {
5
5
  checkPgDumpCompat,
6
6
  pgToolConnection,
7
7
  splitSqlStatements
8
- } from "./chunk-AMG4PVQS.js";
8
+ } from "./chunk-56U2BYRZ.js";
9
9
  import {
10
10
  newPgClient,
11
11
  pgQuery,
12
12
  quoteName
13
- } from "./chunk-W42J6U7R.js";
13
+ } from "./chunk-GKYJA56H.js";
14
14
  import {
15
15
  BRANCHES_FILE,
16
16
  CLONE_PROGRESS_INTERVAL_MS,
@@ -745,4 +745,4 @@ export {
745
745
  isCloneDatabase,
746
746
  sourceLooksLikeCloneOf
747
747
  };
748
- //# sourceMappingURL=chunk-S2GQI7ZO.js.map
748
+ //# sourceMappingURL=chunk-EI7EP5IU.js.map
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  loadMigrations
3
- } from "./chunk-3XQHSJOJ.js";
3
+ } from "./chunk-YCJXGDVL.js";
4
4
  import {
5
5
  ensureMigrationsTable,
6
6
  getAppliedVersions
7
- } from "./chunk-HZ2FKMY3.js";
7
+ } from "./chunk-4EEDB7Q3.js";
8
8
  import {
9
9
  ABSENT_ON_TARGET,
10
10
  EXTENSION_UNAVAILABLE,
@@ -18,23 +18,23 @@ import {
18
18
  rolesNamedBy,
19
19
  sqlSkeleton,
20
20
  statementSubject
21
- } from "./chunk-C7VBY54T.js";
21
+ } from "./chunk-VCNTXXEC.js";
22
22
  import {
23
23
  loadSnapshot
24
- } from "./chunk-X4L3JT4R.js";
24
+ } from "./chunk-TREPSQWG.js";
25
25
  import {
26
26
  isCommentOnly,
27
27
  isPsqlMetaCommand,
28
28
  splitSqlStatements,
29
29
  stripPsqlMetaCommands
30
- } from "./chunk-AMG4PVQS.js";
30
+ } from "./chunk-56U2BYRZ.js";
31
31
  import {
32
32
  newPgClient,
33
33
  pgQuery,
34
34
  quoteIdent,
35
35
  quoteLiteral,
36
36
  quoteName
37
- } from "./chunk-W42J6U7R.js";
37
+ } from "./chunk-GKYJA56H.js";
38
38
  import {
39
39
  DEFAULT_IGNORE_SCHEMAS,
40
40
  MIGRATIONS_TABLE,
@@ -900,4 +900,4 @@ export {
900
900
  previewMigrationRestore,
901
901
  summarizeStatement
902
902
  };
903
- //# sourceMappingURL=chunk-VEXHBRNW.js.map
903
+ //# sourceMappingURL=chunk-EJBEEWGI.js.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  comparedSchemas,
3
3
  fingerprint
4
- } from "./chunk-C7VBY54T.js";
4
+ } from "./chunk-VCNTXXEC.js";
5
5
 
6
6
  // src/written-against.ts
7
7
  import { createHash } from "crypto";
@@ -24,4 +24,4 @@ export {
24
24
  writtenAgainstLine,
25
25
  writtenAgainst
26
26
  };
27
- //# sourceMappingURL=chunk-NCMERJNX.js.map
27
+ //# sourceMappingURL=chunk-EJMRK6LG.js.map
@@ -39,13 +39,13 @@ function newPgClient(dbUrl, queryTimeoutMs) {
39
39
  client.on("error", () => void 0);
40
40
  return client;
41
41
  }
42
- var PINNED_OUTPUT = "SET extra_float_digits = 3; SET bytea_output = hex";
43
- function newPgPool(config) {
42
+ var PINNED_OUTPUT = "SET extra_float_digits = 3; SET bytea_output = hex; SET DateStyle = 'ISO, MDY'; SET IntervalStyle = postgres; SET TimeZone = 'UTC'";
43
+ function newPgPool(config, pinned = true) {
44
44
  const pool = new pg.Pool(config);
45
45
  pool.on("error", () => void 0);
46
46
  pool.on("connect", (client) => {
47
47
  client.on("error", () => void 0);
48
- client.query(PINNED_OUTPUT).catch(() => void 0);
48
+ if (pinned) client.query(PINNED_OUTPUT).catch(() => void 0);
49
49
  });
50
50
  return pool;
51
51
  }
@@ -144,6 +144,16 @@ async function inRolledBackTransaction(dbUrl, fn) {
144
144
  client.release();
145
145
  }
146
146
  }
147
+ var asWritten = /* @__PURE__ */ new Map();
148
+ var pgQueryAsWritten = async (dbUrl, sql, params) => {
149
+ let pool = asWritten.get(dbUrl);
150
+ if (!pool) {
151
+ pool = newPgPool({ ...pgClientConfig(dbUrl), max: POOL_MAX, allowExitOnIdle: true }, false);
152
+ asWritten.set(dbUrl, pool);
153
+ }
154
+ const { rows } = await pool.query(sql, params);
155
+ return rows;
156
+ };
147
157
 
148
158
  // src/local-pg.ts
149
159
  import { execFile } from "child_process";
@@ -611,6 +621,7 @@ export {
611
621
  releaseSnapshots,
612
622
  pgQuery,
613
623
  inRolledBackTransaction,
624
+ pgQueryAsWritten,
614
625
  DEFAULT_LOCAL_PORT,
615
626
  LOCAL_PG_USER,
616
627
  LOCAL_PG_PASSWORD,
@@ -629,4 +640,4 @@ export {
629
640
  MigrationsCheck,
630
641
  readLocalMigrations
631
642
  };
632
- //# sourceMappingURL=chunk-W42J6U7R.js.map
643
+ //# sourceMappingURL=chunk-GKYJA56H.js.map
@@ -22,7 +22,7 @@ import {
22
22
  sqlToIssues,
23
23
  withPlatformExtensionCalls,
24
24
  withoutAbsentRoles
25
- } from "./chunk-C7VBY54T.js";
25
+ } from "./chunk-VCNTXXEC.js";
26
26
  import {
27
27
  RealtimeCheck,
28
28
  createPolicySql,
@@ -34,10 +34,10 @@ import {
34
34
  readJsonArray,
35
35
  readJsonObject,
36
36
  schemaPolicySql
37
- } from "./chunk-X4L3JT4R.js";
37
+ } from "./chunk-TREPSQWG.js";
38
38
  import {
39
39
  splitSqlStatements
40
- } from "./chunk-AMG4PVQS.js";
40
+ } from "./chunk-56U2BYRZ.js";
41
41
  import {
42
42
  Check,
43
43
  CheckSkipped,
@@ -52,7 +52,7 @@ import {
52
52
  releaseSnapshots,
53
53
  setSnapshotsOff,
54
54
  sqlLiteral
55
- } from "./chunk-W42J6U7R.js";
55
+ } from "./chunk-GKYJA56H.js";
56
56
  import {
57
57
  DEFAULT_IGNORE_SCHEMAS,
58
58
  SCORE_MAX,
@@ -1890,6 +1890,50 @@ async function filterChangedTables(sourceUrl, targetUrl, tables, queryFn = pgQue
1890
1890
  return { changed, skipped };
1891
1891
  }
1892
1892
 
1893
+ // src/checks/data-sequences.ts
1894
+ var OWNED = (table2) => `pg_depend d
1895
+ JOIN pg_sequence q ON q.seqrelid = d.objid
1896
+ JOIN pg_attribute a ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid AND NOT a.attisdropped
1897
+ WHERE d.classid = 'pg_class'::regclass AND d.refclassid = 'pg_class'::regclass AND d.deptype IN ('a', 'i')
1898
+ AND d.refobjid = to_regclass(${table2})`;
1899
+ var SOURCE_SQL = (table2) => `SELECT a.attname AS col FROM ${OWNED(quoteLiteral(quoteIdent(table2)))} ORDER BY a.attnum`;
1900
+ var TARGET_SQL = `
1901
+ SELECT q.seqincrement > 0 AS ascending,
1902
+ (coalesce(pg_sequence_last_value(q.seqrelid) + q.seqincrement, q.seqstart))::text AS next
1903
+ FROM ${OWNED("$1")} AND a.attname = $2`;
1904
+ function sequenceFixSql(table2, col) {
1905
+ return `SELECT setval(q.seqrelid, top) FROM (SELECT max(${quoteName(col)}) AS top FROM ${quoteIdent(table2)}) x, ${OWNED(quoteLiteral(quoteIdent(table2))).replace(/\s+/g, " ")} AND a.attname = ${quoteLiteral(col)} AND top IS NOT NULL AND q.seqincrement > 0 AND top >= coalesce(pg_sequence_last_value(q.seqrelid) + q.seqincrement, q.seqstart);`;
1906
+ }
1907
+ async function sequenceIssues(sourceUrl, targetUrl, tables, queryFn) {
1908
+ const issues = [];
1909
+ for (const table2 of tables) {
1910
+ const columns = await queryFn(sourceUrl, SOURCE_SQL(table2));
1911
+ const behind = [];
1912
+ const fixes = [];
1913
+ for (const column of columns) {
1914
+ const [{ max }] = await queryFn(
1915
+ sourceUrl,
1916
+ `SELECT max(${quoteName(column.col)})::text AS max FROM ${quoteIdent(table2)}`
1917
+ );
1918
+ if (max === null) continue;
1919
+ const [target] = await queryFn(targetUrl, TARGET_SQL, [quoteIdent(table2), column.col]);
1920
+ if (target && (!target.ascending || BigInt(target.next) > BigInt(max))) continue;
1921
+ behind.push(target ? `${column.col} (next ${target.next}, rows up to ${max})` : `${column.col} (rows up to ${max}, on a sequence the sync creates)`);
1922
+ fixes.push(sequenceFixSql(table2, column.col));
1923
+ }
1924
+ if (fixes.length === 0) continue;
1925
+ issues.push({
1926
+ id: `data-sequence-${table2}`,
1927
+ check: "data",
1928
+ severity: "warning",
1929
+ title: `Sequence behind the rows in ${table2}`,
1930
+ description: `The ids synced into ${table2} reach past what its sequence would hand out next: ${behind.join(", ")}. The next insert would fail with a duplicate key, so the sequence is moved past them after the rows are written.`,
1931
+ sql: { up: fixes.join("\n"), down: "" }
1932
+ });
1933
+ }
1934
+ return issues;
1935
+ }
1936
+
1893
1937
  // src/checks/data.ts
1894
1938
  var DataCheck = class extends Check {
1895
1939
  constructor(runFn = runDbDiff, queryFn = pgQuery) {
@@ -1913,7 +1957,8 @@ var DataCheck = class extends Check {
1913
1957
  tables,
1914
1958
  this.queryFn
1915
1959
  );
1916
- if (changed.length === 0) return [];
1960
+ const sequences = await sequenceIssues(ctx.source.dbUrl, ctx.target.dbUrl, tables, this.queryFn);
1961
+ if (changed.length === 0) return sequences;
1917
1962
  try {
1918
1963
  const result = await this.runFn({
1919
1964
  sourceUrl: ctx.source.dbUrl,
@@ -1923,10 +1968,10 @@ var DataCheck = class extends Check {
1923
1968
  ...dataScope(changed, ctx.config.ignoreSchemas ?? DEFAULT_IGNORE_SCHEMAS),
1924
1969
  timeoutSeconds: ctx.target?.checks?.schema?.timeout
1925
1970
  });
1926
- return sqlToIssues(result, "data");
1971
+ return [...sqlToIssues(result, "data"), ...sequences];
1927
1972
  } catch (err) {
1928
1973
  if (err instanceof Error && err.message.includes("@dbdiff/cli is not installed")) {
1929
- return [];
1974
+ return sequences;
1930
1975
  }
1931
1976
  throw err;
1932
1977
  }
@@ -2468,4 +2513,4 @@ export {
2468
2513
  createDefaultRegistry,
2469
2514
  proveBeforeApply
2470
2515
  };
2471
- //# sourceMappingURL=chunk-PSSMJRMM.js.map
2516
+ //# sourceMappingURL=chunk-JLY4IG73.js.map
@@ -1,20 +1,20 @@
1
1
  import {
2
2
  targetFingerprint,
3
3
  writtenAgainstLine
4
- } from "./chunk-NCMERJNX.js";
4
+ } from "./chunk-EJMRK6LG.js";
5
5
  import {
6
6
  RolesCheck,
7
7
  isDestructiveSql,
8
8
  runDbDiff
9
- } from "./chunk-C7VBY54T.js";
9
+ } from "./chunk-VCNTXXEC.js";
10
10
  import {
11
11
  slugify,
12
12
  splitSqlStatements
13
- } from "./chunk-AMG4PVQS.js";
13
+ } from "./chunk-56U2BYRZ.js";
14
14
  import {
15
15
  DEFAULT_MIGRATIONS_DIR,
16
16
  pgQuery
17
- } from "./chunk-W42J6U7R.js";
17
+ } from "./chunk-GKYJA56H.js";
18
18
  import {
19
19
  DEFAULT_IGNORE_SCHEMAS
20
20
  } from "./chunk-SQ52YBKK.js";
@@ -89,4 +89,4 @@ export {
89
89
  writeMigrationFile,
90
90
  generateTimestamp
91
91
  };
92
- //# sourceMappingURL=chunk-Z3SB66RS.js.map
92
+ //# sourceMappingURL=chunk-JPZZL3KJ.js.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  CHECK_META,
3
3
  isComparisonCheck
4
- } from "./chunk-C7VBY54T.js";
4
+ } from "./chunk-VCNTXXEC.js";
5
5
  import {
6
6
  CHECK_LINE_PADDING,
7
7
  bold,
@@ -241,4 +241,4 @@ export {
241
241
  renderDetailed,
242
242
  formatSkips
243
243
  };
244
- //# sourceMappingURL=chunk-HKG4KGDV.js.map
244
+ //# sourceMappingURL=chunk-OZW36QCK.js.map
@@ -1,22 +1,22 @@
1
1
  import {
2
2
  targetFingerprint,
3
3
  writtenAgainst
4
- } from "./chunk-NCMERJNX.js";
4
+ } from "./chunk-EJMRK6LG.js";
5
5
  import {
6
6
  ensureMigrationsTable,
7
7
  getAppliedVersions,
8
8
  getPendingMigrations,
9
9
  runMigration
10
- } from "./chunk-HZ2FKMY3.js";
10
+ } from "./chunk-4EEDB7Q3.js";
11
11
  import {
12
12
  isDestructiveSql
13
- } from "./chunk-C7VBY54T.js";
13
+ } from "./chunk-VCNTXXEC.js";
14
14
  import {
15
15
  BaseCommand
16
- } from "./chunk-EPGKCP3K.js";
16
+ } from "./chunk-SQHEF5NQ.js";
17
17
  import {
18
18
  splitSqlStatements
19
- } from "./chunk-AMG4PVQS.js";
19
+ } from "./chunk-56U2BYRZ.js";
20
20
  import {
21
21
  DEFAULT_IGNORE_SCHEMAS,
22
22
  bold,
@@ -180,4 +180,4 @@ export {
180
180
  findDestructiveStatements,
181
181
  truncateSql
182
182
  };
183
- //# sourceMappingURL=chunk-D3JEYV4M.js.map
183
+ //# sourceMappingURL=chunk-P66NJJ6I.js.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  DEFAULT_MIGRATIONS_DIR,
3
3
  Preflight
4
- } from "./chunk-W42J6U7R.js";
4
+ } from "./chunk-GKYJA56H.js";
5
5
  import {
6
6
  installProcessGuard,
7
7
  noteJsonWritten
@@ -213,4 +213,4 @@ export {
213
213
  readRunLog,
214
214
  BaseCommand
215
215
  };
216
- //# sourceMappingURL=chunk-EPGKCP3K.js.map
216
+ //# sourceMappingURL=chunk-SQHEF5NQ.js.map
@@ -5,7 +5,7 @@ import {
5
5
  normalizeRoles,
6
6
  pgToolConnection,
7
7
  resolvePgDumpPath
8
- } from "./chunk-AMG4PVQS.js";
8
+ } from "./chunk-56U2BYRZ.js";
9
9
  import {
10
10
  Check,
11
11
  inRolledBackTransaction,
@@ -16,7 +16,7 @@ import {
16
16
  quoteLiteral,
17
17
  quoteName,
18
18
  readOrAbsent
19
- } from "./chunk-W42J6U7R.js";
19
+ } from "./chunk-GKYJA56H.js";
20
20
  import {
21
21
  DEFAULT_IGNORE_SCHEMAS,
22
22
  MIGRATIONS_SUBDIR,
@@ -1252,4 +1252,4 @@ export {
1252
1252
  DEFAULT_KEEP_COUNT,
1253
1253
  pruneSnapshots
1254
1254
  };
1255
- //# sourceMappingURL=chunk-X4L3JT4R.js.map
1255
+ //# sourceMappingURL=chunk-TREPSQWG.js.map