@akal/supaforge 0.0.33 → 0.0.35

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 (55) hide show
  1. package/README.md +67 -14
  2. package/dist/base-command.d.ts +1 -1
  3. package/dist/base-command.js +4 -3
  4. package/dist/{branch-C4ndfoj5.d.ts → branch-BppjQBv_.d.ts} +1 -1
  5. package/dist/{chunk-4IVN7QVY.js → chunk-44UVQJV2.js} +159 -18
  6. package/dist/{chunk-3HGAV5CC.js → chunk-4AGJRRBT.js} +390 -43
  7. package/dist/{chunk-SQPCWGQO.js → chunk-4M6OQLPO.js} +22 -121
  8. package/dist/{chunk-2SELREHW.js → chunk-5Q6LSLA3.js} +56 -9
  9. package/dist/{chunk-7NAZGZPV.js → chunk-7N63OPFN.js} +28 -21
  10. package/dist/chunk-AGONS5JJ.js +242 -0
  11. package/dist/chunk-GQPGH6YR.js +50 -0
  12. package/dist/{chunk-GEHPTPNZ.js → chunk-H2D3YSKM.js} +7 -3
  13. package/dist/{chunk-6Q7MHFBM.js → chunk-HNJ6BNNZ.js} +2 -2
  14. package/dist/{chunk-6WAMNZLO.js → chunk-KNXHLVF7.js} +126 -4
  15. package/dist/{chunk-DMIWAJRB.js → chunk-NEM5AEPQ.js} +172 -22
  16. package/dist/{chunk-6LJERPUN.js → chunk-NX2KKUS7.js} +20 -12
  17. package/dist/{chunk-SC2XHGIS.js → chunk-O5ERC2SE.js} +3 -3
  18. package/dist/{chunk-MIRLB5JM.js → chunk-SM7HP6EP.js} +64 -182
  19. package/dist/{chunk-BUOWHE36.js → chunk-T7GMPGUO.js} +613 -66
  20. package/dist/{chunk-G2DIPIYU.js → chunk-WK5YTINL.js} +29 -2
  21. package/dist/{chunk-UWAVXC57.js → chunk-WNTCZM2J.js} +34 -482
  22. package/dist/{chunk-GZREXCQZ.js → chunk-XLZCX6R2.js} +17 -10
  23. package/dist/{chunk-ORQJVE57.js → chunk-YWERIWFD.js} +18 -17
  24. package/dist/{chunk-NTUASFQ3.js → chunk-YZM76W22.js} +5 -5
  25. package/dist/{chunk-BLKJWDTL.js → chunk-ZI435CAU.js} +2 -2
  26. package/dist/commands/clone.d.ts +2 -2
  27. package/dist/commands/clone.js +14 -13
  28. package/dist/commands/diff.d.ts +37 -23
  29. package/dist/commands/diff.js +20 -17
  30. package/dist/commands/hukam.d.ts +2 -3
  31. package/dist/commands/hukam.js +16 -15
  32. package/dist/commands/init.d.ts +1 -1
  33. package/dist/commands/init.js +5 -1
  34. package/dist/commands/mcp.js +16 -10
  35. package/dist/commands/migrate/baseline.d.ts +1 -1
  36. package/dist/commands/migrate/baseline.js +5 -4
  37. package/dist/commands/migrate/create.d.ts +1 -1
  38. package/dist/commands/migrate/create.js +15 -9
  39. package/dist/commands/migrate/list.d.ts +1 -1
  40. package/dist/commands/migrate/list.js +6 -5
  41. package/dist/commands/migrate/run.d.ts +4 -1
  42. package/dist/commands/migrate/run.js +10 -8
  43. package/dist/commands/report.d.ts +1 -1
  44. package/dist/commands/report.js +5 -4
  45. package/dist/commands/restore.d.ts +1 -1
  46. package/dist/commands/restore.js +14 -13
  47. package/dist/commands/snapshot.d.ts +8 -1
  48. package/dist/commands/snapshot.js +37 -9
  49. package/dist/commands/sync.d.ts +2 -3
  50. package/dist/commands/sync.js +16 -15
  51. package/dist/{config-CTbeRlJk.d.ts → config-l3ggjw9r.d.ts} +2 -0
  52. package/dist/index.d.ts +21 -4
  53. package/dist/index.js +15 -15
  54. package/package.json +2 -2
  55. package/dist/chunk-DQQ5E563.js +0 -64
@@ -0,0 +1,50 @@
1
+ import {
2
+ describeFailure,
3
+ redactUrls
4
+ } from "./chunk-KNXHLVF7.js";
5
+
6
+ // src/process-guard.ts
7
+ var installed = false;
8
+ var jsonWritten = false;
9
+ var keepRunning = false;
10
+ function noteJsonWritten() {
11
+ jsonWritten = true;
12
+ }
13
+ function keepRunningOnStrayErrors() {
14
+ keepRunning = true;
15
+ }
16
+ function installProcessGuard(argv = process.argv) {
17
+ if (installed) return;
18
+ installed = true;
19
+ const onStray = (err) => {
20
+ const message = redactUrls(describeFailure(err));
21
+ if (keepRunning) {
22
+ process.stderr.write(`SupaForge: an error no handler caught, carrying on: ${message}
23
+ `);
24
+ return;
25
+ }
26
+ if (argv.includes("--json") && !jsonWritten) {
27
+ jsonWritten = true;
28
+ process.stdout.write(JSON.stringify({ error: message }, null, 2) + "\n");
29
+ }
30
+ process.stderr.write(
31
+ `
32
+ Unexpected error: ${message}
33
+ If this looks like a bug, please report it:
34
+ https://github.com/akalsoftware/supaforge/issues/new
35
+
36
+ `
37
+ );
38
+ process.exitCode = 1;
39
+ process.stdout.write("", () => process.exit(1));
40
+ };
41
+ process.on("uncaughtException", onStray);
42
+ process.on("unhandledRejection", onStray);
43
+ }
44
+
45
+ export {
46
+ noteJsonWritten,
47
+ keepRunningOnStrayErrors,
48
+ installProcessGuard
49
+ };
50
+ //# sourceMappingURL=chunk-GQPGH6YR.js.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  CHECK_META,
3
3
  isComparisonCheck
4
- } from "./chunk-UWAVXC57.js";
4
+ } from "./chunk-T7GMPGUO.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-6WAMNZLO.js";
12
+ } from "./chunk-KNXHLVF7.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-GEHPTPNZ.js.map
244
+ //# sourceMappingURL=chunk-H2D3YSKM.js.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  redactUrls
3
- } from "./chunk-SQPCWGQO.js";
3
+ } from "./chunk-KNXHLVF7.js";
4
4
 
5
5
  // src/utils/sanitize.ts
6
6
  function sanitizeForReport(text) {
@@ -10,4 +10,4 @@ function sanitizeForReport(text) {
10
10
  export {
11
11
  sanitizeForReport
12
12
  };
13
- //# sourceMappingURL=chunk-6Q7MHFBM.js.map
13
+ //# sourceMappingURL=chunk-HNJ6BNNZ.js.map
@@ -1,3 +1,111 @@
1
+ // src/utils/error.ts
2
+ var DB_URL_RE = /\b((?:postgres(?:ql)?|mysql|mariadb):\/\/)(\S+)/g;
3
+ function redactUrls(message) {
4
+ return message.replace(DB_URL_RE, (_, scheme, rest) => {
5
+ const lastAt = rest.lastIndexOf("@");
6
+ if (lastAt === -1) return scheme + rest;
7
+ const creds = rest.slice(0, lastAt);
8
+ const hostPart = rest.slice(lastAt);
9
+ const colonIdx = creds.indexOf(":");
10
+ if (colonIdx === -1) return scheme + rest;
11
+ return scheme + creds.slice(0, colonIdx) + ":***" + hostPart;
12
+ });
13
+ }
14
+ var UNDESCRIBED_ERROR = "unknown error \u2014 no message reported";
15
+ function describeFailure(err) {
16
+ if (typeof err === "string") return err.trim() || UNDESCRIBED_ERROR;
17
+ const candidate = err;
18
+ const message = typeof candidate?.message === "string" ? candidate.message.trim() : "";
19
+ if (message) return message;
20
+ const code = typeof candidate?.code === "string" ? candidate.code.trim() : "";
21
+ if (code) return `${code} (no further detail reported)`;
22
+ const name = typeof candidate?.name === "string" ? candidate.name.trim() : "";
23
+ if (name && name !== "Error") return `${name} (no further detail reported)`;
24
+ if (err !== null && err !== void 0 && !(err instanceof Error)) {
25
+ const rendered = String(err).trim();
26
+ if (rendered && rendered !== "[object Object]") return rendered;
27
+ }
28
+ return UNDESCRIBED_ERROR;
29
+ }
30
+ function errMsg(err) {
31
+ return redactUrls(describeFailure(err));
32
+ }
33
+ var DiagnosticError = class extends Error {
34
+ constructor(message) {
35
+ super(message);
36
+ this.name = "DiagnosticError";
37
+ }
38
+ };
39
+ function isDiagnosticError(err) {
40
+ return err instanceof DiagnosticError || err instanceof Error && err.name === "DiagnosticError";
41
+ }
42
+ var CONNECTION_ERROR_PATTERNS = [
43
+ {
44
+ test: (m) => /ECONNREFUSED|Connection refused/i.test(m),
45
+ build: (host) => `Cannot connect to PostgreSQL at ${host} \u2014 the server does not appear to be running. Start it and try again.`
46
+ },
47
+ {
48
+ test: (m) => /ENOTFOUND|getaddrinfo/i.test(m),
49
+ build: (host) => `Cannot resolve hostname ${host} \u2014 check the database URL in your config.`
50
+ },
51
+ {
52
+ // Before the connection timeout: the server answered, and a query ran
53
+ // past its statement_timeout. Calling that a connection problem sent
54
+ // people to check a host and port that were fine.
55
+ test: (m) => /canceling statement due to statement timeout|57014/i.test(m),
56
+ build: (host) => `A query on ${host} was cancelled by statement_timeout \u2014 the server is reachable. Raise statement_timeout for this role or connection and try again.`
57
+ },
58
+ {
59
+ // Deliberately narrow. A bare /timeout/i also matches our own remediation
60
+ // text — "set SUPAFORGE_DBDIFF_TIMEOUT=600" contains "TIMEOUT" — which is
61
+ // how an accurate "Schema diff timed out after Ns" message was being
62
+ // rewritten into a wrong "check the host and port" one (issue #29).
63
+ // DiagnosticError below is the primary guard; this is defence in depth.
64
+ test: (m) => /ETIMEDOUT|connection timed out|timeout expired/i.test(m),
65
+ build: (host) => `Connection to ${host} timed out \u2014 verify the host and port are correct and that the server is reachable.`
66
+ },
67
+ {
68
+ test: (m) => /password authentication failed/i.test(m),
69
+ build: (host) => `Authentication failed for ${host} \u2014 check the username and password in your database URL.`
70
+ },
71
+ {
72
+ // Must stay anchored to "database ... does not exist" (Postgres SQLSTATE
73
+ // 3D000). A bare /does not exist/ is the same trap as the timeout pattern
74
+ // above: it matched dbdiff's own CLI complaint — The "--allow-destructive"
75
+ // option does not exist — and rewrote it into "Database does not exist on
76
+ // 127.0.0.1:5432", sending you off to check a database name that was fine
77
+ // while the real cause (an outdated @dbdiff/cli) went unmentioned.
78
+ test: (m) => /database\s+"[^"]*"\s+does not exist|3D000/i.test(m),
79
+ build: (host) => `Database does not exist on ${host} \u2014 verify the database name in your config.`
80
+ },
81
+ {
82
+ test: (m) => /SSL.*required|no pg_hba.conf entry/i.test(m),
83
+ build: (host) => `Connection rejected by ${host} \u2014 the server requires SSL or your IP is not allowed. Check pg_hba.conf and SSL settings.`
84
+ }
85
+ ];
86
+ function extractHost(dbUrl) {
87
+ try {
88
+ const url = new URL(dbUrl);
89
+ return url.port ? `${url.hostname}:${url.port}` : url.hostname;
90
+ } catch {
91
+ return "the configured host";
92
+ }
93
+ }
94
+ function friendlyDbError(err, dbUrl) {
95
+ const raw = errMsg(err);
96
+ if (isDiagnosticError(err)) return raw;
97
+ const host = dbUrl ? extractHost(dbUrl) : "the configured host";
98
+ for (const pattern of CONNECTION_ERROR_PATTERNS) {
99
+ try {
100
+ if (pattern.test(raw)) {
101
+ return pattern.build(host);
102
+ }
103
+ } catch {
104
+ }
105
+ }
106
+ return raw;
107
+ }
108
+
1
109
  // src/defaults.ts
2
110
  var DEFAULT_IGNORE_SCHEMAS = [
3
111
  "auth",
@@ -164,13 +272,22 @@ async function loadConfig(cwd = process.cwd()) {
164
272
  await loadEnvFiles(cwd);
165
273
  const configPath = resolve2(cwd, CONFIG_FILENAME);
166
274
  const raw = await readFile2(configPath, "utf-8");
167
- const config = JSON.parse(raw);
168
- return resolveConfig(config);
275
+ let parsed;
276
+ try {
277
+ parsed = JSON.parse(raw);
278
+ } catch (err) {
279
+ throw new Error(`${CONFIG_FILENAME} is not valid JSON: ${errMsg(err)}`);
280
+ }
281
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
282
+ throw new Error(`${CONFIG_FILENAME} must hold a JSON object, with an "environments" object inside`);
283
+ }
284
+ return resolveConfig(parsed);
169
285
  }
170
286
  function resolveConfig(config) {
171
287
  return {
172
288
  ...config,
173
- environments: expandEnvironments(config.environments),
289
+ // Missing, it is validateConfig's to report, not a crash here.
290
+ environments: expandEnvironments(config.environments ?? {}),
174
291
  ignoreSchemas: config.ignoreSchemas ?? DEFAULT_IGNORE_SCHEMAS
175
292
  };
176
293
  }
@@ -285,6 +402,11 @@ function printBanner(log) {
285
402
  }
286
403
 
287
404
  export {
405
+ redactUrls,
406
+ describeFailure,
407
+ errMsg,
408
+ DiagnosticError,
409
+ friendlyDbError,
288
410
  DEFAULT_IGNORE_SCHEMAS,
289
411
  SUPABASE_PLATFORM_SCHEMAS,
290
412
  schemaDiffIgnores,
@@ -333,4 +455,4 @@ export {
333
455
  printHints,
334
456
  printBanner
335
457
  };
336
- //# sourceMappingURL=chunk-6WAMNZLO.js.map
458
+ //# sourceMappingURL=chunk-KNXHLVF7.js.map
@@ -1,30 +1,30 @@
1
1
  import {
2
- runLikeApply
3
- } from "./chunk-UWAVXC57.js";
4
- import {
2
+ parseStatements,
3
+ relationName,
5
4
  rolesNamedBy,
5
+ runLikeApply,
6
6
  sqlSkeleton
7
- } from "./chunk-BUOWHE36.js";
7
+ } from "./chunk-T7GMPGUO.js";
8
8
  import {
9
9
  isCommentOnly,
10
10
  splitSqlStatements
11
- } from "./chunk-G2DIPIYU.js";
11
+ } from "./chunk-WK5YTINL.js";
12
12
  import {
13
13
  describeMissingPgDump,
14
14
  getServerMajorVersion,
15
+ globToRegExp,
15
16
  resolvePgDumpPath
16
- } from "./chunk-MIRLB5JM.js";
17
- import {
18
- globToRegExp
19
- } from "./chunk-DQQ5E563.js";
17
+ } from "./chunk-AGONS5JJ.js";
20
18
  import {
21
19
  pgQuery,
20
+ quoteIdent,
21
+ quoteLiteral,
22
22
  quoteName
23
- } from "./chunk-SQPCWGQO.js";
23
+ } from "./chunk-4M6OQLPO.js";
24
24
 
25
25
  // src/prove.ts
26
26
  import { randomBytes } from "crypto";
27
- import { execFile, spawn } from "child_process";
27
+ import { execFile, spawn as spawn2 } from "child_process";
28
28
  import { promisify } from "util";
29
29
  import { fingerprintSql, stateSql } from "@akal/pg-conformance";
30
30
 
@@ -41,15 +41,15 @@ function compareField(out, subject, label, before, after) {
41
41
  const a = show(before), b = show(after);
42
42
  if (a !== b) out.push(`${subject}: ${label} ${a} \u2192 ${b}`);
43
43
  }
44
- function compareKeyed(out, kind, before, after, keyOf, compare) {
44
+ function compareKeyed(out, kind, before, after, keyOf, compare, note = () => "") {
45
45
  const a = new Map((before ?? []).map((x) => [keyOf(x), x]));
46
46
  const b = new Map((after ?? []).map((x) => [keyOf(x), x]));
47
47
  for (const [key, item] of a) {
48
48
  if (!b.has(key)) out.push(`${kind} ${key}: missing`);
49
49
  else compare(out, `${kind} ${key}`, item, b.get(key));
50
50
  }
51
- for (const key of b.keys()) {
52
- if (!a.has(key)) out.push(`${kind} ${key}: unexpected`);
51
+ for (const [key, item] of b) {
52
+ if (!a.has(key)) out.push(`${kind} ${key}: unexpected${note(item)}`);
53
53
  }
54
54
  }
55
55
  function compareColumn(out, subject, a, b) {
@@ -160,7 +160,9 @@ function diffState(before, after) {
160
160
  for (const k of ["type", "start", "increment", "min", "max", "cache", "cycle", "owned_by"]) {
161
161
  compareField(o, s, k.replace("_", " "), a[k], b[k]);
162
162
  }
163
- }
163
+ },
164
+ // A serial or identity column's: it goes with its table.
165
+ (q) => q.owned_by ? ` (owned by ${q.owned_by})` : ""
164
166
  );
165
167
  compareKeyed(
166
168
  out,
@@ -202,6 +204,132 @@ function diffState(before, after) {
202
204
 
203
205
  // src/prove.ts
204
206
  import { join, dirname } from "path";
207
+
208
+ // src/prove-data.ts
209
+ import { spawn } from "child_process";
210
+ var DEFAULT_SAMPLE_ROWS = 1e3;
211
+ function sampleRows() {
212
+ const raw = process.env.SUPAFORGE_PROVE_ROWS;
213
+ const n = raw === void 0 ? DEFAULT_SAMPLE_ROWS : Number(raw);
214
+ return Number.isInteger(n) && n >= 0 ? n : DEFAULT_SAMPLE_ROWS;
215
+ }
216
+ var TABLES_SQL = `
217
+ SELECT n.nspname || '.' || c.relname AS name, quote_ident(n.nspname) || '.' || quote_ident(c.relname) AS ident,
218
+ (SELECT string_agg(quote_ident(a.attname), ', ' ORDER BY a.attnum) FROM pg_attribute a
219
+ WHERE a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped AND a.attgenerated = '') AS columns
220
+ FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
221
+ WHERE c.relkind = 'r' AND NOT c.relispartition AND n.nspname = ANY($1::text[])
222
+ ORDER BY 1`;
223
+ async function sampleInto(psql, fromUrl, intoUrl, schemas, limit) {
224
+ const tables = await pgQuery(fromUrl, TABLES_SQL, [schemas]);
225
+ const sampled = [];
226
+ const unchecked = [];
227
+ for (const t of tables) {
228
+ if (!t.columns) continue;
229
+ try {
230
+ await pipeCopy(
231
+ psql,
232
+ fromUrl,
233
+ intoUrl,
234
+ `\\copy (SELECT ${t.columns} FROM ${t.ident} LIMIT ${limit}) TO STDOUT`,
235
+ `\\copy ${t.ident} (${t.columns}) FROM STDIN`
236
+ );
237
+ sampled.push(t.name);
238
+ } catch {
239
+ unchecked.push(t.name);
240
+ }
241
+ }
242
+ return { sampled, unchecked };
243
+ }
244
+ function pipeCopy(psql, fromUrl, intoUrl, out, into) {
245
+ return new Promise((resolve, reject) => {
246
+ const reader = spawn(psql, [fromUrl, "-X", "-q", "-v", "ON_ERROR_STOP=1", "-c", out], { stdio: ["ignore", "pipe", "pipe"] });
247
+ const writer = spawn(psql, [intoUrl, "-X", "-q", "-v", "ON_ERROR_STOP=1", "-f", "-"], { stdio: ["pipe", "pipe", "pipe"] });
248
+ let stderr = "";
249
+ reader.stderr.on("data", (d) => {
250
+ stderr += d.toString();
251
+ });
252
+ writer.stderr.on("data", (d) => {
253
+ stderr += d.toString();
254
+ });
255
+ reader.on("error", reject);
256
+ writer.on("error", reject);
257
+ writer.stdin.on("error", () => void 0);
258
+ writer.stdin.write(`\\set ON_ERROR_STOP 0
259
+ SET session_replication_role = replica;
260
+ \\set ON_ERROR_STOP 1
261
+ ${into}
262
+ `);
263
+ reader.stdout.pipe(writer.stdin);
264
+ let done = 0;
265
+ let failed = false;
266
+ const finish = (code) => {
267
+ if (code !== 0) failed = true;
268
+ if (++done === 2) failed ? reject(new Error(stderr.trim() || "copy failed")) : resolve();
269
+ };
270
+ reader.on("close", finish);
271
+ writer.on("close", finish);
272
+ });
273
+ }
274
+ async function snapshotTables(dbUrl, schemas) {
275
+ const rows = await pgQuery(dbUrl, `
276
+ SELECT n.nspname || '.' || c.relname AS name, quote_ident(n.nspname) || '.' || quote_ident(c.relname) AS ident,
277
+ json_agg(json_build_array(a.attname, format_type(a.atttypid, a.atttypmod)) ORDER BY a.attnum) AS columns
278
+ FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
279
+ JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped AND a.attgenerated = ''
280
+ WHERE c.relkind = 'r' AND NOT c.relispartition AND n.nspname = ANY($1::text[])
281
+ GROUP BY 1, 2 ORDER BY 1`, [schemas]);
282
+ const out = /* @__PURE__ */ new Map();
283
+ for (const t of rows) {
284
+ const hashes = t.columns.map(([col], i) => {
285
+ const value = `coalesce(md5(x.${quoteIdent(col)}::text), 'n')`;
286
+ return `coalesce(md5(string_agg(${value}, '' ORDER BY ${value})), '') AS h${i}`;
287
+ });
288
+ const [row] = await pgQuery(
289
+ dbUrl,
290
+ `SELECT count(*)::text AS rows, pg_relation_filenode(${quoteLiteral(t.ident)}::regclass)::text AS filenode${hashes.length ? ", " + hashes.join(", ") : ""}
291
+ FROM ${t.ident} x`
292
+ );
293
+ out.set(t.name, {
294
+ rows: Number(row.rows),
295
+ filenode: row.filenode,
296
+ columns: Object.fromEntries(t.columns.map(([col, type], i) => [col, { type, hash: row[`h${i}`] }]))
297
+ });
298
+ }
299
+ return out;
300
+ }
301
+ function emptiedTables(sql) {
302
+ const out = /* @__PURE__ */ new Set();
303
+ for (const s of parseStatements(sql) ?? []) {
304
+ if (s.kind === "DeleteStmt") out.add(relationName(s.node.relation));
305
+ if (s.kind === "TruncateStmt") for (const r of s.node.relations ?? []) out.add(relationName(r.RangeVar));
306
+ }
307
+ return out;
308
+ }
309
+ function compareTables(before, after, sampled, migrationSql) {
310
+ const emptied = emptiedTables(migrationSql);
311
+ const lost = [];
312
+ const rewritten = [];
313
+ for (const name of sampled) {
314
+ const b = before.get(name);
315
+ const a = after.get(name);
316
+ if (!b || !a) continue;
317
+ if (b.filenode !== a.filenode) rewritten.push(name);
318
+ if (emptied.has(name.slice(name.indexOf(".") + 1))) continue;
319
+ if (a.rows < b.rows) {
320
+ lost.push(`${name}: ${b.rows} row(s) before, ${a.rows} after`);
321
+ continue;
322
+ }
323
+ if (a.rows !== b.rows) continue;
324
+ const changed = Object.entries(b.columns).filter(([col, c]) => a.columns[col] && a.columns[col].type === c.type && a.columns[col].hash !== c.hash).map(([col]) => col);
325
+ if (changed.length > 0) {
326
+ lost.push(`${name}: values changed in ${changed.join(", ")}`);
327
+ }
328
+ }
329
+ return { lost, rewritten };
330
+ }
331
+
332
+ // src/prove.ts
205
333
  var exec = promisify(execFile);
206
334
  var TARGET_EXTENSIONS_SQL = `
207
335
  SELECT e.extname AS name, n.nspname AS schema
@@ -314,6 +442,9 @@ async function proveConvergence(opts) {
314
442
  await copyStructure(opts.targetUrl, cloneUrl, schemas, tools, opts.sourceUrl);
315
443
  const scoped = opts.checks !== void 0 && !opts.checks.includes("schema");
316
444
  const before = scoped ? await schemaState(cloneUrl, schemas) : void 0;
445
+ const limit = sampleRows();
446
+ const sample = limit > 0 ? await sampleInto(psql, opts.targetUrl, cloneUrl, schemas, limit) : void 0;
447
+ const tablesBefore = sample ? await snapshotTables(cloneUrl, schemas) : void 0;
317
448
  let notApplied;
318
449
  if (opts.fixes) {
319
450
  const replayed = await runLikeApply(cloneUrl, opts.fixes);
@@ -331,7 +462,17 @@ async function proveConvergence(opts) {
331
462
  } else {
332
463
  await runSql(psql, cloneUrl, opts.migrationSql);
333
464
  }
334
- const reported = (r) => notApplied?.length ? { ...r, notApplied } : r;
465
+ let data;
466
+ if (sample && tablesBefore) {
467
+ data = { ...sample, ...compareTables(tablesBefore, await snapshotTables(cloneUrl, schemas), sample.sampled, opts.migrationSql) };
468
+ if (data.lost.length > 0) {
469
+ return { converged: false, cloneName, data, residual: data.lost.map((l) => `rows lost in ${l}`) };
470
+ }
471
+ }
472
+ const reported = (r) => {
473
+ const withData = data ? { ...r, data } : r;
474
+ return notApplied?.length ? { ...withData, notApplied } : withData;
475
+ };
335
476
  const [want, got] = await Promise.all([
336
477
  fingerprint(opts.sourceUrl, schemas),
337
478
  fingerprint(cloneUrl, schemas)
@@ -426,7 +567,7 @@ var SUPPORTING_SCHEMAS_SQL = (from) => `
426
567
  FROM pg_depend d
427
568
  JOIN pg_namespace n ON n.oid = ${NAMESPACE_OF("d.classid", "d.objid")}
428
569
  JOIN pg_namespace rn ON rn.oid = ${NAMESPACE_OF("d.refclassid", "d.refobjid")}
429
- WHERE n.nspname IN (${from.map(quoteLiteral).join(", ")})
570
+ WHERE n.nspname IN (${from.map(quoteLiteral2).join(", ")})
430
571
  AND rn.nspname <> n.nspname
431
572
  AND rn.nspname NOT IN ('pg_catalog', 'information_schema')
432
573
  AND rn.nspname NOT LIKE 'pg\\_%'
@@ -434,7 +575,7 @@ var SUPPORTING_SCHEMAS_SQL = (from) => `
434
575
  AND NOT EXISTS (SELECT 1 FROM pg_depend e
435
576
  WHERE e.classid = d.refclassid AND e.objid = d.refobjid AND e.deptype = 'e')
436
577
  ORDER BY 1`;
437
- function quoteLiteral(value) {
578
+ function quoteLiteral2(value) {
438
579
  return `'${value.replace(/'/g, "''")}'`;
439
580
  }
440
581
  function tolerateExistingSchemas(sql) {
@@ -465,7 +606,7 @@ function runSql(psqlPath, dbUrl, sql) {
465
606
  const trimmed = sql.trim();
466
607
  if (!trimmed) return Promise.resolve();
467
608
  return new Promise((resolve, reject) => {
468
- const child = spawn(psqlPath, [dbUrl, "-v", "ON_ERROR_STOP=1", "-q", "-f", "-"], {
609
+ const child = spawn2(psqlPath, [dbUrl, "-v", "ON_ERROR_STOP=1", "-q", "-f", "-"], {
469
610
  stdio: ["pipe", "pipe", "pipe"]
470
611
  });
471
612
  let stderr = "";
@@ -527,6 +668,9 @@ function explainStructureFailure(message, clientMajor, serverMajor) {
527
668
  return `could not replay the target's structure onto the clone: ${message}${versionGap}`;
528
669
  }
529
670
  var DROPPED_NAME = /\bDROP\s+(?:TABLE|VIEW|MATERIALIZED\s+VIEW|TYPE|DOMAIN|SEQUENCE|FUNCTION|PROCEDURE|INDEX|COLUMN|CONSTRAINT|POLICY|TRIGGER)\s+(?:IF\s+EXISTS\s+)?(?:(?:"[^"]+"|[\w$]+)\s*\.\s*)?("[^"]+"|[\w$]+)/gi;
671
+ function identParts(qualified) {
672
+ return [...qualified.matchAll(/"((?:[^"]|"")+)"|([^."]+)/g)].map((m) => (m[1]?.replace(/""/g, '"') ?? m[2]).toLowerCase());
673
+ }
530
674
  var CREATED_NAME = /\bCREATE\s+(?:OR\s+REPLACE\s+)?(?:UNIQUE\s+)?(?:TABLE|VIEW|MATERIALIZED\s+VIEW|TYPE|DOMAIN|SEQUENCE|FUNCTION|PROCEDURE|INDEX|POLICY|TRIGGER|EXTENSION)\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:(?:"[^"]+"|[\w$]+)\s*\.\s*)?("[^"]+"|[\w$]+)/gi;
531
675
  function residualHeldBack(residual, heldBackSql) {
532
676
  const dropped = /* @__PURE__ */ new Set();
@@ -540,10 +684,11 @@ function residualHeldBack(residual, heldBackSql) {
540
684
  const heldBack = [];
541
685
  const unexplained = [];
542
686
  for (const line of residual) {
543
- const subject = /^[a-z ]+?\s([^\s:]+(?:\s[^\s:]+)?):\s*unexpected$/i.exec(line)?.[1];
687
+ const [, subject, owner] = /^[a-z ]+?\s([^\s:]+(?:\s[^\s:]+)?):\s*unexpected(?: \(owned by (.+)\))?$/i.exec(line) ?? [];
544
688
  const name = subject?.split(/[.\s]/).pop()?.replace(/^"|"$/g, "").toLowerCase();
689
+ const owners = owner ? identParts(owner) : [];
545
690
  const missing = /^(.*):\s*missing$/i.exec(line)?.[1].toLowerCase();
546
- if (name && dropped.has(name)) heldBack.push(line);
691
+ if (name && dropped.has(name) || owners.some((o) => dropped.has(o))) heldBack.push(line);
547
692
  else if (missing && created.some((n) => missing.endsWith(` ${n}`) || missing.endsWith(`.${n}`))) heldBack.push(line);
548
693
  else unexplained.push(line);
549
694
  }
@@ -592,11 +737,16 @@ function outsideTheRun(line, checks) {
592
737
  }
593
738
 
594
739
  export {
740
+ withDatabase,
741
+ fingerprint,
595
742
  comparedSchemas,
596
743
  proveConvergence,
744
+ copyStructure,
745
+ dropDatabase,
746
+ psqlBeside,
597
747
  dropUnsupportedSetStatements,
598
748
  knownParameters,
599
749
  residualHeldBack,
600
750
  proofScope
601
751
  };
602
- //# sourceMappingURL=chunk-DMIWAJRB.js.map
752
+ //# sourceMappingURL=chunk-NEM5AEPQ.js.map
@@ -1,26 +1,27 @@
1
1
  import {
2
2
  backup
3
- } from "./chunk-NTUASFQ3.js";
3
+ } from "./chunk-YZM76W22.js";
4
4
  import {
5
5
  createDefaultRegistry,
6
6
  scan
7
- } from "./chunk-3HGAV5CC.js";
7
+ } from "./chunk-4AGJRRBT.js";
8
8
  import {
9
9
  CHECK_NAMES,
10
10
  promote
11
- } from "./chunk-UWAVXC57.js";
11
+ } from "./chunk-T7GMPGUO.js";
12
12
  import {
13
- captureSnapshot
14
- } from "./chunk-MIRLB5JM.js";
13
+ captureSnapshot,
14
+ unrestorableSchema
15
+ } from "./chunk-SM7HP6EP.js";
15
16
  import {
16
- checkConnection,
17
- describeFailure,
18
- redactUrls
19
- } from "./chunk-SQPCWGQO.js";
17
+ checkConnection
18
+ } from "./chunk-4M6OQLPO.js";
20
19
  import {
20
+ describeFailure,
21
+ redactUrls,
21
22
  resolveConfig,
22
23
  validateConfig
23
- } from "./chunk-6WAMNZLO.js";
24
+ } from "./chunk-KNXHLVF7.js";
24
25
 
25
26
  // src/mcp/state.ts
26
27
  var lastScanResult = null;
@@ -227,11 +228,18 @@ ${unreachable.join("\n")}` }], isError: true };
227
228
  };
228
229
  }
229
230
  const result = await captureSnapshot({ envName, env, config, cwd: effectiveCwd });
231
+ const unrestorable = unrestorableSchema(result.manifest);
230
232
  return {
231
233
  content: [
232
234
  {
233
235
  type: "text",
234
- text: JSON.stringify({ timestamp: result.timestamp, dir: result.dir, manifest: result.manifest }, null, 2)
236
+ text: JSON.stringify({
237
+ timestamp: result.timestamp,
238
+ dir: result.dir,
239
+ manifest: result.manifest,
240
+ // Said up front: restore is the wrong moment to find out.
241
+ ...unrestorable ? { warning: `This snapshot cannot restore the schema: ${unrestorable}` } : {}
242
+ }, null, 2)
235
243
  }
236
244
  ]
237
245
  };
@@ -496,4 +504,4 @@ export {
496
504
  clearLastScanResult,
497
505
  createServer
498
506
  };
499
- //# sourceMappingURL=chunk-6LJERPUN.js.map
507
+ //# sourceMappingURL=chunk-NX2KKUS7.js.map
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  pgQuery,
3
3
  readLocalMigrations
4
- } from "./chunk-SQPCWGQO.js";
4
+ } from "./chunk-4M6OQLPO.js";
5
5
  import {
6
6
  MIGRATIONS_SCHEMA,
7
7
  MIGRATIONS_TABLE
8
- } from "./chunk-6WAMNZLO.js";
8
+ } from "./chunk-KNXHLVF7.js";
9
9
 
10
10
  // src/migrate.ts
11
11
  import { readFile } from "fs/promises";
@@ -119,4 +119,4 @@ export {
119
119
  runMigration,
120
120
  baselineMigrations
121
121
  };
122
- //# sourceMappingURL=chunk-SC2XHGIS.js.map
122
+ //# sourceMappingURL=chunk-O5ERC2SE.js.map