@akal/supaforge 0.0.34 → 0.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +32 -13
  2. package/dist/base-command.d.ts +1 -1
  3. package/dist/base-command.js +4 -4
  4. package/dist/{branch-C4ndfoj5.d.ts → branch-BppjQBv_.d.ts} +1 -1
  5. package/dist/{chunk-ICSCHS72.js → chunk-5UHIWFQG.js} +76 -186
  6. package/dist/{chunk-S5L4A3UR.js → chunk-AL6INEFX.js} +3 -3
  7. package/dist/{chunk-OB5GP7UB.js → chunk-CAQ3QQFP.js} +437 -45
  8. package/dist/{chunk-TPX2MAO4.js → chunk-CCB7ARZU.js} +54 -5
  9. package/dist/{chunk-V2SBZERO.js → chunk-EWNZIRCD.js} +4 -4
  10. package/dist/{chunk-EFKQOQGG.js → chunk-FD2JKEJA.js} +7 -5
  11. package/dist/{chunk-3WKWPSFN.js → chunk-GGZ7YWBZ.js} +191 -32
  12. package/dist/{chunk-F46FFUKC.js → chunk-JX5L253P.js} +56 -18
  13. package/dist/{chunk-GQPGH6YR.js → chunk-KHXEES2M.js} +2 -2
  14. package/dist/{chunk-MG3SFV5J.js → chunk-KT7NMELZ.js} +77 -7
  15. package/dist/{chunk-PYAGSMFB.js → chunk-M2564IGT.js} +159 -19
  16. package/dist/{chunk-7DL63NWP.js → chunk-MRXIE55Y.js} +7 -3
  17. package/dist/{chunk-6ERVCWLY.js → chunk-MZPWRWBL.js} +7 -7
  18. package/dist/{chunk-5STH2A6K.js → chunk-OXS42LJH.js} +4 -4
  19. package/dist/{chunk-HNJ6BNNZ.js → chunk-Q43MN6NE.js} +2 -2
  20. package/dist/{chunk-ZI435CAU.js → chunk-RQCXGO37.js} +2 -2
  21. package/dist/{chunk-KNXHLVF7.js → chunk-SQ52YBKK.js} +3 -2
  22. package/dist/{chunk-N5VNNOA2.js → chunk-YUJ2MHLM.js} +1236 -22
  23. package/dist/chunk-Z2YUOA4W.js +242 -0
  24. package/dist/commands/clone.d.ts +2 -2
  25. package/dist/commands/clone.js +10 -9
  26. package/dist/commands/diff.d.ts +25 -22
  27. package/dist/commands/diff.js +14 -15
  28. package/dist/commands/hukam.d.ts +2 -3
  29. package/dist/commands/hukam.js +14 -15
  30. package/dist/commands/init.d.ts +1 -1
  31. package/dist/commands/init.js +2 -2
  32. package/dist/commands/mcp.js +9 -10
  33. package/dist/commands/migrate/baseline.d.ts +1 -1
  34. package/dist/commands/migrate/baseline.js +5 -5
  35. package/dist/commands/migrate/create.d.ts +1 -1
  36. package/dist/commands/migrate/create.js +27 -13
  37. package/dist/commands/migrate/list.d.ts +1 -1
  38. package/dist/commands/migrate/list.js +5 -5
  39. package/dist/commands/migrate/run.d.ts +4 -1
  40. package/dist/commands/migrate/run.js +9 -8
  41. package/dist/commands/report.d.ts +1 -1
  42. package/dist/commands/report.js +5 -5
  43. package/dist/commands/restore.d.ts +1 -1
  44. package/dist/commands/restore.js +14 -13
  45. package/dist/commands/snapshot.d.ts +1 -1
  46. package/dist/commands/snapshot.js +8 -8
  47. package/dist/commands/sync.d.ts +2 -3
  48. package/dist/commands/sync.js +14 -15
  49. package/dist/{config-CTbeRlJk.d.ts → config-l3ggjw9r.d.ts} +2 -0
  50. package/dist/index.d.ts +38 -4
  51. package/dist/index.js +13 -14
  52. package/package.json +2 -2
  53. package/dist/chunk-DQJSSTIJ.js +0 -940
  54. package/dist/chunk-DQQ5E563.js +0 -64
@@ -1,24 +1,26 @@
1
1
  import {
2
2
  loadMigrations
3
- } from "./chunk-V2SBZERO.js";
3
+ } from "./chunk-EWNZIRCD.js";
4
4
  import {
5
5
  ensureMigrationsTable,
6
6
  getAppliedVersions
7
- } from "./chunk-S5L4A3UR.js";
7
+ } from "./chunk-AL6INEFX.js";
8
8
  import {
9
9
  dropUnsupportedSetStatements,
10
10
  knownParameters
11
- } from "./chunk-3WKWPSFN.js";
11
+ } from "./chunk-GGZ7YWBZ.js";
12
12
  import {
13
13
  ABSENT_ON_TARGET,
14
14
  EXTENSION_UNAVAILABLE,
15
- PLATFORM_ROLES
16
- } from "./chunk-DQJSSTIJ.js";
17
- import {
15
+ OLDER_SERVER,
16
+ PLATFORM_ROLES,
17
+ estimateLocks,
18
+ lockBudgetWarning,
19
+ readLockBudget,
18
20
  rolesNamedBy,
19
21
  sqlSkeleton,
20
22
  statementSubject
21
- } from "./chunk-N5VNNOA2.js";
23
+ } from "./chunk-YUJ2MHLM.js";
22
24
  import {
23
25
  isCommentOnly,
24
26
  isPsqlMetaCommand,
@@ -27,20 +29,20 @@ import {
27
29
  } from "./chunk-WK5YTINL.js";
28
30
  import {
29
31
  loadSnapshot
30
- } from "./chunk-ICSCHS72.js";
32
+ } from "./chunk-5UHIWFQG.js";
31
33
  import {
32
34
  newPgClient,
33
35
  pgQuery,
34
36
  quoteIdent,
35
37
  quoteLiteral,
36
38
  quoteName
37
- } from "./chunk-MG3SFV5J.js";
39
+ } from "./chunk-KT7NMELZ.js";
38
40
  import {
39
41
  DEFAULT_IGNORE_SCHEMAS,
40
42
  MIGRATIONS_TABLE,
41
43
  SUPABASE_PLATFORM_SCHEMAS,
42
44
  errMsg
43
- } from "./chunk-KNXHLVF7.js";
45
+ } from "./chunk-SQ52YBKK.js";
44
46
 
45
47
  // src/restore.ts
46
48
  import { readFile as readFile2, access } from "fs/promises";
@@ -354,6 +356,22 @@ async function resetRelationGrants(client, schemas) {
354
356
  }
355
357
 
356
358
  // src/restore.ts
359
+ async function snapshotIsNewer(snapshotDir, client) {
360
+ const head = (await readFile2(join2(snapshotDir, "schema.sql"), "utf-8").catch(() => "")).slice(0, 2e3);
361
+ const dumped = /Dumped from database version (\d+)/.exec(head)?.[1];
362
+ if (!dumped) return false;
363
+ const { rows: [row] } = await client.query(`SELECT current_setting('server_version_num') AS v`);
364
+ return Number(dumped) > Math.floor(Number(row?.v ?? 0) / 1e4);
365
+ }
366
+ async function locksForSnapshot(snapshotDir, layers, manifest) {
367
+ const statements = [];
368
+ for (const layer of layers) {
369
+ if (!manifest.layers[layer === "storage-policies" ? "storage" : layer]?.captured) continue;
370
+ const raw = await readFile2(join2(snapshotDir, layerRestoreFile(layer)), "utf-8").catch(() => "");
371
+ statements.push(...extractExecutableStatements(raw));
372
+ }
373
+ return estimateLocks(statements);
374
+ }
357
375
  async function getPublicTables(targetUrl) {
358
376
  const ignoreList = DEFAULT_IGNORE_SCHEMAS.map((s) => `'${s}'`).join(", ");
359
377
  const client = newPgClient(targetUrl);
@@ -377,8 +395,17 @@ async function restoreFromSnapshot(options) {
377
395
  const sqlOrder = RESTORE_SQL_ORDER;
378
396
  const client = newPgClient(options.targetUrl);
379
397
  await client.connect();
398
+ const olderTarget = await snapshotIsNewer(options.snapshotDir, client);
380
399
  const transactional = !options.noTransaction;
381
- if (transactional) await client.query("BEGIN");
400
+ if (transactional) {
401
+ const warning = lockBudgetWarning(
402
+ await locksForSnapshot(options.snapshotDir, sqlOrder, manifest),
403
+ await readLockBudget(client),
404
+ "This restore"
405
+ );
406
+ if (warning) result.warnings = [warning];
407
+ await client.query("BEGIN");
408
+ }
382
409
  const missing = /* @__PURE__ */ new Set();
383
410
  const leftOut = /* @__PURE__ */ new Map();
384
411
  const createdBySchemaLayer = /* @__PURE__ */ new Set();
@@ -459,7 +486,7 @@ async function restoreFromSnapshot(options) {
459
486
  }
460
487
  }
461
488
  for (const sql of statements) {
462
- await applyStatement(client, sql, transactional, result, missing, leftOut);
489
+ await applyStatement(client, sql, transactional, result, missing, leftOut, olderTarget);
463
490
  }
464
491
  } catch (err) {
465
492
  if (err instanceof RestoreAborted) throw err;
@@ -492,7 +519,7 @@ async function restoreFromSnapshot(options) {
492
519
  delete result.incomplete;
493
520
  }
494
521
  if (!(err instanceof RestoreAborted)) {
495
- result.errors.push({ type: "sql", label: "Restore", error: errMsg(err) });
522
+ result.errors.push({ type: "sql", label: "Restore", error: restoreError(err, transactional) });
496
523
  }
497
524
  } finally {
498
525
  await client.end();
@@ -746,10 +773,13 @@ function mentionedPlatformSchema(sql, targetSchemas) {
746
773
  return SUPABASE_ONLY_SCHEMAS.find((schema) => !targetSchemas.has(schema) && new RegExp(String.raw`(?:"${schema}"|\b${schema})\s*\.`, "i").test(skeleton));
747
774
  }
748
775
  var ATTACHMENT = new RegExp(String.raw`^\s*(?:` + String.raw`GRANT|REVOKE|COMMENT\s+ON|SELECT|ALTER\s+PUBLICATION|ALTER\s+DEFAULT\s+PRIVILEGES|` + String.raw`ALTER\s+POLICY|ALTER\s+TABLE\s+(?:ONLY\s+)?\S+\s+ADD\s+CONSTRAINT\b[\s\S]*\bFOREIGN\s+KEY|` + String.raw`CREATE\s+(?:OR\s+REPLACE\s+)?(?:POLICY|(?:CONSTRAINT\s+)?TRIGGER|VIEW|MATERIALIZED\s+VIEW|RULE)` + String.raw`)\b`, "i");
749
- function tolerableFailure(sql, err, targetSchemas, missing = /* @__PURE__ */ new Set(), leftOut = /* @__PURE__ */ new Map()) {
776
+ function tolerableFailure(sql, err, targetSchemas, missing = /* @__PURE__ */ new Set(), leftOut = /* @__PURE__ */ new Map(), olderTarget = false) {
750
777
  const code = err?.code;
751
778
  if (!code) return void 0;
752
779
  const skeleton = sqlSkeleton(sql);
780
+ if (olderTarget && OLDER_SERVER.has(code)) {
781
+ return `this server's PostgreSQL is older than the snapshot's and does not support it: ${errMsg(err)}`;
782
+ }
753
783
  if (/^\s*CREATE\s+EXTENSION\b/i.test(skeleton)) {
754
784
  return EXTENSION_UNAVAILABLE.has(code) ? `extension not available on this server: ${errMsg(err)}` : void 0;
755
785
  }
@@ -784,7 +814,7 @@ function createdObjects(sql) {
784
814
  const bare = (n) => n.replace(/^"|"$/g, "");
785
815
  return [`${m[1] ? bare(m[1]) : "public"}.${bare(m[2])}`];
786
816
  }
787
- async function applyStatement(client, sql, transactional, result, missing, leftOut = /* @__PURE__ */ new Map()) {
817
+ async function applyStatement(client, sql, transactional, result, missing, leftOut = /* @__PURE__ */ new Map(), olderTarget = false) {
788
818
  if (transactional) await client.query("SAVEPOINT sf_restore_statement");
789
819
  try {
790
820
  const needed = extensionTargetSchema(sql);
@@ -797,7 +827,7 @@ async function applyStatement(client, sql, transactional, result, missing, leftO
797
827
  if (transactional) await client.query("ROLLBACK TO SAVEPOINT sf_restore_statement");
798
828
  const { rows } = await client.query("SELECT nspname FROM pg_namespace");
799
829
  const targetSchemas = new Set(rows.map((r) => r.nspname));
800
- const tolerated = tolerableFailure(sql, err, targetSchemas, missing, leftOut);
830
+ const tolerated = tolerableFailure(sql, err, targetSchemas, missing, leftOut, olderTarget);
801
831
  if (tolerated) {
802
832
  result.skipped.push({ type: "sql", label: summarizeStatement(sql), reason: tolerated });
803
833
  const cause = /(this target lacks .+)$/.exec(tolerated)?.[1] ?? tolerated;
@@ -809,7 +839,7 @@ async function applyStatement(client, sql, transactional, result, missing, leftO
809
839
  else result.incomplete = true;
810
840
  return;
811
841
  }
812
- result.errors.push({ type: "sql", label: summarizeStatement(sql), error: errMsg(err) });
842
+ result.errors.push({ type: "sql", label: summarizeStatement(sql), error: restoreError(err, transactional) });
813
843
  if (transactional) throw new RestoreAborted();
814
844
  }
815
845
  }
@@ -855,6 +885,14 @@ END $$;`;
855
885
  }
856
886
  var RestoreAborted = class extends Error {
857
887
  };
888
+ function restoreError(err, transactional) {
889
+ const message = errMsg(err);
890
+ const code = err?.code;
891
+ if (code === "53200" && transactional) {
892
+ return `${message} \u2014 this restore holds more locks than the server allows in one transaction. Raise max_locks_per_transaction on the target (it takes a restart), or re-run with --no-transaction to commit as it goes, giving up rolling the whole restore back on a failure.`;
893
+ }
894
+ return message;
895
+ }
858
896
 
859
897
  export {
860
898
  getPublicTables,
@@ -864,4 +902,4 @@ export {
864
902
  previewMigrationRestore,
865
903
  summarizeStatement
866
904
  };
867
- //# sourceMappingURL=chunk-F46FFUKC.js.map
905
+ //# sourceMappingURL=chunk-JX5L253P.js.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  describeFailure,
3
3
  redactUrls
4
- } from "./chunk-KNXHLVF7.js";
4
+ } from "./chunk-SQ52YBKK.js";
5
5
 
6
6
  // src/process-guard.ts
7
7
  var installed = false;
@@ -47,4 +47,4 @@ export {
47
47
  keepRunningOnStrayErrors,
48
48
  installProcessGuard
49
49
  };
50
- //# sourceMappingURL=chunk-GQPGH6YR.js.map
50
+ //# sourceMappingURL=chunk-KHXEES2M.js.map
@@ -12,7 +12,7 @@ import {
12
12
  ok,
13
13
  redactUrls,
14
14
  warn
15
- } from "./chunk-KNXHLVF7.js";
15
+ } from "./chunk-SQ52YBKK.js";
16
16
 
17
17
  // src/db.ts
18
18
  import pg from "pg";
@@ -60,18 +60,83 @@ function poolFor(dbUrl) {
60
60
  pools.set(dbUrl, pool);
61
61
  return pool;
62
62
  }
63
+ var snapshots = /* @__PURE__ */ new Map();
64
+ var readAlone = false;
65
+ function setSnapshotsOff(off) {
66
+ readAlone = off;
67
+ }
68
+ function snapshotsOff() {
69
+ return readAlone;
70
+ }
71
+ var joined = /* @__PURE__ */ new WeakMap();
72
+ async function holdSnapshot(dbUrl) {
73
+ if (snapshots.has(dbUrl)) return true;
74
+ const anchor = newPgClient(dbUrl);
75
+ try {
76
+ await anchor.connect();
77
+ await anchor.query("BEGIN ISOLATION LEVEL REPEATABLE READ, READ ONLY");
78
+ const { rows: [row] } = await anchor.query("SELECT pg_export_snapshot() AS id");
79
+ snapshots.set(dbUrl, { id: row.id, anchor });
80
+ return true;
81
+ } catch {
82
+ await anchor.end().catch(() => void 0);
83
+ return false;
84
+ }
85
+ }
86
+ function heldSnapshot(dbUrl) {
87
+ return snapshots.get(dbUrl)?.id;
88
+ }
89
+ async function releaseSnapshots() {
90
+ const held = [...snapshots.entries()];
91
+ snapshots.clear();
92
+ await Promise.all(held.map(async ([dbUrl, { anchor }]) => {
93
+ const pool = pools.get(dbUrl);
94
+ pools.delete(dbUrl);
95
+ await pool?.end().catch(() => void 0);
96
+ await anchor.end().catch(() => void 0);
97
+ }));
98
+ }
99
+ async function connectionFor(dbUrl) {
100
+ const client = await poolFor(dbUrl).connect();
101
+ const id = snapshots.get(dbUrl)?.id;
102
+ if (id && joined.get(client) !== id) {
103
+ try {
104
+ await client.query(`BEGIN ISOLATION LEVEL REPEATABLE READ; SET TRANSACTION SNAPSHOT '${id.replace(/'/g, "''")}'`);
105
+ joined.set(client, id);
106
+ } catch (err) {
107
+ await client.query("ROLLBACK").catch(() => void 0);
108
+ client.release();
109
+ throw err;
110
+ }
111
+ }
112
+ return client;
113
+ }
63
114
  var pgQuery = async (dbUrl, sql, params) => {
64
- const { rows } = await poolFor(dbUrl).query(sql, params);
65
- return rows;
115
+ if (!snapshots.has(dbUrl)) {
116
+ const { rows } = await poolFor(dbUrl).query(sql, params);
117
+ return rows;
118
+ }
119
+ const client = await connectionFor(dbUrl);
120
+ try {
121
+ const { rows } = await client.query(sql, params);
122
+ return rows;
123
+ } catch (err) {
124
+ await client.query("ROLLBACK").catch(() => void 0);
125
+ joined.delete(client);
126
+ throw err;
127
+ } finally {
128
+ client.release();
129
+ }
66
130
  };
67
131
  async function inRolledBackTransaction(dbUrl, fn) {
68
- const client = await poolFor(dbUrl).connect();
132
+ const client = await connectionFor(dbUrl);
133
+ const [begin, undo] = joined.has(client) ? ["SAVEPOINT sf_scratch", "ROLLBACK TO SAVEPOINT sf_scratch"] : ["BEGIN", "ROLLBACK"];
69
134
  try {
70
- await client.query("BEGIN");
135
+ await client.query(begin);
71
136
  try {
72
137
  return await fn(client);
73
138
  } finally {
74
- await client.query("ROLLBACK").catch(() => void 0);
139
+ await client.query(undo).catch(() => void 0);
75
140
  }
76
141
  } finally {
77
142
  client.release();
@@ -535,6 +600,11 @@ export {
535
600
  pgClientConfig,
536
601
  newPgClient,
537
602
  newPgPool,
603
+ setSnapshotsOff,
604
+ snapshotsOff,
605
+ holdSnapshot,
606
+ heldSnapshot,
607
+ releaseSnapshots,
538
608
  pgQuery,
539
609
  inRolledBackTransaction,
540
610
  DEFAULT_LOCAL_PORT,
@@ -555,4 +625,4 @@ export {
555
625
  MigrationsCheck,
556
626
  readLocalMigrations
557
627
  };
558
- //# sourceMappingURL=chunk-MG3SFV5J.js.map
628
+ //# sourceMappingURL=chunk-KT7NMELZ.js.map
@@ -1,23 +1,27 @@
1
1
  import {
2
2
  sanitizeForReport
3
- } from "./chunk-HNJ6BNNZ.js";
3
+ } from "./chunk-Q43MN6NE.js";
4
4
  import {
5
5
  coverage,
6
6
  formatSkips,
7
7
  renderDetailed,
8
8
  renderSummary
9
- } from "./chunk-7DL63NWP.js";
9
+ } from "./chunk-MRXIE55Y.js";
10
10
  import {
11
11
  createDefaultRegistry,
12
12
  scan,
13
13
  summarizeByKind
14
- } from "./chunk-OB5GP7UB.js";
14
+ } from "./chunk-CAQ3QQFP.js";
15
15
  import {
16
16
  comparedSchemas,
17
+ copyStructure,
18
+ dropDatabase,
17
19
  proofScope,
18
20
  proveConvergence,
19
- residualHeldBack
20
- } from "./chunk-3WKWPSFN.js";
21
+ psqlBeside,
22
+ residualHeldBack,
23
+ withDatabase
24
+ } from "./chunk-GGZ7YWBZ.js";
21
25
  import {
22
26
  CHECK_META,
23
27
  CHECK_NAMES,
@@ -26,35 +30,47 @@ import {
26
30
  planWork,
27
31
  promote,
28
32
  resolveTableFilter
29
- } from "./chunk-DQJSSTIJ.js";
33
+ } from "./chunk-YUJ2MHLM.js";
34
+ import {
35
+ isCommentOnly,
36
+ splitSqlStatements
37
+ } from "./chunk-WK5YTINL.js";
30
38
  import {
31
39
  renderTip
32
- } from "./chunk-ZI435CAU.js";
40
+ } from "./chunk-RQCXGO37.js";
33
41
  import {
34
42
  BaseCommand
35
- } from "./chunk-5STH2A6K.js";
43
+ } from "./chunk-OXS42LJH.js";
36
44
  import {
37
45
  isCloneDatabase,
38
46
  sourceLooksLikeCloneOf
39
- } from "./chunk-EFKQOQGG.js";
47
+ } from "./chunk-FD2JKEJA.js";
40
48
  import {
41
- parseFlagList
42
- } from "./chunk-DQQ5E563.js";
49
+ describeMissingPgDump,
50
+ getServerMajorVersion,
51
+ parseFlagList,
52
+ resolvePgDumpPath
53
+ } from "./chunk-Z2YUOA4W.js";
43
54
  import {
44
- Preflight
45
- } from "./chunk-MG3SFV5J.js";
55
+ Preflight,
56
+ pgQuery,
57
+ quoteName
58
+ } from "./chunk-KT7NMELZ.js";
46
59
  import {
47
60
  CLONE_SKIP_FLAGS,
48
61
  DEFAULT_IGNORE_SCHEMAS,
62
+ DiagnosticError,
63
+ SUPABASE_PLATFORM_SCHEMAS,
49
64
  bold,
50
65
  cmd,
51
66
  describeFailure,
52
67
  dim,
68
+ errMsg,
53
69
  ok,
54
70
  redactUrls,
55
71
  schemaDiffIgnores,
56
72
  warn
57
- } from "./chunk-KNXHLVF7.js";
73
+ } from "./chunk-SQ52YBKK.js";
58
74
 
59
75
  // src/commands/diff.ts
60
76
  import { Flags } from "@oclif/core";
@@ -99,6 +115,7 @@ function formatCiSummary(result) {
99
115
  const warningIssues = [];
100
116
  const errors = [];
101
117
  const skipped = [];
118
+ const notices = [];
102
119
  for (const check of result.checks ?? []) {
103
120
  if (check?.status === "error") {
104
121
  errors.push({ check: check.check, message: check.error ?? "Check failed with no error message" });
@@ -106,6 +123,7 @@ function formatCiSummary(result) {
106
123
  if (check?.status === "skipped") {
107
124
  skipped.push({ check: check.check, reason: check.skipReason ?? "no reason given" });
108
125
  }
126
+ for (const notice of check?.notices ?? []) notices.push({ check: check.check, notice });
109
127
  for (const issue of check?.issues ?? []) {
110
128
  const entry = { check: check.check, id: issue.id, title: issue.title };
111
129
  if (issue.severity === "critical") {
@@ -123,12 +141,93 @@ function formatCiSummary(result) {
123
141
  errors,
124
142
  skipped,
125
143
  coverage: coverage(result),
144
+ notices,
126
145
  criticalIssues,
127
146
  warningIssues
128
147
  };
129
148
  }
130
149
 
150
+ // src/schema-dir.ts
151
+ import { randomBytes } from "crypto";
152
+ import { readdir, readFile } from "fs/promises";
153
+ import { join, relative } from "path";
154
+ async function openSchemaDir(dir, targetUrl) {
155
+ const files = await sqlFiles(dir);
156
+ if (files.length === 0) throw new DiagnosticError(`--source-dir: no .sql files in ${dir}`);
157
+ const serverMajor = await getServerMajorVersion(targetUrl);
158
+ const resolved = await resolvePgDumpPath(serverMajor);
159
+ if (!resolved) throw new DiagnosticError(`--source-dir needs pg_dump to copy the target's platform schemas: ${await describeMissingPgDump(serverMajor)}`);
160
+ const tools = { pgDump: resolved.path, psql: psqlBeside(resolved.path), clientMajor: resolved.major, serverMajor };
161
+ const name = `supaforge_schema_${randomBytes(4).toString("hex")}`;
162
+ const adminUrl = withDatabase(targetUrl, "postgres");
163
+ const url = withDatabase(targetUrl, name);
164
+ try {
165
+ await pgQuery(adminUrl, `CREATE DATABASE ${quoteName(name)}`);
166
+ } catch (err) {
167
+ throw new DiagnosticError(`--source-dir: could not create a throwaway database on the target's server: ${errMsg(err)}`);
168
+ }
169
+ const close = () => dropDatabase(adminUrl, name);
170
+ try {
171
+ const present = new Set((await pgQuery(targetUrl, "SELECT nspname FROM pg_namespace")).map((r) => r.nspname));
172
+ const platform = SUPABASE_PLATFORM_SCHEMAS.filter((s) => present.has(s) && s !== "pg_catalog" && s !== "information_schema");
173
+ if (platform.length > 0) await copyStructure(targetUrl, url, platform, tools);
174
+ const statements = await loadStatements(url, files);
175
+ return { url, statements, files: files.length, close, unavailable: await unavailableExtensions(targetUrl, url) };
176
+ } catch (err) {
177
+ await close();
178
+ throw err;
179
+ }
180
+ }
181
+ async function sqlFiles(dir) {
182
+ const entries = await readdir(dir, { recursive: true, withFileTypes: true });
183
+ return entries.filter((e) => e.isFile() && e.name.endsWith(".sql")).map((e) => {
184
+ const path = join(e.parentPath ?? e.path, e.name);
185
+ return { path, name: relative(dir, path) };
186
+ }).sort((a, b) => a.name.localeCompare(b.name));
187
+ }
188
+ async function loadStatements(url, files) {
189
+ let pending = [];
190
+ for (const f of files) {
191
+ for (const sql of splitSqlStatements(await readFile(f.path, "utf8"))) {
192
+ if (!isCommentOnly(sql)) pending.push({ file: f.name, sql });
193
+ }
194
+ }
195
+ const total = pending.length;
196
+ for (; ; ) {
197
+ const failed = [];
198
+ for (const p of pending) {
199
+ try {
200
+ await pgQuery(url, p.sql);
201
+ } catch (err) {
202
+ failed.push({ ...p, error: errMsg(err) });
203
+ }
204
+ }
205
+ if (failed.length === 0) return total;
206
+ if (failed.length === pending.length) {
207
+ const first = failed[0];
208
+ throw new DiagnosticError(
209
+ `--source-dir: ${failed.length} statement(s) could not be loaded. First, in ${first.file}: ${first.error}
210
+ ${first.sql.slice(0, 200).replace(/\s+/g, " ")}`
211
+ );
212
+ }
213
+ pending = failed;
214
+ }
215
+ }
216
+ async function unavailableExtensions(targetUrl, copyUrl) {
217
+ const sql = `SELECT e.extname AS name,
218
+ ARRAY(SELECT DISTINCT n.nspname FROM pg_depend d JOIN pg_class c ON c.oid = d.objid
219
+ JOIN pg_namespace n ON n.oid = c.relnamespace
220
+ WHERE d.classid = 'pg_class'::regclass AND d.refobjid = e.oid AND d.deptype = 'e')::text[] AS schemas
221
+ FROM pg_extension e`;
222
+ const [target, copy] = await Promise.all([targetUrl, copyUrl].map((url) => pgQuery(url, sql)));
223
+ const have = new Set(copy.map((e) => e.name));
224
+ const missing = target.filter((e) => !have.has(e.name));
225
+ return { extensions: missing.map((e) => e.name), schemas: [...new Set(missing.flatMap((e) => e.schemas))] };
226
+ }
227
+
131
228
  // src/commands/diff.ts
229
+ var SCHEMA_FILES_ENV = "schema files";
230
+ var SCHEMA_FILE_CHECKS = ["schema", "rls", "extensions"];
132
231
  function describeCheckOutcome(status, issueCount, skipReason) {
133
232
  if (status === "error") return { glyph: warn("\u2717"), text: warn("error") };
134
233
  if (status === "skipped") {
@@ -193,6 +292,10 @@ var Diff = class _Diff extends BaseCommand {
193
292
  { name: "SUPAFORGE_DBDIFF_MEMORY", description: "PHP memory limit for @dbdiff/cli \u2014 512M, 2G, or -1 for unlimited (default dbdiff's own 1G)." }
194
293
  ];
195
294
  static flags = {
295
+ "source-dir": Flags.string({
296
+ description: "Compare a directory of schema files (such as supabase/schemas) with the target, instead of a source database. They are loaded into a throwaway database on the target's server.",
297
+ exclusive: ["source"]
298
+ }),
196
299
  check: Flags.string({
197
300
  char: "l",
198
301
  description: "Limit to a specific check",
@@ -220,6 +323,10 @@ var Diff = class _Diff extends BaseCommand {
220
323
  description: "With --apply, print the fixes in the order they would run without executing any of them",
221
324
  default: false
222
325
  }),
326
+ "no-snapshot": Flags.boolean({
327
+ description: "Read each query on its own, rather than each database as of one instant (which holds what it reads locked until the scan ends, as pg_dump does)",
328
+ default: false
329
+ }),
223
330
  "no-transaction": Flags.boolean({
224
331
  description: "With --apply, run each fix on its own instead of rolling the whole set back on the first failure",
225
332
  aliases: ["continue-on-error"],
@@ -276,12 +383,14 @@ var Diff = class _Diff extends BaseCommand {
276
383
  */
277
384
  renderApplyResult(result, dryRun) {
278
385
  const verb = dryRun ? "Would apply" : "Applied";
386
+ for (const warning of result.warnings ?? []) this.log(`${warn(warning)}
387
+ `);
279
388
  if (result.applied.length > 0) {
280
389
  const heading = `${verb} ${result.applied.length} fix(es)${dryRun ? ", in this order" : ""}:`;
281
390
  this.log(dryRun ? bold(heading) : ok(heading));
282
391
  result.applied.forEach((stmt, i) => {
283
392
  const glyph = dryRun ? dim(`${i + 1}.`) : ok("\u2713");
284
- this.log(` ${glyph} ${dim(`[${stmt.check}]`)} ${stmt.issueId}`);
393
+ this.log(` ${glyph} ${dim(`[${stmt.check}]`)} ${stmt.issueId}${stmt.note ? dim(` \u2014 ${stmt.note}`) : ""}`);
285
394
  if (dryRun && stmt.sql) this.log(` ${dim(stmt.sql.replace(/\s*\n\s*/g, " "))}`);
286
395
  if (dryRun && stmt.action) this.log(` ${dim(stmt.action)}`);
287
396
  });
@@ -322,9 +431,30 @@ ${warn(`${result.errors.length} error(s):`)}`);
322
431
  `);
323
432
  }
324
433
  const config = await this.loadConfigOrFail();
434
+ if (flags["source-dir"]) {
435
+ const { envName: targetName } = this.resolveEnv(config, flags.target ?? config.target);
436
+ const loaded = await openSchemaDir(flags["source-dir"], config.environments[targetName].dbUrl);
437
+ this.log(` ${dim(`Loaded ${loaded.statements} statement(s) from ${loaded.files} file(s) in ${flags["source-dir"]}.`)}
438
+ `);
439
+ config.environments = { ...config.environments, [SCHEMA_FILES_ENV]: { dbUrl: loaded.url } };
440
+ config.ignoreSchemas = [...config.ignoreSchemas ?? DEFAULT_IGNORE_SCHEMAS, ...loaded.unavailable.schemas];
441
+ config.ignoreExtensions = [...config.ignoreExtensions ?? [], ...loaded.unavailable.extensions];
442
+ config.source = SCHEMA_FILES_ENV;
443
+ config.target = targetName;
444
+ try {
445
+ await this.compare(flags, config, flags.check ? void 0 : SCHEMA_FILE_CHECKS);
446
+ } finally {
447
+ await loaded.close();
448
+ }
449
+ return;
450
+ }
325
451
  this.validateDualEnvConfig(config, flags.source, flags.target);
452
+ await this.compare(flags, config);
453
+ }
454
+ /** The comparison itself, between the config's source and target. */
455
+ async compare(flags, config, defaultChecks) {
326
456
  const registry = createDefaultRegistry({ includeFiles: flags["include-files"] });
327
- const checks = flags.check ? [flags.check] : void 0;
457
+ const checks = flags.check ? [flags.check] : defaultChecks;
328
458
  const skip = flags.skip?.length ? flags.skip : void 0;
329
459
  const tableFilter = resolveTableFilter(config, {
330
460
  tables: flags.tables,
@@ -362,7 +492,7 @@ ${warn(`${result.errors.length} error(s):`)}`);
362
492
  };
363
493
  if (flags.apply || flags["dry-run"]) {
364
494
  const onProgress2 = makeProgress();
365
- const scanResult = await scan(registry, { config, checks, skip, tableFilter, onProgress: onProgress2, onDetail: makeDetail() });
495
+ const scanResult = await scan(registry, { config, checks, skip, tableFilter, onProgress: onProgress2, onDetail: makeDetail(), snapshot: !flags["no-snapshot"] });
366
496
  this.setCheckSummaries(scanResult.checks.map((c) => ({
367
497
  check: c.check,
368
498
  status: c.status,
@@ -448,9 +578,19 @@ ${warn(`${result.errors.length} error(s):`)}`);
448
578
  `);
449
579
  }
450
580
  }
581
+ if (proof.data && !proof.skipped) {
582
+ const { sampled, unchecked: unchecked2, rewritten } = proof.data;
583
+ if (sampled.length > 0) this.log(` ${ok("Rows kept")} in the ${sampled.length} table(s) sampled.`);
584
+ if (rewritten.length > 0) {
585
+ this.log(` ${warn("Rewritten")} on the copy \u2014 a long lock on the real target: ${rewritten.join(", ")}`);
586
+ }
587
+ if (unchecked2.length > 0) this.log(` ${dim(`Rows not checked (the sample would not load): ${unchecked2.join(", ")}`)}`);
588
+ this.log("");
589
+ }
451
590
  }
452
591
  const result2 = await promote({
453
592
  dbUrl: targetEnv.dbUrl,
593
+ sourceUrl: config.environments[config.source]?.dbUrl,
454
594
  scanResult,
455
595
  checks,
456
596
  dryRun,
@@ -479,7 +619,7 @@ ${warn(`${result.errors.length} error(s):`)}`);
479
619
  return;
480
620
  }
481
621
  const onProgress = makeProgress();
482
- const result = await scan(registry, { config, checks, skip, tableFilter, onProgress, onDetail: makeDetail() });
622
+ const result = await scan(registry, { config, checks, skip, tableFilter, onProgress, onDetail: makeDetail(), snapshot: !flags["no-snapshot"] });
483
623
  this.setCheckSummaries(result.checks.map((c) => ({
484
624
  check: c.check,
485
625
  status: c.status,
@@ -637,4 +777,4 @@ export {
637
777
  inertApplyFlags,
638
778
  Diff
639
779
  };
640
- //# sourceMappingURL=chunk-PYAGSMFB.js.map
780
+ //# sourceMappingURL=chunk-M2564IGT.js.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  CHECK_META,
3
3
  isComparisonCheck
4
- } from "./chunk-DQJSSTIJ.js";
4
+ } from "./chunk-YUJ2MHLM.js";
5
5
  import {
6
6
  CHECK_LINE_PADDING,
7
7
  bold,
@@ -9,7 +9,7 @@ import {
9
9
  dim,
10
10
  ok,
11
11
  warn
12
- } from "./chunk-KNXHLVF7.js";
12
+ } from "./chunk-SQ52YBKK.js";
13
13
 
14
14
  // src/render.ts
15
15
  function countErrored(result) {
@@ -78,6 +78,7 @@ function renderSummary(result) {
78
78
  lines.push("");
79
79
  for (const lr of result.checks) {
80
80
  lines.push(formatCheckLine(lr));
81
+ for (const notice of lr.notices ?? []) lines.push(` ${dim(`\u24D8 ${notice}`)}`);
81
82
  }
82
83
  lines.push("");
83
84
  const { compared, total } = coverage(result);
@@ -153,6 +154,9 @@ function formatIssue(issue) {
153
154
  const lines = [];
154
155
  lines.push(` ${severityIcon(issue.severity)} ${colorSeverity(issue.severity)} ${bold(issue.title)}`);
155
156
  lines.push(` ${dim(issue.description)}`);
157
+ for (const hazard of issue.hazards ?? []) {
158
+ lines.push(` ${warn(hazard.kind === "data-loss" ? "\u26D4" : "\u23F3")} ${warn(hazard.detail)}`);
159
+ }
156
160
  if (issue.sql) {
157
161
  lines.push("");
158
162
  lines.push(` ${dim("SQL fix (UP):")}`);
@@ -237,4 +241,4 @@ export {
237
241
  renderDetailed,
238
242
  formatSkips
239
243
  };
240
- //# sourceMappingURL=chunk-7DL63NWP.js.map
244
+ //# sourceMappingURL=chunk-MRXIE55Y.js.map