@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.
- package/README.md +28 -12
- package/dist/base-command.d.ts +1 -1
- package/dist/base-command.js +2 -2
- package/dist/{branch-C4ndfoj5.d.ts → branch-BppjQBv_.d.ts} +1 -1
- package/dist/{chunk-PYAGSMFB.js → chunk-44UVQJV2.js} +146 -13
- package/dist/{chunk-OB5GP7UB.js → chunk-4AGJRRBT.js} +376 -32
- package/dist/{chunk-MG3SFV5J.js → chunk-4M6OQLPO.js} +1 -1
- package/dist/{chunk-TPX2MAO4.js → chunk-5Q6LSLA3.js} +53 -4
- package/dist/{chunk-F46FFUKC.js → chunk-7N63OPFN.js} +20 -12
- package/dist/chunk-AGONS5JJ.js +242 -0
- package/dist/{chunk-7DL63NWP.js → chunk-H2D3YSKM.js} +6 -2
- package/dist/{chunk-3WKWPSFN.js → chunk-NEM5AEPQ.js} +171 -21
- package/dist/{chunk-6ERVCWLY.js → chunk-NX2KKUS7.js} +6 -6
- package/dist/{chunk-S5L4A3UR.js → chunk-O5ERC2SE.js} +2 -2
- package/dist/{chunk-ICSCHS72.js → chunk-SM7HP6EP.js} +10 -180
- package/dist/{chunk-N5VNNOA2.js → chunk-T7GMPGUO.js} +578 -16
- package/dist/{chunk-DQJSSTIJ.js → chunk-WNTCZM2J.js} +4 -476
- package/dist/{chunk-5STH2A6K.js → chunk-XLZCX6R2.js} +2 -2
- package/dist/{chunk-EFKQOQGG.js → chunk-YWERIWFD.js} +6 -4
- package/dist/{chunk-V2SBZERO.js → chunk-YZM76W22.js} +3 -3
- package/dist/commands/clone.d.ts +2 -2
- package/dist/commands/clone.js +7 -6
- package/dist/commands/diff.d.ts +24 -22
- package/dist/commands/diff.js +11 -11
- package/dist/commands/hukam.d.ts +2 -3
- package/dist/commands/hukam.js +11 -11
- package/dist/commands/init.d.ts +1 -1
- package/dist/commands/mcp.js +8 -8
- package/dist/commands/migrate/baseline.d.ts +1 -1
- package/dist/commands/migrate/baseline.js +3 -3
- package/dist/commands/migrate/create.d.ts +1 -1
- package/dist/commands/migrate/create.js +12 -7
- package/dist/commands/migrate/list.d.ts +1 -1
- package/dist/commands/migrate/list.js +3 -3
- package/dist/commands/migrate/run.d.ts +4 -1
- package/dist/commands/migrate/run.js +7 -6
- package/dist/commands/report.d.ts +1 -1
- package/dist/commands/report.js +2 -2
- package/dist/commands/restore.d.ts +1 -1
- package/dist/commands/restore.js +10 -10
- package/dist/commands/snapshot.d.ts +1 -1
- package/dist/commands/snapshot.js +5 -5
- package/dist/commands/sync.d.ts +2 -3
- package/dist/commands/sync.js +11 -11
- package/dist/{config-CTbeRlJk.d.ts → config-l3ggjw9r.d.ts} +2 -0
- package/dist/index.d.ts +21 -4
- package/dist/index.js +13 -13
- package/package.json +2 -2
- package/dist/chunk-DQQ5E563.js +0 -64
|
@@ -1,16 +1,20 @@
|
|
|
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
|
-
|
|
7
|
-
} from "./chunk-DQJSSTIJ.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-
|
|
17
|
+
} from "./chunk-T7GMPGUO.js";
|
|
14
18
|
import {
|
|
15
19
|
splitSqlStatements
|
|
16
20
|
} from "./chunk-WK5YTINL.js";
|
|
@@ -25,7 +29,7 @@ import {
|
|
|
25
29
|
readJsonArray,
|
|
26
30
|
readJsonObject,
|
|
27
31
|
schemaPolicySql
|
|
28
|
-
} from "./chunk-
|
|
32
|
+
} from "./chunk-SM7HP6EP.js";
|
|
29
33
|
import {
|
|
30
34
|
Check,
|
|
31
35
|
CheckSkipped,
|
|
@@ -37,7 +41,7 @@ import {
|
|
|
37
41
|
quoteName,
|
|
38
42
|
readOrAbsent,
|
|
39
43
|
sqlLiteral
|
|
40
|
-
} from "./chunk-
|
|
44
|
+
} from "./chunk-4M6OQLPO.js";
|
|
41
45
|
import {
|
|
42
46
|
DEFAULT_IGNORE_SCHEMAS,
|
|
43
47
|
SCORE_MAX,
|
|
@@ -88,6 +92,111 @@ function computePostureScore(results) {
|
|
|
88
92
|
return scoreOf(posture);
|
|
89
93
|
}
|
|
90
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
|
+
|
|
91
200
|
// src/scanner.ts
|
|
92
201
|
function resolveExcludedChecks(config) {
|
|
93
202
|
const lists = [config?.checks?.exclude, config?.environments?.[config?.target ?? ""]?.checks?.exclude];
|
|
@@ -110,14 +219,23 @@ function checkConcurrency() {
|
|
|
110
219
|
}
|
|
111
220
|
return 4;
|
|
112
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
|
+
}
|
|
113
229
|
async function runCheck(check, ctx, name, sourceDbUrl) {
|
|
114
230
|
const start = performance.now();
|
|
231
|
+
const notices = [];
|
|
115
232
|
try {
|
|
116
|
-
const issues = await check.scan(ctx);
|
|
233
|
+
const issues = withHazards(await check.scan({ ...ctx, onNotice: (n) => notices.push(n) }));
|
|
117
234
|
return {
|
|
118
235
|
check: name,
|
|
119
236
|
status: issues.length > 0 ? "drifted" : "clean",
|
|
120
237
|
issues,
|
|
238
|
+
...notices.length > 0 ? { notices } : {},
|
|
121
239
|
durationMs: Math.round(performance.now() - start)
|
|
122
240
|
};
|
|
123
241
|
} catch (err) {
|
|
@@ -265,7 +383,7 @@ var SchemaCheck = class extends Check {
|
|
|
265
383
|
type: "schema",
|
|
266
384
|
include: "both",
|
|
267
385
|
timeoutSeconds: ctx.target?.checks?.schema?.timeout,
|
|
268
|
-
onProgress: ctx.onDetail ? ({ table, tablesSeen }) => ctx.onDetail?.(`${tablesSeen} tables \xB7 ${
|
|
386
|
+
onProgress: ctx.onDetail ? ({ table: table2, tablesSeen }) => ctx.onDetail?.(`${tablesSeen} tables \xB7 ${table2}`) : void 0,
|
|
269
387
|
ignoreSchemas,
|
|
270
388
|
// dbdiff takes the include/exclude lists natively, globs and all, so
|
|
271
389
|
// the scope is enforced in the diff itself rather than by discarding
|
|
@@ -275,6 +393,7 @@ var SchemaCheck = class extends Check {
|
|
|
275
393
|
}),
|
|
276
394
|
this.fetchIgnoredSchemaTables(ctx.target.dbUrl, ignoreSchemas)
|
|
277
395
|
]);
|
|
396
|
+
for (const notice of schemaNotices(result.notices ?? [])) ctx.onNotice?.(notice);
|
|
278
397
|
return sqlToIssues(result, "schema", ignoreSchemas, ignoredSchemaTables);
|
|
279
398
|
} catch (err) {
|
|
280
399
|
if (err instanceof Error && err.message.includes("@dbdiff/cli is not installed")) {
|
|
@@ -1031,8 +1150,8 @@ var StorageCheck = class extends Check {
|
|
|
1031
1150
|
}
|
|
1032
1151
|
return issues;
|
|
1033
1152
|
}
|
|
1034
|
-
async hasTable(dbUrl,
|
|
1035
|
-
const rows = await this.queryFn(dbUrl, tableExistsSql(
|
|
1153
|
+
async hasTable(dbUrl, table2) {
|
|
1154
|
+
const rows = await this.queryFn(dbUrl, tableExistsSql(table2));
|
|
1036
1155
|
return rows[0]?.present === true;
|
|
1037
1156
|
}
|
|
1038
1157
|
async listTypedBuckets(dbUrl, spec) {
|
|
@@ -1137,11 +1256,11 @@ var TYPED_BUCKET_TABLES = [
|
|
|
1137
1256
|
sql: `SELECT id AS key FROM storage.buckets_vectors ORDER BY id`
|
|
1138
1257
|
}
|
|
1139
1258
|
];
|
|
1140
|
-
function tableExistsSql(
|
|
1259
|
+
function tableExistsSql(table2) {
|
|
1141
1260
|
return `
|
|
1142
1261
|
SELECT EXISTS (
|
|
1143
1262
|
SELECT 1 FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
1144
|
-
WHERE n.nspname = 'storage' AND c.relname = '${
|
|
1263
|
+
WHERE n.nspname = 'storage' AND c.relname = '${table2}'
|
|
1145
1264
|
) AS present
|
|
1146
1265
|
`;
|
|
1147
1266
|
}
|
|
@@ -1499,14 +1618,14 @@ function diffAuthConfig(source, target, targetSrc) {
|
|
|
1499
1618
|
const sv = source[key];
|
|
1500
1619
|
const tv = target[key];
|
|
1501
1620
|
if (JSON.stringify(sv) === JSON.stringify(tv)) continue;
|
|
1502
|
-
const
|
|
1621
|
+
const kind2 = classifyAuthKey(key);
|
|
1503
1622
|
const base2 = {
|
|
1504
1623
|
id: `auth-${key.toLowerCase()}`,
|
|
1505
1624
|
check: "auth",
|
|
1506
1625
|
severity: isCriticalAuthKey(key) ? "critical" : "info",
|
|
1507
1626
|
title: `Auth config mismatch: ${key}`
|
|
1508
1627
|
};
|
|
1509
|
-
if (
|
|
1628
|
+
if (kind2 === "secret") {
|
|
1510
1629
|
issues.push({
|
|
1511
1630
|
...base2,
|
|
1512
1631
|
description: `"${key}" differs between source and target. It is a secret: set it on the target directly.`,
|
|
@@ -1514,14 +1633,14 @@ function diffAuthConfig(source, target, targetSrc) {
|
|
|
1514
1633
|
});
|
|
1515
1634
|
continue;
|
|
1516
1635
|
}
|
|
1517
|
-
const action = targetSrc.kind === "hosted" &&
|
|
1636
|
+
const action = targetSrc.kind === "hosted" && kind2 === "shared" ? {
|
|
1518
1637
|
method: "PATCH",
|
|
1519
1638
|
url: `${SUPABASE_MGMT_API}/${encodeURIComponent(targetSrc.ref)}/config/auth`,
|
|
1520
1639
|
headers: { Authorization: `Bearer ${targetSrc.token}` },
|
|
1521
1640
|
body: { [key]: sv },
|
|
1522
1641
|
label: `Set auth config "${key}" to ${JSON.stringify(sv)} in target`
|
|
1523
1642
|
} : void 0;
|
|
1524
|
-
const manualOnly =
|
|
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.";
|
|
1525
1644
|
issues.push({
|
|
1526
1645
|
...base2,
|
|
1527
1646
|
description: `"${key}" differs between source (${JSON.stringify(sv)}) and target (${JSON.stringify(tv)}).` + (manualOnly ? ` ${manualOnly}` : ""),
|
|
@@ -1638,25 +1757,25 @@ SELECT cron.schedule('${key}', '${tj.schedule}', $$${tj.command.trim()}$$);`
|
|
|
1638
1757
|
}
|
|
1639
1758
|
|
|
1640
1759
|
// src/checksum.ts
|
|
1641
|
-
async function getTableFingerprint(dbUrl,
|
|
1760
|
+
async function getTableFingerprint(dbUrl, table2, queryFn = pgQuery) {
|
|
1642
1761
|
const sql = `
|
|
1643
1762
|
SELECT count(*)::int AS row_count,
|
|
1644
1763
|
coalesce(sum(('x' || substr(d, 1, 16))::bit(64)::bigint::numeric), 0)::text
|
|
1645
1764
|
|| ':' ||
|
|
1646
1765
|
coalesce(sum(('x' || substr(d, 17, 16))::bit(64)::bigint::numeric), 0)::text AS content
|
|
1647
|
-
FROM (SELECT md5(t::text) AS d FROM ${quoteIdent(
|
|
1766
|
+
FROM (SELECT md5(t::text) AS d FROM ${quoteIdent(table2)} t) s
|
|
1648
1767
|
`;
|
|
1649
1768
|
const [row] = await queryFn(dbUrl, sql);
|
|
1650
1769
|
return {
|
|
1651
|
-
table,
|
|
1770
|
+
table: table2,
|
|
1652
1771
|
rowCount: row.row_count,
|
|
1653
1772
|
content: row.content
|
|
1654
1773
|
};
|
|
1655
1774
|
}
|
|
1656
|
-
async function tablesMatch(sourceUrl, targetUrl,
|
|
1775
|
+
async function tablesMatch(sourceUrl, targetUrl, table2, queryFn = pgQuery) {
|
|
1657
1776
|
const [source, target] = await Promise.all([
|
|
1658
|
-
getTableFingerprint(sourceUrl,
|
|
1659
|
-
getTableFingerprint(targetUrl,
|
|
1777
|
+
getTableFingerprint(sourceUrl, table2, queryFn),
|
|
1778
|
+
getTableFingerprint(targetUrl, table2, queryFn)
|
|
1660
1779
|
]);
|
|
1661
1780
|
return source.rowCount === target.rowCount && source.content === target.content;
|
|
1662
1781
|
}
|
|
@@ -1664,20 +1783,20 @@ async function filterChangedTables(sourceUrl, targetUrl, tables, queryFn = pgQue
|
|
|
1664
1783
|
const changed = [];
|
|
1665
1784
|
const skipped = [];
|
|
1666
1785
|
const results = await Promise.all(
|
|
1667
|
-
tables.map(async (
|
|
1786
|
+
tables.map(async (table2) => {
|
|
1668
1787
|
try {
|
|
1669
|
-
const match = await tablesMatch(sourceUrl, targetUrl,
|
|
1670
|
-
return { table, match };
|
|
1788
|
+
const match = await tablesMatch(sourceUrl, targetUrl, table2, queryFn);
|
|
1789
|
+
return { table: table2, match };
|
|
1671
1790
|
} catch {
|
|
1672
|
-
return { table, match: false };
|
|
1791
|
+
return { table: table2, match: false };
|
|
1673
1792
|
}
|
|
1674
1793
|
})
|
|
1675
1794
|
);
|
|
1676
|
-
for (const { table, match } of results) {
|
|
1795
|
+
for (const { table: table2, match } of results) {
|
|
1677
1796
|
if (match) {
|
|
1678
|
-
skipped.push(
|
|
1797
|
+
skipped.push(table2);
|
|
1679
1798
|
} else {
|
|
1680
|
-
changed.push(
|
|
1799
|
+
changed.push(table2);
|
|
1681
1800
|
}
|
|
1682
1801
|
}
|
|
1683
1802
|
return { changed, skipped };
|
|
@@ -1977,7 +2096,8 @@ var ExtensionsCheck = class extends Check {
|
|
|
1977
2096
|
this.fetchExtensions(ctx.source.dbUrl),
|
|
1978
2097
|
this.fetchExtensions(ctx.target.dbUrl)
|
|
1979
2098
|
]);
|
|
1980
|
-
|
|
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)));
|
|
1981
2101
|
}
|
|
1982
2102
|
async fetchExtensions(dbUrl) {
|
|
1983
2103
|
return await this.queryFn(dbUrl, EXTENSIONS_SQL);
|
|
@@ -2045,6 +2165,228 @@ function diffExtensions(source, target) {
|
|
|
2045
2165
|
return issues;
|
|
2046
2166
|
}
|
|
2047
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
|
+
|
|
2048
2390
|
// src/checks/index.ts
|
|
2049
2391
|
function createDefaultRegistry(options = {}) {
|
|
2050
2392
|
const registry = new CheckRegistry();
|
|
@@ -2062,6 +2404,8 @@ function createDefaultRegistry(options = {}) {
|
|
|
2062
2404
|
registry.register(new ExtensionsCheck());
|
|
2063
2405
|
registry.register(new MigrationsCheck());
|
|
2064
2406
|
registry.register(new RolesCheck());
|
|
2407
|
+
registry.register(new QueuesCheck());
|
|
2408
|
+
registry.register(new PartitionsCheck());
|
|
2065
2409
|
return registry;
|
|
2066
2410
|
}
|
|
2067
2411
|
|
|
@@ -2073,4 +2417,4 @@ export {
|
|
|
2073
2417
|
CheckRegistry,
|
|
2074
2418
|
createDefaultRegistry
|
|
2075
2419
|
};
|
|
2076
|
-
//# sourceMappingURL=chunk-
|
|
2420
|
+
//# sourceMappingURL=chunk-4AGJRRBT.js.map
|
|
@@ -3,17 +3,22 @@ import {
|
|
|
3
3
|
getAppliedVersions,
|
|
4
4
|
getPendingMigrations,
|
|
5
5
|
runMigration
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-O5ERC2SE.js";
|
|
7
|
+
import {
|
|
8
|
+
comparedSchemas,
|
|
9
|
+
fingerprint
|
|
10
|
+
} from "./chunk-NEM5AEPQ.js";
|
|
7
11
|
import {
|
|
8
12
|
isDestructiveSql
|
|
9
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-T7GMPGUO.js";
|
|
10
14
|
import {
|
|
11
15
|
splitSqlStatements
|
|
12
16
|
} from "./chunk-WK5YTINL.js";
|
|
13
17
|
import {
|
|
14
18
|
BaseCommand
|
|
15
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-XLZCX6R2.js";
|
|
16
20
|
import {
|
|
21
|
+
DEFAULT_IGNORE_SCHEMAS,
|
|
17
22
|
bold,
|
|
18
23
|
dim,
|
|
19
24
|
errMsg,
|
|
@@ -24,6 +29,24 @@ import {
|
|
|
24
29
|
// src/commands/migrate/run.ts
|
|
25
30
|
import { Flags } from "@oclif/core";
|
|
26
31
|
import { readFile } from "fs/promises";
|
|
32
|
+
|
|
33
|
+
// src/written-against.ts
|
|
34
|
+
import { createHash } from "crypto";
|
|
35
|
+
var WRITTEN_AGAINST = "-- supaforge:written-against";
|
|
36
|
+
async function targetFingerprint(dbUrl, ignoreSchemas) {
|
|
37
|
+
const schemas = await comparedSchemas(dbUrl, dbUrl, ignoreSchemas);
|
|
38
|
+
return createHash("sha256").update(await fingerprint(dbUrl, schemas)).digest("hex").slice(0, 16);
|
|
39
|
+
}
|
|
40
|
+
function writtenAgainstLine(env, digest) {
|
|
41
|
+
return `${WRITTEN_AGAINST} ${env} ${digest}`;
|
|
42
|
+
}
|
|
43
|
+
function writtenAgainst(sql) {
|
|
44
|
+
const line = sql.split("\n").find((l) => l.startsWith(WRITTEN_AGAINST + " "));
|
|
45
|
+
const [env, digest] = line?.slice(WRITTEN_AGAINST.length + 1).trim().split(/\s+/) ?? [];
|
|
46
|
+
return env && digest ? { env, digest } : void 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/commands/migrate/run.ts
|
|
27
50
|
var MigrateRun = class _MigrateRun extends BaseCommand {
|
|
28
51
|
static description = "Execute pending migrations against a Supabase environment";
|
|
29
52
|
static examples = [
|
|
@@ -51,6 +74,10 @@ var MigrateRun = class _MigrateRun extends BaseCommand {
|
|
|
51
74
|
"allow-destructive": Flags.boolean({
|
|
52
75
|
description: "Permit migrations that drop tables or columns, delete rows, or drop policies",
|
|
53
76
|
default: false
|
|
77
|
+
}),
|
|
78
|
+
force: Flags.boolean({
|
|
79
|
+
description: "Run a migration even though the target has changed since it was written against it",
|
|
80
|
+
default: false
|
|
54
81
|
})
|
|
55
82
|
};
|
|
56
83
|
async run() {
|
|
@@ -95,6 +122,17 @@ var MigrateRun = class _MigrateRun extends BaseCommand {
|
|
|
95
122
|
this.log(` ${dim(truncateSql(sql))}`);
|
|
96
123
|
}
|
|
97
124
|
}
|
|
125
|
+
const stale = await this.changedSinceWritten(pending, envName, env.dbUrl, config.ignoreSchemas ?? DEFAULT_IGNORE_SCHEMAS);
|
|
126
|
+
if (stale) {
|
|
127
|
+
this.log("");
|
|
128
|
+
this.log(warn(`${stale} was written against ${envName} as it was then, and ${envName} has changed since.`));
|
|
129
|
+
this.log(` ${dim("Re-create it with migrate create, or pass --force to run it anyway.")}`);
|
|
130
|
+
if (flags.apply && !flags.force) {
|
|
131
|
+
this.log(`
|
|
132
|
+
${warn("Nothing was applied.")}`);
|
|
133
|
+
this.exit(1);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
98
136
|
if (!flags.apply) {
|
|
99
137
|
this.log(`
|
|
100
138
|
${dim("Preview only \u2014 nothing was applied.")} Re-run with ${bold("--apply")} to execute` + (destructive.length > 0 ? `, and ${bold("--allow-destructive")} for the statements above.` : "."));
|
|
@@ -122,6 +160,15 @@ ${warn(`Stopped after ${applied_count} migration(s) due to error.`)}`);
|
|
|
122
160
|
this.log(`
|
|
123
161
|
${ok(`Applied ${applied_count} migration(s) successfully.`)} \u2713`);
|
|
124
162
|
}
|
|
163
|
+
/** The first pending migration written against `envName`, if the target has changed since. */
|
|
164
|
+
async changedSinceWritten(pending, envName, dbUrl, ignoreSchemas) {
|
|
165
|
+
const [first] = pending;
|
|
166
|
+
if (!first) return void 0;
|
|
167
|
+
const header = writtenAgainst(await readFile(first.path, "utf-8").catch(() => ""));
|
|
168
|
+
if (!header || header.env !== envName) return void 0;
|
|
169
|
+
const now = await targetFingerprint(dbUrl, ignoreSchemas).catch(() => header.digest);
|
|
170
|
+
return now === header.digest ? void 0 : first.filename;
|
|
171
|
+
}
|
|
125
172
|
};
|
|
126
173
|
async function findDestructiveStatements(migrations, readFileFn = (path) => readFile(path, "utf-8")) {
|
|
127
174
|
const found = [];
|
|
@@ -147,8 +194,10 @@ function truncateSql(sql) {
|
|
|
147
194
|
}
|
|
148
195
|
|
|
149
196
|
export {
|
|
197
|
+
targetFingerprint,
|
|
198
|
+
writtenAgainstLine,
|
|
150
199
|
MigrateRun,
|
|
151
200
|
findDestructiveStatements,
|
|
152
201
|
truncateSql
|
|
153
202
|
};
|
|
154
|
-
//# sourceMappingURL=chunk-
|
|
203
|
+
//# sourceMappingURL=chunk-5Q6LSLA3.js.map
|