@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
@@ -1,19 +1,23 @@
1
+ import {
2
+ RolesCheck
3
+ } from "./chunk-WNTCZM2J.js";
1
4
  import {
2
5
  CHECK_NAMES,
3
- RolesCheck,
4
6
  applyTableFilter,
7
+ destructiveReason,
5
8
  isComparisonCheck,
6
- resolveTableFilter
7
- } from "./chunk-UWAVXC57.js";
8
- import {
9
+ parseStatements,
9
10
  policyOnlyKeys,
11
+ relationName,
12
+ resolveTableFilter,
10
13
  runDbDiff,
14
+ schemaNotices,
11
15
  sqlSkeleton,
12
16
  sqlToIssues
13
- } from "./chunk-BUOWHE36.js";
17
+ } from "./chunk-T7GMPGUO.js";
14
18
  import {
15
19
  splitSqlStatements
16
- } from "./chunk-G2DIPIYU.js";
20
+ } from "./chunk-WK5YTINL.js";
17
21
  import {
18
22
  RealtimeCheck,
19
23
  createPolicySql,
@@ -21,13 +25,15 @@ import {
21
25
  diffSchemaPolicies,
22
26
  dropEquivalentPolicyChanges,
23
27
  dropPolicySql,
28
+ httpStatus,
29
+ readJsonArray,
30
+ readJsonObject,
24
31
  schemaPolicySql
25
- } from "./chunk-MIRLB5JM.js";
32
+ } from "./chunk-SM7HP6EP.js";
26
33
  import {
27
34
  Check,
28
35
  CheckSkipped,
29
36
  MigrationsCheck,
30
- friendlyDbError,
31
37
  isCheckSkipped,
32
38
  pgQuery,
33
39
  quoteIdent,
@@ -35,7 +41,7 @@ import {
35
41
  quoteName,
36
42
  readOrAbsent,
37
43
  sqlLiteral
38
- } from "./chunk-SQPCWGQO.js";
44
+ } from "./chunk-4M6OQLPO.js";
39
45
  import {
40
46
  DEFAULT_IGNORE_SCHEMAS,
41
47
  SCORE_MAX,
@@ -44,8 +50,9 @@ import {
44
50
  SCORE_PENALTY_INFO,
45
51
  SCORE_PENALTY_WARNING,
46
52
  STORAGE_LIST_LIMIT,
47
- SUPABASE_MGMT_API
48
- } from "./chunk-6WAMNZLO.js";
53
+ SUPABASE_MGMT_API,
54
+ friendlyDbError
55
+ } from "./chunk-KNXHLVF7.js";
49
56
 
50
57
  // src/scoring.ts
51
58
  function summarize(results) {
@@ -85,6 +92,111 @@ function computePostureScore(results) {
85
92
  return scoreOf(posture);
86
93
  }
87
94
 
95
+ // src/hazards.ts
96
+ var VOLATILE = /* @__PURE__ */ new Set(["random", "gen_random_uuid", "uuid_generate_v4", "uuid_generate_v1", "clock_timestamp", "nextval", "timeofday"]);
97
+ var table = (rv) => rv ? rv.schemaname ? `${rv.schemaname}.${relationName(rv)}` : relationName(rv) : "";
98
+ function hazardsOf(sql) {
99
+ const out = [];
100
+ const add = (h) => {
101
+ if (!out.some((o) => o.kind === h.kind && o.on === h.on)) out.push(h);
102
+ };
103
+ const loss = destructiveReason(sql);
104
+ if (loss) add({ kind: "data-loss", detail: loss });
105
+ const statements = parseStatements(sql);
106
+ if (!statements) return out;
107
+ const created = new Set(statements.filter((s) => s.kind === "CreateStmt").map((s) => table(s.node.relation)));
108
+ const existing = (name) => name !== "" && !created.has(name);
109
+ for (const s of statements) {
110
+ for (const h of statementHazards(s)) {
111
+ if (h.on === void 0 || existing(h.on)) add(h);
112
+ }
113
+ }
114
+ return out;
115
+ }
116
+ function statementHazards(s) {
117
+ const n = s.node;
118
+ switch (s.kind) {
119
+ case "AlterTableStmt":
120
+ return alterTableHazards(table(n.relation), n.cmds ?? []);
121
+ case "IndexStmt":
122
+ return n.concurrent ? [{ kind: "not-transactional", on: table(n.relation), detail: `builds an index concurrently, which cannot run inside a transaction` }] : [{ kind: "blocks-writes", on: table(n.relation), detail: `blocks writes to ${table(n.relation)} while the index builds` }];
123
+ case "CreateTrigStmt":
124
+ return [{ kind: "blocks-writes", on: table(n.relation), detail: `blocks writes to ${table(n.relation)} briefly` }];
125
+ case "CreatePolicyStmt":
126
+ case "AlterPolicyStmt":
127
+ case "RuleStmt":
128
+ return [{ kind: "blocks-reads-and-writes", on: table(n.table ?? n.relation), detail: `blocks reads and writes on ${table(n.table ?? n.relation)} briefly` }];
129
+ case "RenameStmt":
130
+ return n.relation ? [{ kind: "blocks-reads-and-writes", on: table(n.relation), detail: `blocks reads and writes on ${table(n.relation)} briefly` }] : [];
131
+ case "RefreshMatViewStmt":
132
+ return n.concurrent ? [] : [{ kind: "blocks-reads-and-writes", on: table(n.relation), detail: `blocks reads of ${table(n.relation)} while it refreshes` }];
133
+ case "VacuumStmt":
134
+ return [{ kind: "not-transactional", detail: "VACUUM cannot run inside a transaction" }];
135
+ case "ReindexStmt":
136
+ return (n.params ?? []).some((p) => p.DefElem?.defname === "concurrently") ? [{ kind: "not-transactional", detail: "REINDEX CONCURRENTLY cannot run inside a transaction" }] : [];
137
+ default:
138
+ return [];
139
+ }
140
+ }
141
+ function alterTableHazards(name, cmds) {
142
+ const out = [];
143
+ let exclusive = false;
144
+ let writesOnly = false;
145
+ for (const { AlterTableCmd: cmd } of cmds) {
146
+ if (!cmd) continue;
147
+ switch (cmd.subtype) {
148
+ // Lighter locks: other DDL waits, reads and writes go on.
149
+ case "AT_SetStatistics":
150
+ case "AT_SetOptions":
151
+ case "AT_ResetOptions":
152
+ case "AT_SetRelOptions":
153
+ case "AT_ResetRelOptions":
154
+ case "AT_ValidateConstraint":
155
+ case "AT_ClusterOn":
156
+ case "AT_DropCluster":
157
+ continue;
158
+ case "AT_AlterColumnType":
159
+ out.push({ kind: "rewrite", on: name, detail: `may rewrite ${name}: a column's type changes, unless the new type is binary-compatible` });
160
+ break;
161
+ case "AT_SetLogged":
162
+ case "AT_SetUnLogged":
163
+ out.push({ kind: "rewrite", on: name, detail: `rewrites ${name}: LOGGED and UNLOGGED tables are stored differently` });
164
+ break;
165
+ case "AT_AddColumn": {
166
+ const def = cmd.def?.ColumnDef;
167
+ const constraints = def?.constraints ?? [];
168
+ const generated = constraints.some((c) => c.Constraint?.contype === "CONSTR_GENERATED");
169
+ const volatile = constraints.some((c) => c.Constraint?.contype === "CONSTR_DEFAULT" && VOLATILE.has(String(c.Constraint.raw_expr?.FuncCall?.funcname?.at(-1)?.String?.sval ?? "")));
170
+ if (generated || volatile) {
171
+ out.push({ kind: "rewrite", on: name, detail: `rewrites ${name}: the new column's ${generated ? "generated value" : "default"} is computed for every row` });
172
+ }
173
+ break;
174
+ }
175
+ case "AT_AddConstraint": {
176
+ const c = cmd.def?.Constraint;
177
+ if (c && !c.skip_validation && (c.contype === "CONSTR_CHECK" || c.contype === "CONSTR_FOREIGN")) {
178
+ out.push({ kind: "full-scan", on: name, detail: `scans every row of ${name} to check the new constraint (NOT VALID, then VALIDATE, avoids holding the lock)` });
179
+ }
180
+ if (c?.contype === "CONSTR_FOREIGN") {
181
+ writesOnly = true;
182
+ continue;
183
+ }
184
+ break;
185
+ }
186
+ case "AT_SetNotNull":
187
+ out.push({ kind: "full-scan", on: name, detail: `scans every row of ${name} to check for NULLs` });
188
+ break;
189
+ }
190
+ exclusive = true;
191
+ }
192
+ if (exclusive) {
193
+ out.unshift({ kind: "blocks-reads-and-writes", on: name, detail: `blocks reads and writes on ${name} while it runs` });
194
+ } else if (writesOnly) {
195
+ out.unshift({ kind: "blocks-writes", on: name, detail: `blocks writes to ${name} while the key is checked` });
196
+ }
197
+ return out;
198
+ }
199
+
88
200
  // src/scanner.ts
89
201
  function resolveExcludedChecks(config) {
90
202
  const lists = [config?.checks?.exclude, config?.environments?.[config?.target ?? ""]?.checks?.exclude];
@@ -107,14 +219,23 @@ function checkConcurrency() {
107
219
  }
108
220
  return 4;
109
221
  }
222
+ function withHazards(issues) {
223
+ return issues.map((issue) => {
224
+ if (!issue.sql?.up) return issue;
225
+ const hazards = hazardsOf(issue.sql.up);
226
+ return hazards.length > 0 ? { ...issue, hazards } : issue;
227
+ });
228
+ }
110
229
  async function runCheck(check, ctx, name, sourceDbUrl) {
111
230
  const start = performance.now();
231
+ const notices = [];
112
232
  try {
113
- const issues = await check.scan(ctx);
233
+ const issues = withHazards(await check.scan({ ...ctx, onNotice: (n) => notices.push(n) }));
114
234
  return {
115
235
  check: name,
116
236
  status: issues.length > 0 ? "drifted" : "clean",
117
237
  issues,
238
+ ...notices.length > 0 ? { notices } : {},
118
239
  durationMs: Math.round(performance.now() - start)
119
240
  };
120
241
  } catch (err) {
@@ -262,7 +383,7 @@ var SchemaCheck = class extends Check {
262
383
  type: "schema",
263
384
  include: "both",
264
385
  timeoutSeconds: ctx.target?.checks?.schema?.timeout,
265
- onProgress: ctx.onDetail ? ({ table, tablesSeen }) => ctx.onDetail?.(`${tablesSeen} tables \xB7 ${table}`) : void 0,
386
+ onProgress: ctx.onDetail ? ({ table: table2, tablesSeen }) => ctx.onDetail?.(`${tablesSeen} tables \xB7 ${table2}`) : void 0,
266
387
  ignoreSchemas,
267
388
  // dbdiff takes the include/exclude lists natively, globs and all, so
268
389
  // the scope is enforced in the diff itself rather than by discarding
@@ -272,6 +393,7 @@ var SchemaCheck = class extends Check {
272
393
  }),
273
394
  this.fetchIgnoredSchemaTables(ctx.target.dbUrl, ignoreSchemas)
274
395
  ]);
396
+ for (const notice of schemaNotices(result.notices ?? [])) ctx.onNotice?.(notice);
275
397
  return sqlToIssues(result, "schema", ignoreSchemas, ignoredSchemaTables);
276
398
  } catch (err) {
277
399
  if (err instanceof Error && err.message.includes("@dbdiff/cli is not installed")) {
@@ -625,7 +747,7 @@ async function inventoryFunctionsViaStudio(studioUrl, ref, fetchFn) {
625
747
  if (!listRes.ok) {
626
748
  throw new Error(`Studio functions API returned ${listRes.status} for ${studioUrl}`);
627
749
  }
628
- const listed = await listRes.json();
750
+ const listed = await readJsonArray(listRes, `The Studio functions API at ${studioUrl}`);
629
751
  const out = [];
630
752
  for (const fn of listed) {
631
753
  if (!fn?.slug) continue;
@@ -698,8 +820,8 @@ var EdgeFunctionsCheck = class extends Check {
698
820
  const res = await this.fetchFn(url, {
699
821
  headers: { Authorization: `Bearer ${accessToken}` }
700
822
  });
701
- if (!res.ok) throw new Error(`Failed to list functions for ${projectRef}: ${res.statusText}`);
702
- return res.json();
823
+ if (!res.ok) throw new Error(`Failed to list functions for ${projectRef}: HTTP ${httpStatus(res)}`);
824
+ return readJsonArray(res, `The Edge Function list of ${projectRef}`);
703
825
  }
704
826
  };
705
827
  function makeDeleteAction(slug, targetRef, targetKey) {
@@ -1028,8 +1150,8 @@ var StorageCheck = class extends Check {
1028
1150
  }
1029
1151
  return issues;
1030
1152
  }
1031
- async hasTable(dbUrl, table) {
1032
- const rows = await this.queryFn(dbUrl, tableExistsSql(table));
1153
+ async hasTable(dbUrl, table2) {
1154
+ const rows = await this.queryFn(dbUrl, tableExistsSql(table2));
1033
1155
  return rows[0]?.present === true;
1034
1156
  }
1035
1157
  async listTypedBuckets(dbUrl, spec) {
@@ -1134,11 +1256,11 @@ var TYPED_BUCKET_TABLES = [
1134
1256
  sql: `SELECT id AS key FROM storage.buckets_vectors ORDER BY id`
1135
1257
  }
1136
1258
  ];
1137
- function tableExistsSql(table) {
1259
+ function tableExistsSql(table2) {
1138
1260
  return `
1139
1261
  SELECT EXISTS (
1140
1262
  SELECT 1 FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
1141
- WHERE n.nspname = 'storage' AND c.relname = '${table}'
1263
+ WHERE n.nspname = 'storage' AND c.relname = '${table2}'
1142
1264
  ) AS present
1143
1265
  `;
1144
1266
  }
@@ -1472,8 +1594,8 @@ var AuthCheck = class extends Check {
1472
1594
  const res = await this.fetchFn(url, {
1473
1595
  headers: { Authorization: `Bearer ${accessToken}` }
1474
1596
  });
1475
- if (!res.ok) throw new Error(`Failed to fetch auth config for ${projectRef}: ${res.statusText}`);
1476
- return res.json();
1597
+ if (!res.ok) throw new Error(`Failed to fetch auth config for ${projectRef}: HTTP ${httpStatus(res)}`);
1598
+ return readJsonObject(res, `The auth config of ${projectRef}`);
1477
1599
  }
1478
1600
  /**
1479
1601
  * GoTrue wants the service-role key in both `apikey` and `Authorization`.
@@ -1484,8 +1606,8 @@ var AuthCheck = class extends Check {
1484
1606
  const res = await this.fetchFn(url, {
1485
1607
  headers: { apikey: serviceKey, Authorization: `Bearer ${serviceKey}` }
1486
1608
  });
1487
- if (!res.ok) throw new Error(`Failed to fetch auth settings from ${apiUrl}: ${res.statusText}`);
1488
- const raw = await res.json();
1609
+ if (!res.ok) throw new Error(`Failed to fetch auth settings from ${apiUrl}: HTTP ${httpStatus(res)}`);
1610
+ const raw = await readJsonObject(res, `The auth settings at ${apiUrl}`);
1489
1611
  return normalizeGoTrueSettings(raw);
1490
1612
  }
1491
1613
  };
@@ -1496,14 +1618,14 @@ function diffAuthConfig(source, target, targetSrc) {
1496
1618
  const sv = source[key];
1497
1619
  const tv = target[key];
1498
1620
  if (JSON.stringify(sv) === JSON.stringify(tv)) continue;
1499
- const kind = classifyAuthKey(key);
1621
+ const kind2 = classifyAuthKey(key);
1500
1622
  const base2 = {
1501
1623
  id: `auth-${key.toLowerCase()}`,
1502
1624
  check: "auth",
1503
1625
  severity: isCriticalAuthKey(key) ? "critical" : "info",
1504
1626
  title: `Auth config mismatch: ${key}`
1505
1627
  };
1506
- if (kind === "secret") {
1628
+ if (kind2 === "secret") {
1507
1629
  issues.push({
1508
1630
  ...base2,
1509
1631
  description: `"${key}" differs between source and target. It is a secret: set it on the target directly.`,
@@ -1511,14 +1633,14 @@ function diffAuthConfig(source, target, targetSrc) {
1511
1633
  });
1512
1634
  continue;
1513
1635
  }
1514
- const action = targetSrc.kind === "hosted" && kind === "shared" ? {
1636
+ const action = targetSrc.kind === "hosted" && kind2 === "shared" ? {
1515
1637
  method: "PATCH",
1516
1638
  url: `${SUPABASE_MGMT_API}/${encodeURIComponent(targetSrc.ref)}/config/auth`,
1517
1639
  headers: { Authorization: `Bearer ${targetSrc.token}` },
1518
1640
  body: { [key]: sv },
1519
1641
  label: `Set auth config "${key}" to ${JSON.stringify(sv)} in target`
1520
1642
  } : void 0;
1521
- const manualOnly = kind === "environment" ? `"${key}" belongs to each environment and is not copied. Change it on the target if it is wrong there.` : action ? void 0 : "Self-hosted GoTrue is configured through its environment \u2014 update the target deployment and restart it.";
1643
+ const manualOnly = kind2 === "environment" ? `"${key}" belongs to each environment and is not copied. Change it on the target if it is wrong there.` : action ? void 0 : "Self-hosted GoTrue is configured through its environment \u2014 update the target deployment and restart it.";
1522
1644
  issues.push({
1523
1645
  ...base2,
1524
1646
  description: `"${key}" differs between source (${JSON.stringify(sv)}) and target (${JSON.stringify(tv)}).` + (manualOnly ? ` ${manualOnly}` : ""),
@@ -1635,25 +1757,25 @@ SELECT cron.schedule('${key}', '${tj.schedule}', $$${tj.command.trim()}$$);`
1635
1757
  }
1636
1758
 
1637
1759
  // src/checksum.ts
1638
- async function getTableFingerprint(dbUrl, table, queryFn = pgQuery) {
1760
+ async function getTableFingerprint(dbUrl, table2, queryFn = pgQuery) {
1639
1761
  const sql = `
1640
1762
  SELECT count(*)::int AS row_count,
1641
1763
  coalesce(sum(('x' || substr(d, 1, 16))::bit(64)::bigint::numeric), 0)::text
1642
1764
  || ':' ||
1643
1765
  coalesce(sum(('x' || substr(d, 17, 16))::bit(64)::bigint::numeric), 0)::text AS content
1644
- FROM (SELECT md5(t::text) AS d FROM ${quoteIdent(table)} t) s
1766
+ FROM (SELECT md5(t::text) AS d FROM ${quoteIdent(table2)} t) s
1645
1767
  `;
1646
1768
  const [row] = await queryFn(dbUrl, sql);
1647
1769
  return {
1648
- table,
1770
+ table: table2,
1649
1771
  rowCount: row.row_count,
1650
1772
  content: row.content
1651
1773
  };
1652
1774
  }
1653
- async function tablesMatch(sourceUrl, targetUrl, table, queryFn = pgQuery) {
1775
+ async function tablesMatch(sourceUrl, targetUrl, table2, queryFn = pgQuery) {
1654
1776
  const [source, target] = await Promise.all([
1655
- getTableFingerprint(sourceUrl, table, queryFn),
1656
- getTableFingerprint(targetUrl, table, queryFn)
1777
+ getTableFingerprint(sourceUrl, table2, queryFn),
1778
+ getTableFingerprint(targetUrl, table2, queryFn)
1657
1779
  ]);
1658
1780
  return source.rowCount === target.rowCount && source.content === target.content;
1659
1781
  }
@@ -1661,20 +1783,20 @@ async function filterChangedTables(sourceUrl, targetUrl, tables, queryFn = pgQue
1661
1783
  const changed = [];
1662
1784
  const skipped = [];
1663
1785
  const results = await Promise.all(
1664
- tables.map(async (table) => {
1786
+ tables.map(async (table2) => {
1665
1787
  try {
1666
- const match = await tablesMatch(sourceUrl, targetUrl, table, queryFn);
1667
- return { table, match };
1788
+ const match = await tablesMatch(sourceUrl, targetUrl, table2, queryFn);
1789
+ return { table: table2, match };
1668
1790
  } catch {
1669
- return { table, match: false };
1791
+ return { table: table2, match: false };
1670
1792
  }
1671
1793
  })
1672
1794
  );
1673
- for (const { table, match } of results) {
1795
+ for (const { table: table2, match } of results) {
1674
1796
  if (match) {
1675
- skipped.push(table);
1797
+ skipped.push(table2);
1676
1798
  } else {
1677
- changed.push(table);
1799
+ changed.push(table2);
1678
1800
  }
1679
1801
  }
1680
1802
  return { changed, skipped };
@@ -1974,7 +2096,8 @@ var ExtensionsCheck = class extends Check {
1974
2096
  this.fetchExtensions(ctx.source.dbUrl),
1975
2097
  this.fetchExtensions(ctx.target.dbUrl)
1976
2098
  ]);
1977
- return diffExtensions(source, target);
2099
+ const ignored = new Set(ctx.config.ignoreExtensions ?? []);
2100
+ return diffExtensions(source, target.filter((e) => !ignored.has(e.name) || source.some((s) => s.name === e.name)));
1978
2101
  }
1979
2102
  async fetchExtensions(dbUrl) {
1980
2103
  return await this.queryFn(dbUrl, EXTENSIONS_SQL);
@@ -2042,6 +2165,228 @@ function diffExtensions(source, target) {
2042
2165
  return issues;
2043
2166
  }
2044
2167
 
2168
+ // src/checks/queues.ts
2169
+ var QueuesCheck = class extends Check {
2170
+ constructor(queryFn = pgQuery) {
2171
+ super();
2172
+ this.queryFn = queryFn;
2173
+ }
2174
+ name = "queues";
2175
+ async scan(ctx) {
2176
+ const [source, target] = await Promise.all([
2177
+ this.fetchQueues(ctx.source.dbUrl),
2178
+ this.fetchQueues(ctx.target.dbUrl)
2179
+ ]);
2180
+ if (!source.available && !target.available) {
2181
+ throw new CheckSkipped("pgmq extension not installed");
2182
+ }
2183
+ return diffQueues(source.queues, target.queues, target.available);
2184
+ }
2185
+ async fetchQueues(dbUrl) {
2186
+ return readOrAbsent(
2187
+ async () => ({ queues: await this.queryFn(dbUrl, QUEUES_SQL), available: true }),
2188
+ { queues: [], available: false }
2189
+ );
2190
+ }
2191
+ };
2192
+ var QUEUES_SQL = `
2193
+ SELECT queue_name, is_partitioned, is_unlogged
2194
+ FROM pgmq.meta
2195
+ ORDER BY queue_name
2196
+ `;
2197
+ function createSql(q) {
2198
+ if (q.is_partitioned) return void 0;
2199
+ return `SELECT pgmq.${q.is_unlogged ? "create_unlogged" : "create"}(${quoteLiteral(q.queue_name)});`;
2200
+ }
2201
+ var dropSql = (q) => `SELECT pgmq.drop_queue(${quoteLiteral(q.queue_name)});`;
2202
+ var kind = (q) => q.is_partitioned ? "partitioned" : q.is_unlogged ? "unlogged" : "standard";
2203
+ function diffQueues(source, target, targetHasPgmq = true) {
2204
+ const issues = [];
2205
+ const sourceMap = new Map(source.map((q) => [q.queue_name, q]));
2206
+ const targetMap = new Map(target.map((q) => [q.queue_name, q]));
2207
+ const needsExtension = targetHasPgmq ? "" : "CREATE EXTENSION IF NOT EXISTS pgmq;\n";
2208
+ for (const [name, q] of sourceMap) {
2209
+ const t = targetMap.get(name);
2210
+ if (!t) {
2211
+ const up = createSql(q);
2212
+ issues.push({
2213
+ id: `queues-missing-${name}`,
2214
+ check: "queues",
2215
+ severity: "warning",
2216
+ title: `Missing queue: ${name}`,
2217
+ description: `Queue "${name}" (${kind(q)}) exists in source but not in target.`,
2218
+ sourceValue: q,
2219
+ ...up ? { sql: { up: needsExtension + up, down: dropSql(q) } } : { manualOnly: "a partitioned queue is created with pg_partman settings pgmq.meta does not record \u2014 create it with pgmq.create_partitioned on the target" }
2220
+ });
2221
+ } else if (kind(q) !== kind(t)) {
2222
+ issues.push({
2223
+ id: `queues-kind-${name}`,
2224
+ check: "queues",
2225
+ severity: "warning",
2226
+ title: `Queue kind differs: ${name}`,
2227
+ description: `Queue "${name}" is ${kind(q)} in source and ${kind(t)} in target. Changing it means dropping the queue and its messages, so it is left to you.`,
2228
+ sourceValue: q,
2229
+ targetValue: t,
2230
+ manualOnly: "changing a queue's kind drops it and its messages; recreate it by hand when it is empty"
2231
+ });
2232
+ }
2233
+ }
2234
+ for (const [name, t] of targetMap) {
2235
+ if (sourceMap.has(name)) continue;
2236
+ const down = createSql(t);
2237
+ issues.push({
2238
+ id: `queues-extra-${name}`,
2239
+ check: "queues",
2240
+ severity: "critical",
2241
+ title: `Extra queue: ${name}`,
2242
+ description: `Queue "${name}" exists in target but not in source. Dropping it drops its messages.`,
2243
+ targetValue: t,
2244
+ sql: { up: dropSql(t), down: down ?? "" }
2245
+ });
2246
+ }
2247
+ return issues;
2248
+ }
2249
+
2250
+ // src/checks/partitions.ts
2251
+ var SHAPE = ["control", "partition_interval", "partition_type", "epoch", "time_encoder", "time_decoder"];
2252
+ var SETTINGS = [
2253
+ "premake",
2254
+ "automatic_maintenance",
2255
+ "retention",
2256
+ "retention_schema",
2257
+ "retention_keep_index",
2258
+ "retention_keep_table",
2259
+ "optimize_constraint",
2260
+ "infinite_time_partitions",
2261
+ "inherit_privileges",
2262
+ "constraint_valid",
2263
+ "ignore_default_data",
2264
+ "maintenance_order",
2265
+ "retention_keep_publication",
2266
+ "detach_before_drop",
2267
+ "jobmon",
2268
+ "date_trunc_interval"
2269
+ ];
2270
+ var PartitionsCheck = class extends Check {
2271
+ constructor(queryFn = pgQuery) {
2272
+ super();
2273
+ this.queryFn = queryFn;
2274
+ }
2275
+ name = "partitions";
2276
+ async scan(ctx) {
2277
+ const [source, target] = await Promise.all([this.fetch(ctx.source.dbUrl), this.fetch(ctx.target.dbUrl)]);
2278
+ if (source.schema === null && target.schema === null) {
2279
+ throw new CheckSkipped("pg_partman extension not installed");
2280
+ }
2281
+ return diffPartitionSets(source.sets, target.sets, target.schema);
2282
+ }
2283
+ async fetch(dbUrl) {
2284
+ const rows = await this.queryFn(dbUrl, `SELECT n.nspname AS schema FROM pg_extension e
2285
+ JOIN pg_namespace n ON n.oid = e.extnamespace WHERE e.extname = 'pg_partman'`);
2286
+ const schema = rows[0]?.schema ?? null;
2287
+ if (schema === null) return { schema, sets: [] };
2288
+ const sets = await this.queryFn(dbUrl, `
2289
+ SELECT (SELECT n.nspname || '.' || c.relname FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
2290
+ WHERE c.oid = to_regclass(pc.parent_table)) AS parent_table, pc.control, pc.partition_interval, pc.partition_type,
2291
+ pc.epoch, pc.premake, pc.automatic_maintenance, pc.constraint_cols, pc.jobmon, pc.date_trunc_interval,
2292
+ pc.time_encoder, pc.time_decoder,
2293
+ to_regclass(pc.parent_table || '_default') IS NOT NULL AS has_default,
2294
+ (to_jsonb(pc) - ARRAY(SELECT k FROM jsonb_object_keys(to_jsonb(pc)) k WHERE k <> ALL($1::text[]))) AS settings
2295
+ FROM ${quoteIdent(schema)}.part_config pc
2296
+ WHERE to_regclass(pc.parent_table) IS NOT NULL
2297
+ ORDER BY 1`, [SETTINGS]);
2298
+ return { schema, sets };
2299
+ }
2300
+ };
2301
+ var literal = (v) => v === null || v === void 0 ? "NULL" : typeof v === "boolean" || typeof v === "number" ? String(v) : Array.isArray(v) ? `ARRAY[${v.map((x) => quoteLiteral(String(x))).join(", ")}]::text[]` : quoteLiteral(String(v));
2302
+ function registerSql(set, schema) {
2303
+ const args = [
2304
+ `p_parent_table => ${quoteLiteral(set.parent_table)}`,
2305
+ `p_control => ${quoteLiteral(set.control)}`,
2306
+ `p_interval => ${quoteLiteral(set.partition_interval)}`,
2307
+ `p_type => ${quoteLiteral(set.partition_type)}`,
2308
+ `p_epoch => ${quoteLiteral(set.epoch)}`,
2309
+ `p_premake => ${set.premake}`,
2310
+ `p_default_table => ${set.has_default}`,
2311
+ `p_automatic_maintenance => ${quoteLiteral(set.automatic_maintenance)}`,
2312
+ `p_jobmon => ${set.jobmon}`,
2313
+ ...set.constraint_cols?.length ? [`p_constraint_cols => ${literal(set.constraint_cols)}`] : [],
2314
+ ...set.date_trunc_interval ? [`p_date_trunc_interval => ${quoteLiteral(set.date_trunc_interval)}`] : [],
2315
+ ...set.time_encoder ? [`p_time_encoder => ${quoteLiteral(set.time_encoder)}`] : [],
2316
+ ...set.time_decoder ? [`p_time_decoder => ${quoteLiteral(set.time_decoder)}`] : []
2317
+ ];
2318
+ return `SELECT ${quoteIdent(schema)}.create_parent(${args.join(", ")});`;
2319
+ }
2320
+ function updateSql(schema, parent, settings) {
2321
+ const sets = Object.entries(settings).map(([k, v]) => `${quoteIdent(k)} = ${literal(v)}`);
2322
+ return `UPDATE ${quoteIdent(schema)}.part_config SET ${sets.join(", ")} WHERE parent_table = ${quoteLiteral(parent)};`;
2323
+ }
2324
+ function undoSql(schema, parent) {
2325
+ return `DELETE FROM ${quoteIdent(schema)}.part_config WHERE parent_table = ${quoteLiteral(parent)};`;
2326
+ }
2327
+ function diffPartitionSets(source, target, targetSchema) {
2328
+ const issues = [];
2329
+ const targetMap = new Map(target.map((s) => [s.parent_table, s]));
2330
+ const sourceNames = new Set(source.map((s) => s.parent_table));
2331
+ for (const s of source) {
2332
+ const t = targetMap.get(s.parent_table);
2333
+ if (!t) {
2334
+ issues.push({
2335
+ id: `partitions-missing-${s.parent_table}`,
2336
+ check: "partitions",
2337
+ severity: "warning",
2338
+ title: `Partition set not registered: ${s.parent_table}`,
2339
+ description: `pg_partman manages ${s.parent_table} in source (${s.partition_type} by ${s.control}, every ${s.partition_interval}) but not in target.`,
2340
+ sourceValue: s,
2341
+ ...targetSchema ? { sql: { up: `${registerSql(s, targetSchema)}
2342
+ ${updateSql(targetSchema, s.parent_table, s.settings)}`, down: undoSql(targetSchema, s.parent_table) } } : { manualOnly: "the target does not have pg_partman; install it, then re-run to register the partition set" }
2343
+ });
2344
+ continue;
2345
+ }
2346
+ const reshaped = SHAPE.filter((k) => String(s[k] ?? "") !== String(t[k] ?? ""));
2347
+ if (reshaped.length > 0) {
2348
+ issues.push({
2349
+ id: `partitions-shape-${s.parent_table}`,
2350
+ check: "partitions",
2351
+ severity: "warning",
2352
+ title: `Partition set differs: ${s.parent_table}`,
2353
+ description: `${reshaped.join(", ")} differ${reshaped.length === 1 ? "s" : ""} for ${s.parent_table}. pg_partman cannot change that in place.`,
2354
+ sourceValue: s,
2355
+ targetValue: t,
2356
+ manualOnly: "changing how a set is partitioned means undoing and recreating it with pg_partman (undo_partition, then create_parent)"
2357
+ });
2358
+ continue;
2359
+ }
2360
+ const changed = Object.fromEntries(Object.entries(s.settings).filter(([k, v]) => JSON.stringify(v) !== JSON.stringify(t.settings[k])));
2361
+ if (Object.keys(changed).length > 0 && targetSchema) {
2362
+ const previous = Object.fromEntries(Object.keys(changed).map((k) => [k, t.settings[k]]));
2363
+ issues.push({
2364
+ id: `partitions-settings-${s.parent_table}`,
2365
+ check: "partitions",
2366
+ severity: "warning",
2367
+ title: `Partition set settings differ: ${s.parent_table}`,
2368
+ description: `${Object.keys(changed).join(", ")} differ for ${s.parent_table}.`,
2369
+ sourceValue: s.settings,
2370
+ targetValue: t.settings,
2371
+ sql: { up: updateSql(targetSchema, s.parent_table, changed), down: updateSql(targetSchema, s.parent_table, previous) }
2372
+ });
2373
+ }
2374
+ }
2375
+ for (const t of target) {
2376
+ if (sourceNames.has(t.parent_table)) continue;
2377
+ issues.push({
2378
+ id: `partitions-extra-${t.parent_table}`,
2379
+ check: "partitions",
2380
+ severity: "info",
2381
+ title: `Extra partition set: ${t.parent_table}`,
2382
+ description: `pg_partman manages ${t.parent_table} in target but not in source.`,
2383
+ targetValue: t,
2384
+ manualOnly: "unregistering a set leaves its partitions as ordinary tables; use pg_partman's undo_partition if that is what you want"
2385
+ });
2386
+ }
2387
+ return issues;
2388
+ }
2389
+
2045
2390
  // src/checks/index.ts
2046
2391
  function createDefaultRegistry(options = {}) {
2047
2392
  const registry = new CheckRegistry();
@@ -2059,6 +2404,8 @@ function createDefaultRegistry(options = {}) {
2059
2404
  registry.register(new ExtensionsCheck());
2060
2405
  registry.register(new MigrationsCheck());
2061
2406
  registry.register(new RolesCheck());
2407
+ registry.register(new QueuesCheck());
2408
+ registry.register(new PartitionsCheck());
2062
2409
  return registry;
2063
2410
  }
2064
2411
 
@@ -2070,4 +2417,4 @@ export {
2070
2417
  CheckRegistry,
2071
2418
  createDefaultRegistry
2072
2419
  };
2073
- //# sourceMappingURL=chunk-3HGAV5CC.js.map
2420
+ //# sourceMappingURL=chunk-4AGJRRBT.js.map