@akal/supaforge 0.0.37 → 0.0.38

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.
@@ -3,16 +3,21 @@ import {
3
3
  splitSqlStatements
4
4
  } from "./chunk-WK5YTINL.js";
5
5
  import {
6
+ describeMissingPgDump,
6
7
  escapeRegex,
8
+ getServerMajorVersion,
7
9
  globToRegExp,
8
10
  matchesGlob,
9
- parseFlagList
11
+ parseFlagList,
12
+ resolvePgDumpPath
10
13
  } from "./chunk-Z2YUOA4W.js";
11
14
  import {
12
15
  Check,
13
16
  heldSnapshot,
14
17
  newPgClient,
15
18
  pgQuery,
19
+ quoteIdent,
20
+ quoteLiteral,
16
21
  quoteName,
17
22
  snapshotsOff
18
23
  } from "./chunk-KT7NMELZ.js";
@@ -128,6 +133,14 @@ function isComparisonCheck(name) {
128
133
  return CHECK_KIND[name] === "comparison";
129
134
  }
130
135
 
136
+ // src/dbdiff.ts
137
+ import { execFile } from "child_process";
138
+ import { createRequire } from "module";
139
+ import { readFile, unlink, access } from "fs/promises";
140
+ import { tmpdir } from "os";
141
+ import { join } from "path";
142
+ import { promisify } from "util";
143
+
131
144
  // src/checks/roles.ts
132
145
  var PLATFORM_ROLES = [
133
146
  "postgres",
@@ -621,14 +634,6 @@ function diffGrants(source, target, missingRoles = /* @__PURE__ */ new Set()) {
621
634
  return issues;
622
635
  }
623
636
 
624
- // src/dbdiff.ts
625
- import { execFile } from "child_process";
626
- import { createRequire } from "module";
627
- import { readFile, unlink, access } from "fs/promises";
628
- import { tmpdir } from "os";
629
- import { join } from "path";
630
- import { promisify } from "util";
631
-
632
637
  // src/sql-ast.ts
633
638
  import { loadModule, parseSync } from "libpg-query";
634
639
  await loadModule();
@@ -1376,7 +1381,7 @@ function unfoldedIssues(result, check, ignoreSchemas, ignoredSchemaTables) {
1376
1381
  // The DOWN counterpart is passed so a title can recover a schema the UP
1377
1382
  // statement does not carry — see routineLabel (issue #47).
1378
1383
  title: summariseStatement(lead, check, downSql),
1379
- description: `${check === "schema" ? "Schema" : "Data"} difference detected by @dbdiff/cli.`,
1384
+ description: describeDifference(type),
1380
1385
  sql: { up: upSql, down: downSql }
1381
1386
  };
1382
1387
  });
@@ -1427,7 +1432,7 @@ function issuesFromUnits(upUnits, downUnits, check, ignoreSchemas, ignoredSchema
1427
1432
  check,
1428
1433
  severity: DROP_TYPES.includes(type) ? "critical" : "warning",
1429
1434
  title: summariseStatement(lead, check, downSql),
1430
- description: `${check === "schema" ? "Schema" : "Data"} difference detected by @dbdiff/cli.`,
1435
+ description: describeDifference(type),
1431
1436
  sql: { up: upSql, down: downSql },
1432
1437
  ...manual
1433
1438
  };
@@ -1736,6 +1741,16 @@ function splitStatements(sql) {
1736
1741
  function withoutLeadingComments(chunk) {
1737
1742
  return chunk.trim().replace(/^(?:--[^\n]*(?:\n|$)\s*)+/, "").trim();
1738
1743
  }
1744
+ function describeDifference(type) {
1745
+ if (type === "insert") return "Rows in the source are missing from the target. The fix inserts them.";
1746
+ if (type === "update") return "Rows differ between source and target. The fix updates the target's rows to match.";
1747
+ if (type === "delete") return "The target has rows the source does not. The fix deletes them from the target.";
1748
+ if (type === "drop" || type.startsWith("drop-")) {
1749
+ return "The target has this and the source does not. The fix drops it from the target, and with it any data it holds.";
1750
+ }
1751
+ if (type.startsWith("create")) return "The source has this and the target does not. The fix creates it on the target.";
1752
+ return "This exists on both sides but differs. The fix changes the target to match the source.";
1753
+ }
1739
1754
  function classifyStatement(sql) {
1740
1755
  const upper = sql.toUpperCase().trimStart();
1741
1756
  if (upper.startsWith("CREATE VIEW") || upper.startsWith("CREATE OR REPLACE VIEW")) return "create-view";
@@ -2475,6 +2490,729 @@ async function promote(options) {
2475
2490
  return result;
2476
2491
  }
2477
2492
 
2493
+ // src/prove.ts
2494
+ import { randomBytes } from "crypto";
2495
+ import { execFile as execFile2, spawn as spawn2 } from "child_process";
2496
+ import { promisify as promisify2 } from "util";
2497
+ import { fingerprintSql, stateSql } from "@akal/pg-conformance";
2498
+
2499
+ // src/expression-forms.ts
2500
+ var LITERAL = `'(?:[^']|'')*'(?:::[a-z_][\\w ]*(?:\\(\\d+(?:,\\s*\\d+)?\\))?)?`;
2501
+ var CAST_ARRAY = new RegExp(`\\(ARRAY\\[(${LITERAL}(?:, ${LITERAL})*)\\]\\)::([a-z_][\\w ]*?(?:\\(\\d+(?:,\\s*\\d+)?\\))?)\\[\\]`, "g");
2502
+ var ELEMENT = new RegExp(LITERAL, "g");
2503
+ function canonicalArrays(text) {
2504
+ return text.replace(CAST_ARRAY, (_match, elements, type) => `ARRAY[${(elements.match(ELEMENT) ?? []).map((e) => `(${e})::${type}`).join(", ")}]`);
2505
+ }
2506
+
2507
+ // src/state-diff.ts
2508
+ var canonical = (d) => typeof d === "string" ? canonicalArrays(d) : d;
2509
+ var show = (v) => {
2510
+ if (v === null || v === void 0) return "none";
2511
+ if (typeof v === "boolean") return v ? "yes" : "no";
2512
+ if (Array.isArray(v)) return v.length ? v.join(", ") : "none";
2513
+ if (typeof v === "object") return JSON.stringify(v);
2514
+ return String(v);
2515
+ };
2516
+ var qualify = (schema, name) => `${schema}.${name}`;
2517
+ function compareField(out, subject, label, before, after) {
2518
+ const a = show(before), b = show(after);
2519
+ if (a !== b) out.push(`${subject}: ${label} ${a} \u2192 ${b}`);
2520
+ }
2521
+ function compareKeyed(out, kind, before, after, keyOf, compare, note = () => "") {
2522
+ const a = new Map((before ?? []).map((x) => [keyOf(x), x]));
2523
+ const b = new Map((after ?? []).map((x) => [keyOf(x), x]));
2524
+ for (const [key, item] of a) {
2525
+ if (!b.has(key)) out.push(`${kind} ${key}: missing`);
2526
+ else compare(out, `${kind} ${key}`, item, b.get(key));
2527
+ }
2528
+ for (const [key, item] of b) {
2529
+ if (!a.has(key)) out.push(`${kind} ${key}: unexpected${note(item)}`);
2530
+ }
2531
+ }
2532
+ function compareColumn(out, subject, a, b) {
2533
+ compareField(out, subject, "type", a.type, b.type);
2534
+ compareField(out, subject, "not null", a.not_null, b.not_null);
2535
+ compareField(out, subject, "default", a.default, b.default);
2536
+ compareField(out, subject, "identity", a.identity, b.identity);
2537
+ compareField(out, subject, "identity options", a.identity_options, b.identity_options);
2538
+ compareField(out, subject, "generated", a.generated, b.generated);
2539
+ compareField(out, subject, "storage", a.storage, b.storage);
2540
+ compareField(out, subject, "compression", a.compression, b.compression);
2541
+ compareField(out, subject, "collation", a.collation, b.collation);
2542
+ compareField(out, subject, "comment", a.comment, b.comment);
2543
+ }
2544
+ function compareTable(out, subject, a, b) {
2545
+ compareField(out, subject, "kind", a.kind, b.kind);
2546
+ compareField(out, subject, "unlogged", a.unlogged, b.unlogged);
2547
+ compareField(out, subject, "partition of", a.partition_of, b.partition_of);
2548
+ compareField(out, subject, "partition bound", a.partition_bound, b.partition_bound);
2549
+ compareField(out, subject, "partition by", a.partition_by, b.partition_by);
2550
+ compareField(out, subject, "inherits", a.inherits, b.inherits);
2551
+ compareField(out, subject, "options", a.options, b.options);
2552
+ compareField(out, subject, "RLS enabled", a.rls_enabled, b.rls_enabled);
2553
+ compareField(out, subject, "RLS forced", a.rls_forced, b.rls_forced);
2554
+ compareField(out, subject, "comment", a.comment, b.comment);
2555
+ const prefix = subject.replace(/^table /, "");
2556
+ compareKeyed(
2557
+ out,
2558
+ "column",
2559
+ a.columns,
2560
+ b.columns,
2561
+ (c) => `${prefix}.${c.name}`,
2562
+ (o, s, x, y) => compareColumn(o, s, x, y)
2563
+ );
2564
+ compareKeyed(
2565
+ out,
2566
+ `constraint on ${prefix}`,
2567
+ a.constraints,
2568
+ b.constraints,
2569
+ (c) => c.type === "not_null" ? c.definition : c.name,
2570
+ (o, s, x, y) => {
2571
+ compareField(o, s, "definition", canonical(x.definition), canonical(y.definition));
2572
+ compareField(o, s, "validated", x.validated, y.validated);
2573
+ compareField(o, s, "deferrable", x.deferrable, y.deferrable);
2574
+ compareField(o, s, "deferred", x.deferred, y.deferred);
2575
+ }
2576
+ );
2577
+ compareKeyed(
2578
+ out,
2579
+ `index on ${prefix}`,
2580
+ a.indexes,
2581
+ b.indexes,
2582
+ (i) => i.name,
2583
+ (o, s, x, y) => compareField(o, s, "definition", canonical(x.definition), canonical(y.definition))
2584
+ );
2585
+ compareKeyed(
2586
+ out,
2587
+ `policy on ${prefix}`,
2588
+ a.policies,
2589
+ b.policies,
2590
+ (p) => p.name,
2591
+ (o, s, x, y) => {
2592
+ compareField(o, s, "command", x.command, y.command);
2593
+ compareField(o, s, "permissive", x.permissive, y.permissive);
2594
+ compareField(o, s, "roles", x.roles, y.roles);
2595
+ compareField(o, s, "using", canonical(x.using), canonical(y.using));
2596
+ compareField(o, s, "with check", canonical(x.with_check), canonical(y.with_check));
2597
+ }
2598
+ );
2599
+ compareKeyed(
2600
+ out,
2601
+ `trigger on ${prefix}`,
2602
+ a.triggers,
2603
+ b.triggers,
2604
+ (t) => t.name,
2605
+ (o, s, x, y) => compareField(o, s, "definition", canonical(x.definition), canonical(y.definition))
2606
+ );
2607
+ }
2608
+ function diffState(before, after) {
2609
+ const out = [];
2610
+ compareKeyed(
2611
+ out,
2612
+ "table",
2613
+ before.tables,
2614
+ after.tables,
2615
+ (t) => qualify(t.schema, t.name),
2616
+ (o, s, a, b) => compareTable(o, s, a, b)
2617
+ );
2618
+ compareKeyed(
2619
+ out,
2620
+ "view",
2621
+ before.views,
2622
+ after.views,
2623
+ (v) => qualify(v.schema, v.name),
2624
+ (o, s, a, b) => {
2625
+ compareField(o, s, "materialized", a.materialized, b.materialized);
2626
+ compareField(o, s, "definition", canonical(a.definition), canonical(b.definition));
2627
+ compareField(o, s, "options", a.options, b.options);
2628
+ }
2629
+ );
2630
+ compareKeyed(
2631
+ out,
2632
+ "sequence",
2633
+ before.sequences,
2634
+ after.sequences,
2635
+ (q) => qualify(q.schema, q.name),
2636
+ (o, s, a, b) => {
2637
+ for (const k of ["type", "start", "increment", "min", "max", "cache", "cycle", "owned_by"]) {
2638
+ compareField(o, s, k.replace("_", " "), a[k], b[k]);
2639
+ }
2640
+ },
2641
+ // A serial or identity column's: it goes with its table.
2642
+ (q) => q.owned_by ? ` (owned by ${q.owned_by})` : ""
2643
+ );
2644
+ compareKeyed(
2645
+ out,
2646
+ "routine",
2647
+ before.routines,
2648
+ after.routines,
2649
+ (r) => `${qualify(r.schema, r.name)}(${r.arguments})`,
2650
+ (o, s, a, b) => {
2651
+ compareField(o, s, "kind", a.kind, b.kind);
2652
+ compareField(o, s, "definition", canonical(a.definition), canonical(b.definition));
2653
+ }
2654
+ );
2655
+ compareKeyed(
2656
+ out,
2657
+ "type",
2658
+ before.types,
2659
+ after.types,
2660
+ (t) => qualify(t.schema, t.name),
2661
+ (o, s, a, b) => {
2662
+ compareField(o, s, "kind", a.kind, b.kind);
2663
+ compareField(o, s, "labels", a.labels, b.labels);
2664
+ compareField(o, s, "base type", a.base_type, b.base_type);
2665
+ compareField(o, s, "not null", a.not_null, b.not_null);
2666
+ compareField(o, s, "default", a.default, b.default);
2667
+ compareField(o, s, "constraints", a.constraints, b.constraints);
2668
+ compareField(o, s, "attributes", a.attributes, b.attributes);
2669
+ }
2670
+ );
2671
+ compareKeyed(
2672
+ out,
2673
+ "extension",
2674
+ before.extensions,
2675
+ after.extensions,
2676
+ (e) => e.name,
2677
+ (o, s, a, b) => compareField(o, s, "version", a.version, b.version)
2678
+ );
2679
+ return out;
2680
+ }
2681
+
2682
+ // src/prove.ts
2683
+ import { join as join2, dirname } from "path";
2684
+
2685
+ // src/prove-data.ts
2686
+ import { spawn } from "child_process";
2687
+ var DEFAULT_SAMPLE_ROWS = 1e3;
2688
+ function sampleRows() {
2689
+ const raw = process.env.SUPAFORGE_PROVE_ROWS;
2690
+ const n = raw === void 0 ? DEFAULT_SAMPLE_ROWS : Number(raw);
2691
+ return Number.isInteger(n) && n >= 0 ? n : DEFAULT_SAMPLE_ROWS;
2692
+ }
2693
+ var TABLES_SQL = `
2694
+ SELECT n.nspname || '.' || c.relname AS name, quote_ident(n.nspname) || '.' || quote_ident(c.relname) AS ident,
2695
+ (SELECT string_agg(quote_ident(a.attname), ', ' ORDER BY a.attnum) FROM pg_attribute a
2696
+ WHERE a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped AND a.attgenerated = '') AS columns
2697
+ FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
2698
+ WHERE c.relkind = 'r' AND NOT c.relispartition AND n.nspname = ANY($1::text[])
2699
+ ORDER BY 1`;
2700
+ async function sampleInto(psql, fromUrl, intoUrl, schemas, limit) {
2701
+ const tables = await pgQuery(fromUrl, TABLES_SQL, [schemas]);
2702
+ const sampled = [];
2703
+ const unchecked = [];
2704
+ for (const t of tables) {
2705
+ if (!t.columns) continue;
2706
+ try {
2707
+ await pipeCopy(
2708
+ psql,
2709
+ fromUrl,
2710
+ intoUrl,
2711
+ `\\copy (SELECT ${t.columns} FROM ${t.ident} LIMIT ${limit}) TO STDOUT`,
2712
+ `\\copy ${t.ident} (${t.columns}) FROM STDIN`
2713
+ );
2714
+ sampled.push(t.name);
2715
+ } catch {
2716
+ unchecked.push(t.name);
2717
+ }
2718
+ }
2719
+ return { sampled, unchecked };
2720
+ }
2721
+ function pipeCopy(psql, fromUrl, intoUrl, out, into) {
2722
+ return new Promise((resolve, reject) => {
2723
+ const reader = spawn(psql, [fromUrl, "-X", "-q", "-v", "ON_ERROR_STOP=1", "-c", out], { stdio: ["ignore", "pipe", "pipe"] });
2724
+ const writer = spawn(psql, [intoUrl, "-X", "-q", "-v", "ON_ERROR_STOP=1", "-f", "-"], { stdio: ["pipe", "pipe", "pipe"] });
2725
+ let stderr = "";
2726
+ reader.stderr.on("data", (d) => {
2727
+ stderr += d.toString();
2728
+ });
2729
+ writer.stderr.on("data", (d) => {
2730
+ stderr += d.toString();
2731
+ });
2732
+ reader.on("error", reject);
2733
+ writer.on("error", reject);
2734
+ writer.stdin.on("error", () => void 0);
2735
+ writer.stdin.write(`\\set ON_ERROR_STOP 0
2736
+ SET session_replication_role = replica;
2737
+ \\set ON_ERROR_STOP 1
2738
+ ${into}
2739
+ `);
2740
+ reader.stdout.pipe(writer.stdin);
2741
+ let done = 0;
2742
+ let failed = false;
2743
+ const finish = (code) => {
2744
+ if (code !== 0) failed = true;
2745
+ if (++done === 2) failed ? reject(new Error(stderr.trim() || "copy failed")) : resolve();
2746
+ };
2747
+ reader.on("close", finish);
2748
+ writer.on("close", finish);
2749
+ });
2750
+ }
2751
+ async function snapshotTables(dbUrl, schemas) {
2752
+ const rows = await pgQuery(dbUrl, `
2753
+ SELECT n.nspname || '.' || c.relname AS name, quote_ident(n.nspname) || '.' || quote_ident(c.relname) AS ident,
2754
+ json_agg(json_build_array(a.attname, format_type(a.atttypid, a.atttypmod)) ORDER BY a.attnum) AS columns
2755
+ FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
2756
+ JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped AND a.attgenerated = ''
2757
+ WHERE c.relkind = 'r' AND NOT c.relispartition AND n.nspname = ANY($1::text[])
2758
+ GROUP BY 1, 2 ORDER BY 1`, [schemas]);
2759
+ const out = /* @__PURE__ */ new Map();
2760
+ for (const t of rows) {
2761
+ const hashes = t.columns.map(([col], i) => {
2762
+ const value = `coalesce(md5(x.${quoteIdent(col)}::text), 'n')`;
2763
+ return `coalesce(md5(string_agg(${value}, '' ORDER BY ${value})), '') AS h${i}`;
2764
+ });
2765
+ const [row] = await pgQuery(
2766
+ dbUrl,
2767
+ `SELECT count(*)::text AS rows, pg_relation_filenode(${quoteLiteral(t.ident)}::regclass)::text AS filenode${hashes.length ? ", " + hashes.join(", ") : ""}
2768
+ FROM ${t.ident} x`
2769
+ );
2770
+ out.set(t.name, {
2771
+ rows: Number(row.rows),
2772
+ filenode: row.filenode,
2773
+ columns: Object.fromEntries(t.columns.map(([col, type], i) => [col, { type, hash: row[`h${i}`] }]))
2774
+ });
2775
+ }
2776
+ return out;
2777
+ }
2778
+ function emptiedTables(sql) {
2779
+ const out = /* @__PURE__ */ new Set();
2780
+ for (const s of parseStatements(sql) ?? []) {
2781
+ if (s.kind === "DeleteStmt") out.add(relationName(s.node.relation));
2782
+ if (s.kind === "TruncateStmt") for (const r of s.node.relations ?? []) out.add(relationName(r.RangeVar));
2783
+ }
2784
+ return out;
2785
+ }
2786
+ function compareTables(before, after, sampled, migrationSql) {
2787
+ const emptied = emptiedTables(migrationSql);
2788
+ const lost = [];
2789
+ const rewritten = [];
2790
+ for (const name of sampled) {
2791
+ const b = before.get(name);
2792
+ const a = after.get(name);
2793
+ if (!b || !a) continue;
2794
+ if (b.filenode !== a.filenode) rewritten.push(name);
2795
+ if (emptied.has(name.slice(name.indexOf(".") + 1))) continue;
2796
+ if (a.rows < b.rows) {
2797
+ lost.push(`${name}: ${b.rows} row(s) before, ${a.rows} after`);
2798
+ continue;
2799
+ }
2800
+ if (a.rows !== b.rows) continue;
2801
+ 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);
2802
+ if (changed.length > 0) {
2803
+ lost.push(`${name}: values changed in ${changed.join(", ")}`);
2804
+ }
2805
+ }
2806
+ return { lost, rewritten };
2807
+ }
2808
+
2809
+ // src/prove.ts
2810
+ var exec = promisify2(execFile2);
2811
+ var TARGET_EXTENSIONS_SQL = `
2812
+ SELECT e.extname AS name, n.nspname AS schema
2813
+ FROM pg_extension e
2814
+ JOIN pg_namespace n ON n.oid = e.extnamespace
2815
+ WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
2816
+ AND n.nspname NOT LIKE 'pg\\_%'
2817
+ ORDER BY e.extname
2818
+ `;
2819
+ function withDatabase(dbUrl, database) {
2820
+ const url = new URL(dbUrl);
2821
+ url.pathname = `/${database}`;
2822
+ return url.toString();
2823
+ }
2824
+ async function fingerprint(dbUrl, schemas) {
2825
+ const rows = await pgQuery(dbUrl, fingerprintSql(schemas));
2826
+ const value = rows[0]?.fingerprint;
2827
+ if (value === void 0) {
2828
+ throw new Error('fingerprint query returned no "fingerprint" column');
2829
+ }
2830
+ return value ?? "";
2831
+ }
2832
+ async function describeDifference2(sourceUrl, cloneUrl, schemas, want, got) {
2833
+ try {
2834
+ const [before, after] = await Promise.all([
2835
+ schemaState(sourceUrl, schemas),
2836
+ schemaState(cloneUrl, schemas)
2837
+ ]);
2838
+ const findings = diffState(before, after);
2839
+ if (findings.length > 0) return findings;
2840
+ } catch {
2841
+ }
2842
+ const wanted = new Set(want.split("\n").filter(Boolean));
2843
+ const actual = new Set(got.split("\n").filter(Boolean));
2844
+ return [
2845
+ ...[...wanted].filter((l) => !actual.has(l)).map((l) => `missing: ${l}`),
2846
+ ...[...actual].filter((l) => !wanted.has(l)).map((l) => `unexpected: ${l}`)
2847
+ ];
2848
+ }
2849
+ async function schemaState(dbUrl, schemas) {
2850
+ const rows = await pgQuery(dbUrl, stateSql(schemas));
2851
+ const value = rows[0]?.state;
2852
+ if (!value) throw new Error('state query returned no "state" column');
2853
+ return JSON.parse(value);
2854
+ }
2855
+ async function comparedSchemas(sourceUrl, targetUrl, ignore, query = pgQuery) {
2856
+ const sql = `SELECT n.nspname AS name FROM pg_namespace n
2857
+ WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
2858
+ AND n.nspname NOT LIKE 'pg\\_toast%' AND n.nspname NOT LIKE 'pg\\_temp\\_%'
2859
+ AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.classid = 'pg_namespace'::regclass
2860
+ AND d.objid = n.oid AND d.deptype = 'e')`;
2861
+ const ignored = ignore.map(globToRegExp);
2862
+ const names2 = (await Promise.all([sourceUrl, targetUrl].map((url) => query(url, sql)))).flat().map((r) => String(r.name));
2863
+ return [...new Set(names2)].filter((n) => !ignored.some((g) => g.test(n))).sort();
2864
+ }
2865
+ async function proveConvergence(opts) {
2866
+ const schemas = opts.schemas ?? ["public"];
2867
+ const suffix = randomBytes(4).toString("hex");
2868
+ const cloneName = `supaforge_prove_${suffix}`;
2869
+ const adminUrl = withDatabase(opts.targetUrl, "postgres");
2870
+ const cloneUrl = withDatabase(opts.targetUrl, cloneName);
2871
+ let pgDump;
2872
+ let psql;
2873
+ let serverMajor2;
2874
+ let clientMajor;
2875
+ let sourceMajor;
2876
+ try {
2877
+ serverMajor2 = await getServerMajorVersion(opts.targetUrl);
2878
+ sourceMajor = await getServerMajorVersion(opts.sourceUrl).catch(() => serverMajor2);
2879
+ const resolved = await resolvePgDumpPath(Math.max(serverMajor2, sourceMajor)) ?? await resolvePgDumpPath(serverMajor2);
2880
+ if (!resolved) {
2881
+ return { converged: false, residual: [], cloneName, skipped: await describeMissingPgDump(serverMajor2) };
2882
+ }
2883
+ pgDump = resolved.path;
2884
+ clientMajor = resolved.major;
2885
+ psql = psqlBeside(pgDump);
2886
+ } catch (err) {
2887
+ return {
2888
+ converged: false,
2889
+ residual: [],
2890
+ cloneName,
2891
+ skipped: `could not resolve pg_dump: ${err.message}`
2892
+ };
2893
+ }
2894
+ const absent = await absentRoles(opts.targetUrl, opts.migrationSql);
2895
+ if (absent.length > 0) {
2896
+ return {
2897
+ converged: false,
2898
+ residual: [],
2899
+ cloneName,
2900
+ skipped: `the migration needs role(s) this server does not have yet (${absent.join(", ")}), and creating a role is server-wide, so it cannot be done in a throwaway database`
2901
+ };
2902
+ }
2903
+ let created = false;
2904
+ let sourceCopyCreated = false;
2905
+ let roundTripName;
2906
+ try {
2907
+ try {
2908
+ await pgQuery(adminUrl, `CREATE DATABASE ${quoteName(cloneName)}`);
2909
+ created = true;
2910
+ } catch (err) {
2911
+ return {
2912
+ converged: false,
2913
+ residual: [],
2914
+ cloneName,
2915
+ skipped: `could not create a throwaway database: ${err.message}`
2916
+ };
2917
+ }
2918
+ const tools = { pgDump, psql, clientMajor, serverMajor: serverMajor2 };
2919
+ await copyStructure(opts.targetUrl, cloneUrl, schemas, tools, opts.sourceUrl);
2920
+ const scoped = opts.checks !== void 0 && !opts.checks.includes("schema");
2921
+ const before = scoped ? await schemaState(cloneUrl, schemas) : void 0;
2922
+ const limit = sampleRows();
2923
+ const sample = limit > 0 ? await sampleInto(psql, opts.targetUrl, cloneUrl, schemas, limit) : void 0;
2924
+ const tablesBefore = sample ? await snapshotTables(cloneUrl, schemas) : void 0;
2925
+ let notApplied;
2926
+ if (opts.fixes) {
2927
+ const replayed = await runLikeApply(cloneUrl, opts.fixes, sourceMajor);
2928
+ const unmet = replayed.errors.filter((e) => / — not applied: /.test(e.error));
2929
+ const failed = replayed.errors.filter((e) => !unmet.includes(e));
2930
+ if (failed.length > 0) {
2931
+ return {
2932
+ converged: false,
2933
+ cloneName,
2934
+ residual: failed.map((e) => `fix ${e.issueId} failed: ${e.error}`)
2935
+ };
2936
+ }
2937
+ const byId = new Map(opts.fixes.map((f) => [f.issueId, f.sql]));
2938
+ notApplied = unmet.map((e) => ({ issueId: e.issueId, sql: byId.get(e.issueId) ?? "", reason: e.error }));
2939
+ } else {
2940
+ await runSql(psql, cloneUrl, opts.migrationSql);
2941
+ }
2942
+ let data;
2943
+ if (sample && tablesBefore) {
2944
+ data = { ...sample, ...compareTables(tablesBefore, await snapshotTables(cloneUrl, schemas), sample.sampled, opts.migrationSql) };
2945
+ if (data.lost.length > 0) {
2946
+ return { converged: false, cloneName, data, residual: data.lost.map((l) => `rows lost in ${l}`) };
2947
+ }
2948
+ }
2949
+ const reported = (r) => {
2950
+ const withData = data ? { ...r, data } : r;
2951
+ return notApplied?.length ? { ...withData, notApplied } : withData;
2952
+ };
2953
+ const [want, got] = await Promise.all([
2954
+ fingerprint(opts.sourceUrl, schemas),
2955
+ fingerprint(cloneUrl, schemas)
2956
+ ]);
2957
+ if (canonicalArrays(want) === canonicalArrays(got)) return reported({ converged: true, residual: [], cloneName });
2958
+ roundTripName = `${cloneName}_src`;
2959
+ const roundTripUrl = withDatabase(opts.targetUrl, roundTripName);
2960
+ const roundTrip = await copyOfSource(adminUrl, opts.sourceUrl, roundTripUrl, roundTripName, schemas, tools);
2961
+ if (roundTrip !== null) {
2962
+ sourceCopyCreated = true;
2963
+ if (roundTrip.ok) {
2964
+ const wantRoundTripped = await fingerprint(roundTripUrl, schemas);
2965
+ if (canonicalArrays(wantRoundTripped) === canonicalArrays(got)) return reported({ converged: true, residual: [], cloneName });
2966
+ const residual2 = await describeDifference2(roundTripUrl, cloneUrl, schemas, wantRoundTripped, got);
2967
+ return reported(withinScope(residual2, before && diffState(await schemaState(roundTripUrl, schemas), before), opts.checks, cloneName));
2968
+ }
2969
+ }
2970
+ if (sourceMajor !== serverMajor2) {
2971
+ return {
2972
+ converged: false,
2973
+ residual: [],
2974
+ cloneName,
2975
+ skipped: `no pg_dump ${sourceMajor} or later to copy the PostgreSQL ${sourceMajor} source onto the PostgreSQL ${serverMajor2} target's server, which comparing across the two versions needs`
2976
+ };
2977
+ }
2978
+ const residual = await describeDifference2(opts.sourceUrl, cloneUrl, schemas, want, got);
2979
+ return reported(withinScope(residual, before && diffState(await schemaState(opts.sourceUrl, schemas), before), opts.checks, cloneName));
2980
+ } finally {
2981
+ if (sourceCopyCreated && roundTripName) {
2982
+ await dropDatabase(adminUrl, roundTripName);
2983
+ }
2984
+ if (created) {
2985
+ await dropDatabase(adminUrl, cloneName);
2986
+ }
2987
+ }
2988
+ }
2989
+ async function copyStructure(fromUrl, intoUrl, schemas, tools, migratingFrom) {
2990
+ await prepareClone(intoUrl, fromUrl, schemas);
2991
+ const supporting = await supportingSchemas(fromUrl, schemas);
2992
+ const present = new Set((await pgQuery(fromUrl, "SELECT nspname FROM pg_namespace")).map((r) => r.nspname));
2993
+ if (migratingFrom) {
2994
+ for (const name of await supportingSchemas(migratingFrom, schemas)) {
2995
+ if (present.has(name) && !supporting.includes(name)) supporting.push(name);
2996
+ }
2997
+ }
2998
+ const dumpArgs = [
2999
+ fromUrl,
3000
+ "--schema-only",
3001
+ "--no-owner",
3002
+ "--no-privileges",
3003
+ ...[...schemas.filter((s) => present.has(s)), ...supporting].map((s) => `--schema=${quoteName(s)}`)
3004
+ ];
3005
+ const { stdout: structure } = await exec(tools.pgDump, dumpArgs, {
3006
+ maxBuffer: 256 * 1024 * 1024,
3007
+ timeout: 3e5
3008
+ });
3009
+ const { sql: replayable } = dropUnsupportedSetStatements(
3010
+ tolerateExistingSchemas(structure),
3011
+ await knownParameters(intoUrl)
3012
+ );
3013
+ try {
3014
+ await runSql(tools.psql, intoUrl, replayable);
3015
+ } catch (err) {
3016
+ throw new Error(explainStructureFailure(err.message, tools.clientMajor, tools.serverMajor));
3017
+ }
3018
+ }
3019
+ async function supportingSchemas(dbUrl, schemas, queryFn = pgQuery) {
3020
+ const known = new Set(schemas);
3021
+ const found = [];
3022
+ for (let from = [...schemas]; from.length > 0; ) {
3023
+ const rows = await queryFn(dbUrl, SUPPORTING_SCHEMAS_SQL(from));
3024
+ from = rows.map((r) => r.schema).filter((s) => !known.has(s));
3025
+ for (const schema of from) {
3026
+ known.add(schema);
3027
+ found.push(schema);
3028
+ }
3029
+ }
3030
+ return found;
3031
+ }
3032
+ var NAMESPACE_OF = (cls, oid) => `CASE ${cls}
3033
+ WHEN 'pg_class'::regclass THEN (SELECT relnamespace FROM pg_class WHERE oid = ${oid})
3034
+ WHEN 'pg_proc'::regclass THEN (SELECT pronamespace FROM pg_proc WHERE oid = ${oid})
3035
+ WHEN 'pg_type'::regclass THEN (SELECT typnamespace FROM pg_type WHERE oid = ${oid})
3036
+ WHEN 'pg_constraint'::regclass THEN (SELECT connamespace FROM pg_constraint WHERE oid = ${oid})
3037
+ WHEN 'pg_attrdef'::regclass THEN (SELECT x_c.relnamespace FROM pg_attrdef x_d JOIN pg_class x_c ON x_c.oid = x_d.adrelid WHERE x_d.oid = ${oid})
3038
+ WHEN 'pg_policy'::regclass THEN (SELECT x_c.relnamespace FROM pg_policy x_p JOIN pg_class x_c ON x_c.oid = x_p.polrelid WHERE x_p.oid = ${oid})
3039
+ WHEN 'pg_trigger'::regclass THEN (SELECT x_c.relnamespace FROM pg_trigger x_t JOIN pg_class x_c ON x_c.oid = x_t.tgrelid WHERE x_t.oid = ${oid})
3040
+ WHEN 'pg_rewrite'::regclass THEN (SELECT x_c.relnamespace FROM pg_rewrite x_r JOIN pg_class x_c ON x_c.oid = x_r.ev_class WHERE x_r.oid = ${oid})
3041
+ END`;
3042
+ var SUPPORTING_SCHEMAS_SQL = (from) => `
3043
+ SELECT DISTINCT rn.nspname AS schema
3044
+ FROM pg_depend d
3045
+ JOIN pg_namespace n ON n.oid = ${NAMESPACE_OF("d.classid", "d.objid")}
3046
+ JOIN pg_namespace rn ON rn.oid = ${NAMESPACE_OF("d.refclassid", "d.refobjid")}
3047
+ WHERE n.nspname IN (${from.map(quoteLiteral2).join(", ")})
3048
+ AND rn.nspname <> n.nspname
3049
+ AND rn.nspname NOT IN ('pg_catalog', 'information_schema')
3050
+ AND rn.nspname NOT LIKE 'pg\\_%'
3051
+ AND d.deptype IN ('n', 'a')
3052
+ AND NOT EXISTS (SELECT 1 FROM pg_depend e
3053
+ WHERE e.classid = d.refclassid AND e.objid = d.refobjid AND e.deptype = 'e')
3054
+ ORDER BY 1`;
3055
+ function quoteLiteral2(value) {
3056
+ return `'${value.replace(/'/g, "''")}'`;
3057
+ }
3058
+ function tolerateExistingSchemas(sql) {
3059
+ return sql.replace(/^CREATE SCHEMA (?!IF NOT EXISTS )/gm, "CREATE SCHEMA IF NOT EXISTS ");
3060
+ }
3061
+ async function copyOfSource(adminUrl, sourceUrl, intoUrl, name, schemas, tools) {
3062
+ try {
3063
+ await pgQuery(adminUrl, `CREATE DATABASE ${quoteName(name)}`);
3064
+ } catch {
3065
+ return null;
3066
+ }
3067
+ try {
3068
+ await copyStructure(sourceUrl, intoUrl, schemas, tools);
3069
+ return { ok: true };
3070
+ } catch {
3071
+ return { ok: false };
3072
+ }
3073
+ }
3074
+ async function dropDatabase(adminUrl, name) {
3075
+ await pgQuery(
3076
+ adminUrl,
3077
+ `SELECT pg_terminate_backend(pid) FROM pg_stat_activity
3078
+ WHERE datname = '${name}' AND pid <> pg_backend_pid()`
3079
+ ).catch(() => void 0);
3080
+ await pgQuery(adminUrl, `DROP DATABASE IF EXISTS ${quoteName(name)}`).catch(() => void 0);
3081
+ }
3082
+ function runSql(psqlPath, dbUrl, sql) {
3083
+ const trimmed = sql.trim();
3084
+ if (!trimmed) return Promise.resolve();
3085
+ return new Promise((resolve, reject) => {
3086
+ const child = spawn2(psqlPath, [dbUrl, "-v", "ON_ERROR_STOP=1", "-q", "-f", "-"], {
3087
+ stdio: ["pipe", "pipe", "pipe"]
3088
+ });
3089
+ let stderr = "";
3090
+ child.stderr.on("data", (d) => {
3091
+ stderr += d.toString();
3092
+ });
3093
+ child.on("error", reject);
3094
+ child.on("close", (code) => {
3095
+ if (code === 0) resolve();
3096
+ else reject(new Error(stderr.trim() || `psql exited ${code}`));
3097
+ });
3098
+ child.stdin.write(trimmed);
3099
+ child.stdin.end();
3100
+ });
3101
+ }
3102
+ function psqlBeside(pgDumpPath) {
3103
+ return pgDumpPath === "pg_dump" ? "psql" : join2(dirname(pgDumpPath), "psql");
3104
+ }
3105
+ var PREAMBLE_LINE = /^\s*(?:--|$|\\(?:un)?restrict\b|SET\s+[\w.]+\s*=|SELECT\s+pg_catalog\.set_config\s*\()/i;
3106
+ var SET_STATEMENT = /^\s*SET\s+([\w.]+)\s*=/i;
3107
+ function dropUnsupportedSetStatements(sql, knownParameters2) {
3108
+ const lines = sql.split("\n");
3109
+ const dropped = [];
3110
+ const kept = [];
3111
+ let inPreamble = true;
3112
+ for (const line of lines) {
3113
+ if (inPreamble && !PREAMBLE_LINE.test(line)) inPreamble = false;
3114
+ if (inPreamble) {
3115
+ const name = SET_STATEMENT.exec(line)?.[1];
3116
+ if (name && !knownParameters2.has(name.toLowerCase())) {
3117
+ dropped.push(name.toLowerCase());
3118
+ continue;
3119
+ }
3120
+ }
3121
+ kept.push(line);
3122
+ }
3123
+ return { sql: kept.join("\n"), dropped };
3124
+ }
3125
+ async function prepareClone(cloneUrl, targetUrl, schemas, queryFn = pgQuery) {
3126
+ for (const schema of schemas) {
3127
+ await queryFn(cloneUrl, `DROP SCHEMA IF EXISTS ${quoteName(schema)} CASCADE`);
3128
+ }
3129
+ const extensions = await queryFn(targetUrl, TARGET_EXTENSIONS_SQL);
3130
+ for (const ext of extensions) {
3131
+ await queryFn(cloneUrl, `CREATE SCHEMA IF NOT EXISTS ${quoteName(ext.schema)}`).catch(() => void 0);
3132
+ await queryFn(
3133
+ cloneUrl,
3134
+ `CREATE EXTENSION IF NOT EXISTS ${quoteName(ext.name)} WITH SCHEMA ${quoteName(ext.schema)}`
3135
+ ).catch(() => void 0);
3136
+ }
3137
+ }
3138
+ async function knownParameters(dbUrl) {
3139
+ const rows = await pgQuery(dbUrl, "SELECT name FROM pg_settings");
3140
+ return new Set(rows.map((r) => r.name.toLowerCase()));
3141
+ }
3142
+ function explainStructureFailure(message, clientMajor, serverMajor2) {
3143
+ const looksLikeVersionGap = /unrecognized configuration parameter|syntax error/i.test(message);
3144
+ const versionGap = clientMajor > serverMajor2 && looksLikeVersionGap ? ` The local client is PostgreSQL ${clientMajor} and the target server is ${serverMajor2}; pg_dump ${clientMajor} can emit SQL that a ${serverMajor2} server rejects. Installing postgresql-client-${serverMajor2} removes the gap.` : "";
3145
+ return `could not replay the target's structure onto the clone: ${message}${versionGap}`;
3146
+ }
3147
+ 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;
3148
+ function identParts(qualified) {
3149
+ return [...qualified.matchAll(/"((?:[^"]|"")+)"|([^."]+)/g)].map((m) => (m[1]?.replace(/""/g, '"') ?? m[2]).toLowerCase());
3150
+ }
3151
+ 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;
3152
+ function residualHeldBack(residual, heldBackSql) {
3153
+ const dropped = /* @__PURE__ */ new Set();
3154
+ for (const sql of heldBackSql) {
3155
+ for (const m of sql.matchAll(DROPPED_NAME)) dropped.add(m[1].replace(/^"|"$/g, "").toLowerCase());
3156
+ }
3157
+ const created = [];
3158
+ for (const sql of heldBackSql) {
3159
+ for (const m of sql.matchAll(CREATED_NAME)) created.push(m[1].replace(/^"|"$/g, "").toLowerCase());
3160
+ }
3161
+ const heldBack = [];
3162
+ const unexplained = [];
3163
+ for (const line of residual) {
3164
+ const [, subject, owner] = /^[a-z ]+?\s([^\s:]+(?:\s[^\s:]+)?):\s*unexpected(?: \(owned by (.+)\))?$/i.exec(line) ?? [];
3165
+ const name = subject?.split(/[.\s]/).pop()?.replace(/^"|"$/g, "").toLowerCase();
3166
+ const owners = owner ? identParts(owner) : [];
3167
+ const missing = /^(.*):\s*missing$/i.exec(line)?.[1].toLowerCase();
3168
+ if (name && dropped.has(name) || owners.some((o) => dropped.has(o))) heldBack.push(line);
3169
+ else if (missing && created.some((n) => missing.endsWith(` ${n}`) || missing.endsWith(`.${n}`))) heldBack.push(line);
3170
+ else unexplained.push(line);
3171
+ }
3172
+ return { heldBack, unexplained };
3173
+ }
3174
+ var PROVED_CHECKS = /* @__PURE__ */ new Set(["schema", "rls", "rls-coverage", "extensions", "webhooks"]);
3175
+ var SERVER_WIDE = [
3176
+ /^\s*(?:CREATE|ALTER|DROP)\s+(?:ROLE|USER|GROUP|DATABASE|TABLESPACE)\b/i,
3177
+ /^\s*ALTER\s+SYSTEM\b/i,
3178
+ // Role membership: a GRANT or REVOKE with no ON names roles, not objects.
3179
+ /^\s*(?:GRANT|REVOKE)\b(?![\s\S]*\bON\b)/i
3180
+ ];
3181
+ function proofScope(statements) {
3182
+ const replay = [];
3183
+ const unproved = [];
3184
+ for (const stmt of statements) {
3185
+ const serverWide = splitSqlStatements(stmt.sql).filter((s) => !isCommentOnly(s)).some((s) => SERVER_WIDE.some((re) => re.test(sqlSkeleton(s))));
3186
+ if (PROVED_CHECKS.has(stmt.check) && !serverWide) replay.push(stmt);
3187
+ else unproved.push(stmt);
3188
+ }
3189
+ return { replay, unproved };
3190
+ }
3191
+ async function absentRoles(dbUrl, sql) {
3192
+ const named2 = [...new Set(splitSqlStatements(sql).flatMap((s) => rolesNamedBy(s)))];
3193
+ if (named2.length === 0) return [];
3194
+ const rows = await pgQuery(dbUrl, "SELECT rolname FROM pg_roles WHERE rolname = ANY($1::text[])", [named2]);
3195
+ const present = new Set(rows.map((r) => r.rolname));
3196
+ return named2.filter((r) => !present.has(r));
3197
+ }
3198
+ function withinScope(residual, baseline, checks, cloneName) {
3199
+ if (!baseline || !checks) return { converged: residual.length === 0, residual, cloneName };
3200
+ const had = new Set(baseline);
3201
+ const outOfScope = residual.filter((l) => had.has(l) && outsideTheRun(l, checks));
3202
+ const remaining = residual.filter((l) => !outOfScope.includes(l));
3203
+ return { converged: remaining.length === 0, residual: remaining, outOfScope, cloneName };
3204
+ }
3205
+ var GOVERNED = {
3206
+ "rls": /^policy on |: RLS (?:enabled|forced) /,
3207
+ "rls-coverage": /: RLS enabled /,
3208
+ "extensions": /^extension /,
3209
+ "webhooks": /^trigger on /
3210
+ };
3211
+ function outsideTheRun(line, checks) {
3212
+ if (checks.includes("schema")) return false;
3213
+ return !checks.some((c) => GOVERNED[c]?.test(line));
3214
+ }
3215
+
2478
3216
  export {
2479
3217
  parseStatements,
2480
3218
  relationName,
@@ -2504,7 +3242,17 @@ export {
2504
3242
  readLockBudget,
2505
3243
  lockBudgetWarning,
2506
3244
  planWork,
2507
- runLikeApply,
2508
- promote
3245
+ promote,
3246
+ withDatabase,
3247
+ fingerprint,
3248
+ comparedSchemas,
3249
+ proveConvergence,
3250
+ copyStructure,
3251
+ dropDatabase,
3252
+ psqlBeside,
3253
+ dropUnsupportedSetStatements,
3254
+ knownParameters,
3255
+ residualHeldBack,
3256
+ proofScope
2509
3257
  };
2510
- //# sourceMappingURL=chunk-YUJ2MHLM.js.map
3258
+ //# sourceMappingURL=chunk-2HJPUDPP.js.map