@akal/supaforge 0.0.34 → 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 (49) hide show
  1. package/README.md +28 -12
  2. package/dist/base-command.d.ts +1 -1
  3. package/dist/base-command.js +2 -2
  4. package/dist/{branch-C4ndfoj5.d.ts → branch-BppjQBv_.d.ts} +1 -1
  5. package/dist/{chunk-PYAGSMFB.js → chunk-44UVQJV2.js} +146 -13
  6. package/dist/{chunk-OB5GP7UB.js → chunk-4AGJRRBT.js} +376 -32
  7. package/dist/{chunk-MG3SFV5J.js → chunk-4M6OQLPO.js} +1 -1
  8. package/dist/{chunk-TPX2MAO4.js → chunk-5Q6LSLA3.js} +53 -4
  9. package/dist/{chunk-F46FFUKC.js → chunk-7N63OPFN.js} +20 -12
  10. package/dist/chunk-AGONS5JJ.js +242 -0
  11. package/dist/{chunk-7DL63NWP.js → chunk-H2D3YSKM.js} +6 -2
  12. package/dist/{chunk-3WKWPSFN.js → chunk-NEM5AEPQ.js} +171 -21
  13. package/dist/{chunk-6ERVCWLY.js → chunk-NX2KKUS7.js} +6 -6
  14. package/dist/{chunk-S5L4A3UR.js → chunk-O5ERC2SE.js} +2 -2
  15. package/dist/{chunk-ICSCHS72.js → chunk-SM7HP6EP.js} +10 -180
  16. package/dist/{chunk-N5VNNOA2.js → chunk-T7GMPGUO.js} +578 -16
  17. package/dist/{chunk-DQJSSTIJ.js → chunk-WNTCZM2J.js} +4 -476
  18. package/dist/{chunk-5STH2A6K.js → chunk-XLZCX6R2.js} +2 -2
  19. package/dist/{chunk-EFKQOQGG.js → chunk-YWERIWFD.js} +6 -4
  20. package/dist/{chunk-V2SBZERO.js → chunk-YZM76W22.js} +3 -3
  21. package/dist/commands/clone.d.ts +2 -2
  22. package/dist/commands/clone.js +7 -6
  23. package/dist/commands/diff.d.ts +24 -22
  24. package/dist/commands/diff.js +11 -11
  25. package/dist/commands/hukam.d.ts +2 -3
  26. package/dist/commands/hukam.js +11 -11
  27. package/dist/commands/init.d.ts +1 -1
  28. package/dist/commands/mcp.js +8 -8
  29. package/dist/commands/migrate/baseline.d.ts +1 -1
  30. package/dist/commands/migrate/baseline.js +3 -3
  31. package/dist/commands/migrate/create.d.ts +1 -1
  32. package/dist/commands/migrate/create.js +12 -7
  33. package/dist/commands/migrate/list.d.ts +1 -1
  34. package/dist/commands/migrate/list.js +3 -3
  35. package/dist/commands/migrate/run.d.ts +4 -1
  36. package/dist/commands/migrate/run.js +7 -6
  37. package/dist/commands/report.d.ts +1 -1
  38. package/dist/commands/report.js +2 -2
  39. package/dist/commands/restore.d.ts +1 -1
  40. package/dist/commands/restore.js +10 -10
  41. package/dist/commands/snapshot.d.ts +1 -1
  42. package/dist/commands/snapshot.js +5 -5
  43. package/dist/commands/sync.d.ts +2 -3
  44. package/dist/commands/sync.js +11 -11
  45. package/dist/{config-CTbeRlJk.d.ts → config-l3ggjw9r.d.ts} +2 -0
  46. package/dist/index.d.ts +21 -4
  47. package/dist/index.js +13 -13
  48. package/package.json +2 -2
  49. package/dist/chunk-DQQ5E563.js +0 -64
@@ -1,24 +1,24 @@
1
1
  import {
2
2
  loadMigrations
3
- } from "./chunk-V2SBZERO.js";
3
+ } from "./chunk-YZM76W22.js";
4
4
  import {
5
5
  ensureMigrationsTable,
6
6
  getAppliedVersions
7
- } from "./chunk-S5L4A3UR.js";
7
+ } from "./chunk-O5ERC2SE.js";
8
+ import {
9
+ PLATFORM_ROLES
10
+ } from "./chunk-WNTCZM2J.js";
8
11
  import {
9
12
  dropUnsupportedSetStatements,
10
13
  knownParameters
11
- } from "./chunk-3WKWPSFN.js";
14
+ } from "./chunk-NEM5AEPQ.js";
12
15
  import {
13
16
  ABSENT_ON_TARGET,
14
17
  EXTENSION_UNAVAILABLE,
15
- PLATFORM_ROLES
16
- } from "./chunk-DQJSSTIJ.js";
17
- import {
18
18
  rolesNamedBy,
19
19
  sqlSkeleton,
20
20
  statementSubject
21
- } from "./chunk-N5VNNOA2.js";
21
+ } from "./chunk-T7GMPGUO.js";
22
22
  import {
23
23
  isCommentOnly,
24
24
  isPsqlMetaCommand,
@@ -27,14 +27,14 @@ import {
27
27
  } from "./chunk-WK5YTINL.js";
28
28
  import {
29
29
  loadSnapshot
30
- } from "./chunk-ICSCHS72.js";
30
+ } from "./chunk-SM7HP6EP.js";
31
31
  import {
32
32
  newPgClient,
33
33
  pgQuery,
34
34
  quoteIdent,
35
35
  quoteLiteral,
36
36
  quoteName
37
- } from "./chunk-MG3SFV5J.js";
37
+ } from "./chunk-4M6OQLPO.js";
38
38
  import {
39
39
  DEFAULT_IGNORE_SCHEMAS,
40
40
  MIGRATIONS_TABLE,
@@ -492,7 +492,7 @@ async function restoreFromSnapshot(options) {
492
492
  delete result.incomplete;
493
493
  }
494
494
  if (!(err instanceof RestoreAborted)) {
495
- result.errors.push({ type: "sql", label: "Restore", error: errMsg(err) });
495
+ result.errors.push({ type: "sql", label: "Restore", error: restoreError(err, transactional) });
496
496
  }
497
497
  } finally {
498
498
  await client.end();
@@ -809,7 +809,7 @@ async function applyStatement(client, sql, transactional, result, missing, leftO
809
809
  else result.incomplete = true;
810
810
  return;
811
811
  }
812
- result.errors.push({ type: "sql", label: summarizeStatement(sql), error: errMsg(err) });
812
+ result.errors.push({ type: "sql", label: summarizeStatement(sql), error: restoreError(err, transactional) });
813
813
  if (transactional) throw new RestoreAborted();
814
814
  }
815
815
  }
@@ -855,6 +855,14 @@ END $$;`;
855
855
  }
856
856
  var RestoreAborted = class extends Error {
857
857
  };
858
+ function restoreError(err, transactional) {
859
+ const message = errMsg(err);
860
+ const code = err?.code;
861
+ if (code === "53200" && transactional) {
862
+ 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.`;
863
+ }
864
+ return message;
865
+ }
858
866
 
859
867
  export {
860
868
  getPublicTables,
@@ -864,4 +872,4 @@ export {
864
872
  previewMigrationRestore,
865
873
  summarizeStatement
866
874
  };
867
- //# sourceMappingURL=chunk-F46FFUKC.js.map
875
+ //# sourceMappingURL=chunk-7N63OPFN.js.map
@@ -0,0 +1,242 @@
1
+ import {
2
+ newPgClient
3
+ } from "./chunk-4M6OQLPO.js";
4
+
5
+ // src/utils/strings.ts
6
+ var MAX_SLUG_LENGTH = 60;
7
+ function slugify(name, separator = "_") {
8
+ const pattern = new RegExp(`[^a-z0-9]+`, "g");
9
+ const edgePattern = new RegExp(`^${escapeRegex(separator)}|${escapeRegex(separator)}$`, "g");
10
+ return name.toLowerCase().replace(pattern, separator).replace(edgePattern, "").slice(0, MAX_SLUG_LENGTH);
11
+ }
12
+ function escapeRegex(s) {
13
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
14
+ }
15
+ function parseFlagList(values) {
16
+ if (!Array.isArray(values)) return [];
17
+ return values.flatMap((v) => String(v).split(",")).map((v) => v.trim()).filter((v) => v.length > 0);
18
+ }
19
+ function globToRegExp(pattern) {
20
+ let out = "^";
21
+ for (const ch of pattern) {
22
+ if (ch === "*") out += ".*";
23
+ else if (ch === "?") out += ".";
24
+ else out += escapeRegex(ch);
25
+ }
26
+ return new RegExp(out + "$");
27
+ }
28
+ function matchesGlob(value, pattern) {
29
+ return globToRegExp(pattern.toLowerCase()).test(value.toLowerCase());
30
+ }
31
+ function normalizeRoles(roles) {
32
+ const arr = Array.isArray(roles) ? roles : [roles];
33
+ return [...new Set(
34
+ arr.flatMap((r) => /^\{.*\}$/s.test(r) ? arrayLiteralElements(r) : [r]).map((r) => r.trim()).filter(Boolean)
35
+ )].sort();
36
+ }
37
+ function arrayLiteralElements(literal) {
38
+ const inner = literal.slice(1, -1);
39
+ const out = [];
40
+ let i = 0;
41
+ while (i < inner.length) {
42
+ let value = "";
43
+ if (inner[i] === '"') {
44
+ for (i++; i < inner.length && inner[i] !== '"'; i++) {
45
+ if (inner[i] === "\\") i++;
46
+ value += inner[i];
47
+ }
48
+ i++;
49
+ } else {
50
+ const end = inner.indexOf(",", i);
51
+ value = inner.slice(i, end === -1 ? inner.length : end);
52
+ i = end === -1 ? inner.length : end;
53
+ }
54
+ out.push(value);
55
+ if (inner[i] === ",") i++;
56
+ }
57
+ return out;
58
+ }
59
+
60
+ // src/pg-tools.ts
61
+ import { execFile } from "child_process";
62
+ import { platform } from "os";
63
+ import { existsSync } from "fs";
64
+ function parsePgDumpVersion(output) {
65
+ const m = output.match(/(\d+)(?:\.\d+)?/);
66
+ return m ? Number(m[1]) : null;
67
+ }
68
+ function parseServerVersion(versionStr) {
69
+ const m = versionStr.match(/^(\d+)/);
70
+ return m ? Number(m[1]) : null;
71
+ }
72
+ async function getLocalPgDumpVersion() {
73
+ return new Promise((resolve) => {
74
+ execFile("pg_dump", ["--version"], (err, stdout) => {
75
+ if (err) return resolve(null);
76
+ resolve(parsePgDumpVersion(stdout));
77
+ });
78
+ });
79
+ }
80
+ function getPgDumpVersionAt(path) {
81
+ return new Promise((resolve) => {
82
+ execFile(path, ["--version"], (err, stdout) => {
83
+ if (err) return resolve(null);
84
+ resolve(parsePgDumpVersion(stdout));
85
+ });
86
+ });
87
+ }
88
+ var VERSIONED_PG_DIRS = [
89
+ "/usr/lib/postgresql/{v}/bin",
90
+ // Debian / Ubuntu (PGDG)
91
+ "/opt/homebrew/opt/postgresql@{v}/bin",
92
+ // macOS Homebrew (Apple Silicon)
93
+ "/usr/local/opt/postgresql@{v}/bin",
94
+ // macOS Homebrew (Intel)
95
+ "/usr/pgsql-{v}/bin"
96
+ // RHEL / CentOS (PGDG)
97
+ ];
98
+ async function resolvePgDumpPath(serverMajor) {
99
+ const named = process.env.SUPAFORGE_PG_BIN?.trim();
100
+ if (named) {
101
+ if (!existsSync(`${named}/pg_dump`)) {
102
+ throw new Error(`SUPAFORGE_PG_BIN is set to "${named}", which has no pg_dump`);
103
+ }
104
+ const major = await getPgDumpVersionAt(`${named}/pg_dump`);
105
+ if (major !== null && major >= serverMajor) return { path: `${named}/pg_dump`, major };
106
+ }
107
+ const defaultMajor = await getLocalPgDumpVersion();
108
+ if (defaultMajor !== null && defaultMajor >= serverMajor) {
109
+ return { path: "pg_dump", major: defaultMajor };
110
+ }
111
+ for (const v of versionsToCheck(serverMajor)) {
112
+ for (const tmpl of VERSIONED_PG_DIRS) {
113
+ const dir = tmpl.replace("{v}", String(v));
114
+ const candidate = `${dir}/pg_dump`;
115
+ if (!existsSync(candidate)) continue;
116
+ const major = await getPgDumpVersionAt(candidate);
117
+ if (major !== null && major >= serverMajor) {
118
+ return { path: candidate, major };
119
+ }
120
+ }
121
+ }
122
+ return null;
123
+ }
124
+ function assertPgBinUsable() {
125
+ const named = process.env.SUPAFORGE_PG_BIN?.trim();
126
+ if (named && !existsSync(`${named}/pg_dump`)) {
127
+ throw new Error(
128
+ `SUPAFORGE_PG_BIN is set to "${named}", which has no pg_dump. Point it at a directory of PostgreSQL client tools, or unset it to use the ones on PATH.`
129
+ );
130
+ }
131
+ }
132
+ async function describeMissingPgDump(neededMajor) {
133
+ const onPath = await getLocalPgDumpVersion();
134
+ const named = process.env.SUPAFORGE_PG_BIN?.trim();
135
+ const namedMajor = named ? await getPgDumpVersionAt(`${named}/pg_dump`) : null;
136
+ const found = [
137
+ onPath === null ? "none on PATH" : `${onPath} on PATH`,
138
+ ...named ? [`${namedMajor ?? "none"} in SUPAFORGE_PG_BIN`] : []
139
+ ].join(", ");
140
+ return `no pg_dump ${neededMajor} or newer (found: ${found}); set SUPAFORGE_PG_BIN to a directory of PostgreSQL ${neededMajor}+ client tools`;
141
+ }
142
+ function resolvePgRestorePath(pgDumpPath) {
143
+ if (pgDumpPath === "pg_dump") return "pg_restore";
144
+ return pgDumpPath.replace(/pg_dump$/, "pg_restore");
145
+ }
146
+ function versionsToCheck(required) {
147
+ const versions = [];
148
+ for (let v = required + 3; v >= required; v--) {
149
+ versions.push(v);
150
+ }
151
+ return versions;
152
+ }
153
+ async function getServerMajorVersion(dbUrl) {
154
+ const client = newPgClient(dbUrl);
155
+ try {
156
+ await client.connect();
157
+ const { rows } = await client.query("SHOW server_version");
158
+ const raw = rows[0].server_version;
159
+ const major = parseServerVersion(raw);
160
+ if (!major) throw new Error(`Could not parse server version: ${raw}`);
161
+ return major;
162
+ } finally {
163
+ await client.end();
164
+ }
165
+ }
166
+ async function checkPgDumpCompat(dbUrl) {
167
+ const serverMajor = await getServerMajorVersion(dbUrl);
168
+ const resolved = await resolvePgDumpPath(serverMajor);
169
+ if (resolved) {
170
+ return {
171
+ localMajor: resolved.major,
172
+ serverMajor,
173
+ compatible: true,
174
+ message: "",
175
+ pgDumpPath: resolved.path,
176
+ pgRestorePath: resolvePgRestorePath(resolved.path)
177
+ };
178
+ }
179
+ const localMajor = await getLocalPgDumpVersion();
180
+ const message = buildInstallMessage(localMajor, serverMajor);
181
+ return {
182
+ localMajor,
183
+ serverMajor,
184
+ compatible: false,
185
+ message,
186
+ pgDumpPath: "pg_dump",
187
+ pgRestorePath: "pg_restore"
188
+ };
189
+ }
190
+ function buildInstallMessage(localMajor, serverMajor) {
191
+ const header = localMajor === null ? `pg_dump not found. The server is PostgreSQL ${serverMajor} \u2014 install postgresql-client-${serverMajor}:` : `pg_dump version mismatch: local v${localMajor}, server v${serverMajor}. Install postgresql-client-${serverMajor}:`;
192
+ const instructions = getInstallInstructions(serverMajor);
193
+ return `${header}
194
+
195
+ ${instructions}`;
196
+ }
197
+ function getInstallInstructions(majorVersion) {
198
+ const os = platform();
199
+ const v = majorVersion;
200
+ switch (os) {
201
+ case "darwin":
202
+ return [
203
+ ` brew install postgresql@${v}`,
204
+ ` # Then add to PATH:`,
205
+ ` export PATH="/opt/homebrew/opt/postgresql@${v}/bin:$PATH"`
206
+ ].join("\n");
207
+ case "linux":
208
+ return [
209
+ ` # Debian / Ubuntu:`,
210
+ ` sudo apt-get install -y postgresql-client-${v}`,
211
+ ` # If the package is not found, add the PGDG repository first:`,
212
+ ` # https://www.postgresql.org/download/linux/ubuntu/`,
213
+ ``,
214
+ ` # RHEL / Fedora:`,
215
+ ` sudo dnf install -y postgresql${v}`
216
+ ].join("\n");
217
+ case "win32":
218
+ return [
219
+ ` # Download the installer from:`,
220
+ ` # https://www.postgresql.org/download/windows/`,
221
+ ` # Or use Chocolatey:`,
222
+ ` choco install postgresql${v} --params '/Password:postgres'`
223
+ ].join("\n");
224
+ default:
225
+ return ` Download PostgreSQL ${v} client tools from: https://www.postgresql.org/download/`;
226
+ }
227
+ }
228
+
229
+ export {
230
+ slugify,
231
+ escapeRegex,
232
+ parseFlagList,
233
+ globToRegExp,
234
+ matchesGlob,
235
+ normalizeRoles,
236
+ resolvePgDumpPath,
237
+ assertPgBinUsable,
238
+ describeMissingPgDump,
239
+ getServerMajorVersion,
240
+ checkPgDumpCompat
241
+ };
242
+ //# sourceMappingURL=chunk-AGONS5JJ.js.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  CHECK_META,
3
3
  isComparisonCheck
4
- } from "./chunk-DQJSSTIJ.js";
4
+ } from "./chunk-T7GMPGUO.js";
5
5
  import {
6
6
  CHECK_LINE_PADDING,
7
7
  bold,
@@ -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-H2D3YSKM.js.map
@@ -1,10 +1,10 @@
1
1
  import {
2
- runLikeApply
3
- } from "./chunk-DQJSSTIJ.js";
4
- import {
2
+ parseStatements,
3
+ relationName,
5
4
  rolesNamedBy,
5
+ runLikeApply,
6
6
  sqlSkeleton
7
- } from "./chunk-N5VNNOA2.js";
7
+ } from "./chunk-T7GMPGUO.js";
8
8
  import {
9
9
  isCommentOnly,
10
10
  splitSqlStatements
@@ -12,19 +12,19 @@ import {
12
12
  import {
13
13
  describeMissingPgDump,
14
14
  getServerMajorVersion,
15
+ globToRegExp,
15
16
  resolvePgDumpPath
16
- } from "./chunk-ICSCHS72.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-MG3SFV5J.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-3WKWPSFN.js.map
752
+ //# sourceMappingURL=chunk-NEM5AEPQ.js.map
@@ -1,21 +1,21 @@
1
1
  import {
2
2
  backup
3
- } from "./chunk-V2SBZERO.js";
3
+ } from "./chunk-YZM76W22.js";
4
4
  import {
5
5
  createDefaultRegistry,
6
6
  scan
7
- } from "./chunk-OB5GP7UB.js";
7
+ } from "./chunk-4AGJRRBT.js";
8
8
  import {
9
9
  CHECK_NAMES,
10
10
  promote
11
- } from "./chunk-DQJSSTIJ.js";
11
+ } from "./chunk-T7GMPGUO.js";
12
12
  import {
13
13
  captureSnapshot,
14
14
  unrestorableSchema
15
- } from "./chunk-ICSCHS72.js";
15
+ } from "./chunk-SM7HP6EP.js";
16
16
  import {
17
17
  checkConnection
18
- } from "./chunk-MG3SFV5J.js";
18
+ } from "./chunk-4M6OQLPO.js";
19
19
  import {
20
20
  describeFailure,
21
21
  redactUrls,
@@ -504,4 +504,4 @@ export {
504
504
  clearLastScanResult,
505
505
  createServer
506
506
  };
507
- //# sourceMappingURL=chunk-6ERVCWLY.js.map
507
+ //# sourceMappingURL=chunk-NX2KKUS7.js.map