@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,9 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
+
isCommentOnly,
|
|
2
3
|
splitSqlStatements
|
|
3
4
|
} from "./chunk-WK5YTINL.js";
|
|
4
5
|
import {
|
|
5
|
-
escapeRegex
|
|
6
|
-
|
|
6
|
+
escapeRegex,
|
|
7
|
+
globToRegExp,
|
|
8
|
+
matchesGlob,
|
|
9
|
+
parseFlagList
|
|
10
|
+
} from "./chunk-AGONS5JJ.js";
|
|
11
|
+
import {
|
|
12
|
+
newPgClient
|
|
13
|
+
} from "./chunk-4M6OQLPO.js";
|
|
7
14
|
import {
|
|
8
15
|
DBDIFF_EXEC_TIMEOUT_MS,
|
|
9
16
|
DBDIFF_MAX_BUFFER,
|
|
@@ -13,6 +20,108 @@ import {
|
|
|
13
20
|
schemaDiffIgnores
|
|
14
21
|
} from "./chunk-KNXHLVF7.js";
|
|
15
22
|
|
|
23
|
+
// src/utils/table-filter.ts
|
|
24
|
+
var parseTableList = parseFlagList;
|
|
25
|
+
function resolveTableFilter(config, cli = {}) {
|
|
26
|
+
const cliTables = parseTableList(cli.tables);
|
|
27
|
+
const cliExclude = parseTableList(cli.excludeTables);
|
|
28
|
+
const configTables = Array.isArray(config?.checks?.tables) ? config.checks.tables : [];
|
|
29
|
+
const configExclude = Array.isArray(config?.checks?.excludeTables) ? config.checks.excludeTables : [];
|
|
30
|
+
const tables = cliTables.length > 0 ? cliTables : parseTableList(configTables);
|
|
31
|
+
const excludeTables = [.../* @__PURE__ */ new Set([...parseTableList(configExclude), ...cliExclude])];
|
|
32
|
+
const filter = {};
|
|
33
|
+
if (tables.length > 0) filter.tables = tables;
|
|
34
|
+
if (excludeTables.length > 0) filter.excludeTables = excludeTables;
|
|
35
|
+
return filter;
|
|
36
|
+
}
|
|
37
|
+
function isFiltered(filter) {
|
|
38
|
+
return Boolean(filter?.tables?.length || filter?.excludeTables?.length);
|
|
39
|
+
}
|
|
40
|
+
function describeTableFilter(filter) {
|
|
41
|
+
if (!isFiltered(filter)) return null;
|
|
42
|
+
const parts = [];
|
|
43
|
+
if (filter?.tables?.length) parts.push(`only ${filter.tables.join(", ")}`);
|
|
44
|
+
if (filter?.excludeTables?.length) parts.push(`excluding ${filter.excludeTables.join(", ")}`);
|
|
45
|
+
return `Scoped to ${parts.join(", ")} \u2014 applies to the schema and data checks; other layers are unfiltered.`;
|
|
46
|
+
}
|
|
47
|
+
function matchesPattern(name, pattern) {
|
|
48
|
+
const candidates = [name.toLowerCase()];
|
|
49
|
+
const dot = name.indexOf(".");
|
|
50
|
+
if (dot !== -1) candidates.push(name.slice(dot + 1).toLowerCase());
|
|
51
|
+
const rx = globToRegExp(pattern.toLowerCase());
|
|
52
|
+
return candidates.some((c) => rx.test(c));
|
|
53
|
+
}
|
|
54
|
+
function applyTableFilter(names2, filter) {
|
|
55
|
+
if (!isFiltered(filter)) return names2;
|
|
56
|
+
let out = names2;
|
|
57
|
+
if (filter?.tables?.length) {
|
|
58
|
+
out = out.filter((n) => filter.tables.some((p) => matchesPattern(n, p)));
|
|
59
|
+
}
|
|
60
|
+
if (filter?.excludeTables?.length) {
|
|
61
|
+
out = out.filter((n) => !filter.excludeTables.some((p) => matchesPattern(n, p)));
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/types/drift.ts
|
|
67
|
+
var CHECK_NAMES = [
|
|
68
|
+
"schema",
|
|
69
|
+
"rls",
|
|
70
|
+
"rls-coverage",
|
|
71
|
+
"edge-functions",
|
|
72
|
+
"storage",
|
|
73
|
+
"auth",
|
|
74
|
+
"cron",
|
|
75
|
+
"data",
|
|
76
|
+
"webhooks",
|
|
77
|
+
"realtime",
|
|
78
|
+
"vault",
|
|
79
|
+
"extensions",
|
|
80
|
+
"migrations",
|
|
81
|
+
"roles",
|
|
82
|
+
"queues",
|
|
83
|
+
"partitions"
|
|
84
|
+
];
|
|
85
|
+
var CHECK_META = {
|
|
86
|
+
"schema": { number: 1, emoji: "\u{1F5C4}\uFE0F", label: "Schema" },
|
|
87
|
+
"rls": { number: 2, emoji: "\u{1F512}", label: "RLS Policies" },
|
|
88
|
+
"rls-coverage": { number: 3, emoji: "\u{1F6E1}\uFE0F", label: "RLS Coverage" },
|
|
89
|
+
"edge-functions": { number: 4, emoji: "\u26A1", label: "Edge Functions" },
|
|
90
|
+
"storage": { number: 5, emoji: "\u{1FAA3}", label: "Storage" },
|
|
91
|
+
"auth": { number: 6, emoji: "\u{1F511}", label: "Auth Config" },
|
|
92
|
+
"cron": { number: 7, emoji: "\u23F0", label: "Cron Jobs" },
|
|
93
|
+
"data": { number: 8, emoji: "\u{1F5C3}\uFE0F", label: "Reference Data" },
|
|
94
|
+
"webhooks": { number: 9, emoji: "\u26A1\u{1F517}", label: "Webhooks" },
|
|
95
|
+
"realtime": { number: 10, emoji: "\u{1F4E1}", label: "Realtime Publications" },
|
|
96
|
+
"vault": { number: 11, emoji: "\u{1F510}", label: "Vault Secrets" },
|
|
97
|
+
"extensions": { number: 12, emoji: "\u{1F9E9}", label: "Postgres Extensions" },
|
|
98
|
+
"migrations": { number: 13, emoji: "\u{1F4CB}", label: "Migration History" },
|
|
99
|
+
"roles": { number: 14, emoji: "\u{1F464}", label: "Postgres Roles & Grants" },
|
|
100
|
+
"queues": { number: 15, emoji: "\u{1F4EC}", label: "Queues" },
|
|
101
|
+
"partitions": { number: 16, emoji: "\u{1F5C2}\uFE0F", label: "Partition Sets" }
|
|
102
|
+
};
|
|
103
|
+
var CHECK_KIND = {
|
|
104
|
+
"schema": "comparison",
|
|
105
|
+
"rls": "comparison",
|
|
106
|
+
"rls-coverage": "posture",
|
|
107
|
+
"edge-functions": "comparison",
|
|
108
|
+
"storage": "comparison",
|
|
109
|
+
"auth": "comparison",
|
|
110
|
+
"cron": "comparison",
|
|
111
|
+
"data": "comparison",
|
|
112
|
+
"webhooks": "comparison",
|
|
113
|
+
"realtime": "comparison",
|
|
114
|
+
"vault": "comparison",
|
|
115
|
+
"extensions": "comparison",
|
|
116
|
+
"migrations": "posture",
|
|
117
|
+
"roles": "comparison",
|
|
118
|
+
"queues": "comparison",
|
|
119
|
+
"partitions": "comparison"
|
|
120
|
+
};
|
|
121
|
+
function isComparisonCheck(name) {
|
|
122
|
+
return CHECK_KIND[name] === "comparison";
|
|
123
|
+
}
|
|
124
|
+
|
|
16
125
|
// src/dbdiff.ts
|
|
17
126
|
import { execFile } from "child_process";
|
|
18
127
|
import { createRequire } from "module";
|
|
@@ -416,6 +525,9 @@ var REPLACING_KINDS = {
|
|
|
416
525
|
|
|
417
526
|
// src/dbdiff.ts
|
|
418
527
|
var execFileAsync = promisify(execFile);
|
|
528
|
+
function dbdiffNotices(stdout) {
|
|
529
|
+
return stdout.replace(/\u001b\[[0-9;]*m/g, "").split("\n").map((line) => line.trim()).filter((line) => line.startsWith("\u26A0")).map((line) => line.replace(/^⚠\s*/, ""));
|
|
530
|
+
}
|
|
419
531
|
function dbdiffNotInstalled(code, stderr) {
|
|
420
532
|
if (code === "ENOENT") return true;
|
|
421
533
|
return /ERR_MODULE_NOT_FOUND|could not determine executable|npm (?:ERR!|error) (?:code E404|404)|\bE404\b|command not found|: not found\s*$/m.test(stderr);
|
|
@@ -534,13 +646,14 @@ async function runDbDiff(options) {
|
|
|
534
646
|
maxBuffer: DBDIFF_MAX_BUFFER
|
|
535
647
|
});
|
|
536
648
|
attachProgress(running, options.onProgress);
|
|
537
|
-
await running;
|
|
649
|
+
const { stdout } = await running;
|
|
650
|
+
const notices = dbdiffNotices(String(stdout ?? ""));
|
|
538
651
|
const fileExists = await access(outputFile).then(() => true, () => false);
|
|
539
652
|
if (!fileExists) {
|
|
540
|
-
return { up: "", down: "" };
|
|
653
|
+
return { up: "", down: "", notices };
|
|
541
654
|
}
|
|
542
655
|
const output = await readFile(outputFile, "utf8");
|
|
543
|
-
return parseDbDiffOutput(output);
|
|
656
|
+
return { ...parseDbDiffOutput(output), notices };
|
|
544
657
|
} catch (err) {
|
|
545
658
|
const errObj = err ?? {};
|
|
546
659
|
const message = errMsg(err);
|
|
@@ -589,6 +702,9 @@ function isDestructiveSql(sql) {
|
|
|
589
702
|
return destructiveReason(sql) !== void 0;
|
|
590
703
|
}
|
|
591
704
|
function destructiveReason(sql, recreatedElsewhere = /* @__PURE__ */ new Set()) {
|
|
705
|
+
if (/\bPGMQ\s*\.\s*"?DROP_QUEUE"?\s*\(/.test(sqlSkeleton(sql).toUpperCase())) {
|
|
706
|
+
return "drops a queue and the messages in it";
|
|
707
|
+
}
|
|
592
708
|
const statements = parseStatements(sql);
|
|
593
709
|
return statements ? destructiveInTree(statements, recreatedElsewhere) : destructiveInText(sql, recreatedElsewhere);
|
|
594
710
|
}
|
|
@@ -658,6 +774,14 @@ function sqlToIssues(result, check, ignoreSchemas, ignoredSchemaTables) {
|
|
|
658
774
|
const issues = unfoldedIssues(result, check, ignoreSchemas, ignoredSchemaTables);
|
|
659
775
|
return check === "schema" ? foldCommentsIntoCreates(issues) : issues;
|
|
660
776
|
}
|
|
777
|
+
var COMPARED_ELSEWHERE = /^Publication objects are not compared/;
|
|
778
|
+
function schemaNotices(notices) {
|
|
779
|
+
return notices.filter((n) => !COMPARED_ELSEWHERE.test(n)).map((notice) => {
|
|
780
|
+
const [what, advice] = notice.split(" \u2014 ");
|
|
781
|
+
const title = /not compared/.test(what) ? `Not compared: ${what.replace(/ objects are not compared/, "")}` : what;
|
|
782
|
+
return advice ? `${title} \u2014 ${advice}.` : `${title}.`;
|
|
783
|
+
});
|
|
784
|
+
}
|
|
661
785
|
var COMMENT_TITLE = /^(?:Comment changed|Comment missing): (?:schema (.+)|(.+) \(([a-z ]+)\))$/;
|
|
662
786
|
var CREATE_TITLE = /^(Schema|Table|View|Function|Procedure|Type|Domain|Sequence|Index|Policy|Trigger|Extension) (?:missing|modified): (.+)$/;
|
|
663
787
|
var ON_A_TABLE = /* @__PURE__ */ new Set(["column", "constraint", "trigger", "policy", "rule"]);
|
|
@@ -764,6 +888,18 @@ function issuesFromUnits(upUnits, downUnits, check, ignoreSchemas, ignoredSchema
|
|
|
764
888
|
...manual
|
|
765
889
|
};
|
|
766
890
|
}
|
|
891
|
+
const modified = modifiedTitle(up, statements);
|
|
892
|
+
if (modified) {
|
|
893
|
+
return {
|
|
894
|
+
id: `${check}-alter-${up.kind.replace(/^Alter/, "").toLowerCase()}-${i + 1}`,
|
|
895
|
+
check,
|
|
896
|
+
severity: "warning",
|
|
897
|
+
title: modified,
|
|
898
|
+
description: "Definition differs between source and target.",
|
|
899
|
+
sql: { up: upSql, down: downSql },
|
|
900
|
+
...manual
|
|
901
|
+
};
|
|
902
|
+
}
|
|
767
903
|
const lead = leadStatement(up, statements);
|
|
768
904
|
const type = classifyStatement(lead);
|
|
769
905
|
return {
|
|
@@ -808,6 +944,28 @@ function replacedView(unit, statements) {
|
|
|
808
944
|
return named2 ? { kind: "view", name: REPLACEABLE_NAME.view(named2) } : void 0;
|
|
809
945
|
}
|
|
810
946
|
var bareName2 = (name) => name.slice(name.lastIndexOf(".") + 1).replace(/"/g, "").toLowerCase();
|
|
947
|
+
var MODIFIED_KINDS = {
|
|
948
|
+
AlterRangeType: "Type modified",
|
|
949
|
+
AlterAggregate: "Aggregate modified",
|
|
950
|
+
AlterRule: "Rule modified",
|
|
951
|
+
AlterStatistics: "Statistics modified",
|
|
952
|
+
AlterEventTrigger: "Event trigger modified",
|
|
953
|
+
AlterCast: "Cast modified",
|
|
954
|
+
AlterTextSearchConfiguration: "Text search configuration modified",
|
|
955
|
+
AlterTextSearchDictionary: "Text search dictionary modified",
|
|
956
|
+
AlterForeignServer: "Foreign server modified",
|
|
957
|
+
AlterUserMapping: "User mapping modified",
|
|
958
|
+
AlterForeignTable: "Foreign table modified",
|
|
959
|
+
AlterDefaultPrivileges: "Default privileges differ"
|
|
960
|
+
};
|
|
961
|
+
function modifiedTitle(unit, statements) {
|
|
962
|
+
const label = MODIFIED_KINDS[unit.kind];
|
|
963
|
+
if (!label) return void 0;
|
|
964
|
+
const making = statements.find((sql) => !/^\s*DROP\b/i.test(sql)) ?? statements[0];
|
|
965
|
+
const summary = making ? summariseStatement(making, "schema") : "";
|
|
966
|
+
const name = summary.slice(summary.indexOf(": ") + 2);
|
|
967
|
+
return `${label}: ${name || unit.object}`;
|
|
968
|
+
}
|
|
811
969
|
function leadStatement(unit, statements) {
|
|
812
970
|
if (unit.kind.startsWith("AlterTable")) {
|
|
813
971
|
const alter = statements.find((sql) => /^\s*ALTER\s+TABLE\b/i.test(sql));
|
|
@@ -1103,9 +1261,40 @@ var AFTER = {
|
|
|
1103
1261
|
index: /\bINDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+(?:NOT\s+)?EXISTS\s+)?/i,
|
|
1104
1262
|
policy: /\bPOLICY\s+(?:IF\s+EXISTS\s+)?/i,
|
|
1105
1263
|
collation: /\bCOLLATION\s+(?:IF\s+(?:NOT\s+)?EXISTS\s+)?/i,
|
|
1264
|
+
eventTrigger: /\bEVENT\s+TRIGGER\s+(?:IF\s+EXISTS\s+)?/i,
|
|
1265
|
+
aggregate: /\bAGGREGATE\s+(?:IF\s+EXISTS\s+)?/i,
|
|
1266
|
+
statistics: /\bSTATISTICS\s+(?:IF\s+(?:NOT\s+)?EXISTS\s+)?/i,
|
|
1267
|
+
textSearch: /\bTEXT\s+SEARCH\s+(?:CONFIGURATION|DICTIONARY)\s+(?:IF\s+EXISTS\s+)?/i,
|
|
1268
|
+
server: /\bSERVER\s+(?:IF\s+(?:NOT\s+)?EXISTS\s+)?/i,
|
|
1269
|
+
foreignTable: /\bFOREIGN\s+TABLE\s+(?:IF\s+(?:NOT\s+)?EXISTS\s+)?/i,
|
|
1106
1270
|
on: /\bON\s+(?:ONLY\s+)?/i
|
|
1107
1271
|
};
|
|
1108
1272
|
var named = (head) => (sql) => extractQualifiedName(sql, head);
|
|
1273
|
+
function renameLabel(sql) {
|
|
1274
|
+
const table = extractQualifiedName(sql, /\b(?:TABLE|INDEX)\s+(?:IF\s+EXISTS\s+)?(?:ONLY\s+)?/i);
|
|
1275
|
+
const to = /\bTO\s+("(?:[^"]|"")*"|[\w$]+)\s*;?\s*$/i.exec(sql.trim())?.[1];
|
|
1276
|
+
const from = /\bRENAME\s+(?:COLUMN|CONSTRAINT)\s+("(?:[^"]|"")*"|[\w$]+)/i.exec(sql)?.[1];
|
|
1277
|
+
return `${qualifySchemaName(table)}${from ? "." + unquoted(from) : ""} \u2192 ${to ? unquoted(to) : UNKNOWN_NAME}`;
|
|
1278
|
+
}
|
|
1279
|
+
function defaultPrivilegesLabel(sql) {
|
|
1280
|
+
const role = /\bFOR\s+ROLE\s+("(?:[^"]|"")*"|\S+)/i.exec(sql)?.[1];
|
|
1281
|
+
const schema = /\bIN\s+SCHEMA\s+("(?:[^"]|"")*"|\S+)/i.exec(sql)?.[1];
|
|
1282
|
+
const kind = /\bON\s+(TABLES|SEQUENCES|FUNCTIONS|ROUTINES|TYPES|SCHEMAS)\b/i.exec(sql)?.[1];
|
|
1283
|
+
return [role && unquoted(role), schema ? `in ${unquoted(schema)}` : "in every schema", kind && `on ${kind.toLowerCase()}`].filter(Boolean).join(" ");
|
|
1284
|
+
}
|
|
1285
|
+
function ruleLabel(sql) {
|
|
1286
|
+
const rule = extractQualifiedName(sql, /\bRULE\s+(?:IF\s+EXISTS\s+)?/i);
|
|
1287
|
+
const table = /\bTO\s+((?:"(?:[^"]|"")*"|[\w$]+)(?:\.(?:"(?:[^"]|"")*"|[\w$]+))?)/i.exec(sql)?.[1] ?? /\bON\s+((?:"(?:[^"]|"")*"|[\w$]+)(?:\.(?:"(?:[^"]|"")*"|[\w$]+))?)/i.exec(sql)?.[1];
|
|
1288
|
+
return table ? `${unquoted(table.split(".").pop() ?? table)}.${rule.split(".").pop()}` : rule;
|
|
1289
|
+
}
|
|
1290
|
+
function castLabel(sql) {
|
|
1291
|
+
return /\(([^()]+?\s+AS\s+[^()]+?)\)/i.exec(sql)?.[1] ?? UNKNOWN_NAME;
|
|
1292
|
+
}
|
|
1293
|
+
function userMappingLabel(sql) {
|
|
1294
|
+
const user = /\bFOR\s+("(?:[^"]|"")*"|\S+)/i.exec(sql)?.[1] ?? "unknown";
|
|
1295
|
+
const server = /\bSERVER\s+("(?:[^"]|"")*"|[^\s;]+)/i.exec(sql)?.[1] ?? "unknown";
|
|
1296
|
+
return `${unquoted(user)}@${unquoted(server)}`;
|
|
1297
|
+
}
|
|
1109
1298
|
function policyLabel(sql) {
|
|
1110
1299
|
const policy = extractQualifiedName(sql, AFTER.policy);
|
|
1111
1300
|
const table = extractQualifiedName(sql, AFTER.on);
|
|
@@ -1157,6 +1346,10 @@ var SCHEMA_RULES = [
|
|
|
1157
1346
|
{ match: /^\s*CREATE\s+DOMAIN\b/i, label: "Domain missing", name: named(AFTER.domain) },
|
|
1158
1347
|
{ match: /^\s*ALTER\s+DOMAIN\b/i, label: "Domain altered", name: named(AFTER.domain) },
|
|
1159
1348
|
{ match: /^\s*DROP\s+DOMAIN\b/i, label: "Extra domain", name: named(AFTER.domain) },
|
|
1349
|
+
{ match: /^\s*ALTER\s+TABLE\b[^;]*\bRENAME\s+COLUMN\b/is, label: "Column renamed", name: renameLabel, unqualified: true },
|
|
1350
|
+
{ match: /^\s*ALTER\s+TABLE\b[^;]*\bRENAME\s+CONSTRAINT\b/is, label: "Constraint renamed", name: renameLabel, unqualified: true },
|
|
1351
|
+
{ match: /^\s*ALTER\s+TABLE\b[^;]*\bRENAME\s+TO\b/is, label: "Table renamed", name: renameLabel, unqualified: true },
|
|
1352
|
+
{ match: /^\s*ALTER\s+INDEX\b[^;]*\bRENAME\s+TO\b/is, label: "Index renamed", name: renameLabel, unqualified: true },
|
|
1160
1353
|
{ match: /^\s*ALTER\s+TABLE\b/i, label: "Table altered", name: named(AFTER.table) },
|
|
1161
1354
|
{ match: /^\s*CREATE\s+TABLE\b/i, label: "Table missing", name: named(AFTER.table) },
|
|
1162
1355
|
{ match: /^\s*DROP\s+TABLE\b/i, label: "Extra table", name: named(AFTER.table) },
|
|
@@ -1178,6 +1371,28 @@ var SCHEMA_RULES = [
|
|
|
1178
1371
|
{ match: /^\s*CREATE\s+COLLATION\b/i, label: "Collation missing", name: named(AFTER.collation) },
|
|
1179
1372
|
{ match: /^\s*ALTER\s+COLLATION\b/i, label: "Collation altered", name: named(AFTER.collation) },
|
|
1180
1373
|
{ match: /^\s*DROP\s+COLLATION\b/i, label: "Extra collation", name: named(AFTER.collation) },
|
|
1374
|
+
{ match: /^\s*ALTER\s+DEFAULT\s+PRIVILEGES\b/i, label: "Default privileges differ", name: defaultPrivilegesLabel, unqualified: true },
|
|
1375
|
+
{ match: /^\s*CREATE\s+EVENT\s+TRIGGER\b/i, label: "Event trigger missing", name: named(AFTER.eventTrigger) },
|
|
1376
|
+
{ match: /^\s*DROP\s+EVENT\s+TRIGGER\b/i, label: "Extra event trigger", name: named(AFTER.eventTrigger) },
|
|
1377
|
+
{ match: /^\s*CREATE\s+(?:OR\s+REPLACE\s+)?RULE\b/i, label: "Rule missing", name: ruleLabel },
|
|
1378
|
+
{ match: /^\s*DROP\s+RULE\b/i, label: "Extra rule", name: ruleLabel },
|
|
1379
|
+
{ match: /^\s*CREATE\s+(?:OR\s+REPLACE\s+)?AGGREGATE\b/i, label: "Aggregate missing", name: named(AFTER.aggregate) },
|
|
1380
|
+
{ match: /^\s*DROP\s+AGGREGATE\b/i, label: "Extra aggregate", name: named(AFTER.aggregate) },
|
|
1381
|
+
{ match: /^\s*CREATE\s+STATISTICS\b/i, label: "Statistics missing", name: named(AFTER.statistics) },
|
|
1382
|
+
{ match: /^\s*DROP\s+STATISTICS\b/i, label: "Extra statistics", name: named(AFTER.statistics) },
|
|
1383
|
+
{ match: /^\s*(?:CREATE|ALTER)\s+TEXT\s+SEARCH\s+CONFIGURATION\b/i, label: "Text search configuration missing", name: named(AFTER.textSearch) },
|
|
1384
|
+
{ match: /^\s*DROP\s+TEXT\s+SEARCH\s+CONFIGURATION\b/i, label: "Extra text search configuration", name: named(AFTER.textSearch) },
|
|
1385
|
+
{ match: /^\s*CREATE\s+TEXT\s+SEARCH\s+DICTIONARY\b/i, label: "Text search dictionary missing", name: named(AFTER.textSearch) },
|
|
1386
|
+
{ match: /^\s*DROP\s+TEXT\s+SEARCH\s+DICTIONARY\b/i, label: "Extra text search dictionary", name: named(AFTER.textSearch) },
|
|
1387
|
+
{ match: /^\s*CREATE\s+CAST\b/i, label: "Cast missing", name: castLabel, unqualified: true },
|
|
1388
|
+
{ match: /^\s*DROP\s+CAST\b/i, label: "Extra cast", name: castLabel, unqualified: true },
|
|
1389
|
+
{ match: /^\s*CREATE\s+SERVER\b/i, label: "Foreign server missing", name: named(AFTER.server) },
|
|
1390
|
+
{ match: /^\s*ALTER\s+SERVER\b/i, label: "Foreign server altered", name: named(AFTER.server) },
|
|
1391
|
+
{ match: /^\s*DROP\s+SERVER\b/i, label: "Extra foreign server", name: named(AFTER.server) },
|
|
1392
|
+
{ match: /^\s*(?:CREATE|ALTER)\s+USER\s+MAPPING\b/i, label: "User mapping missing", name: userMappingLabel, unqualified: true },
|
|
1393
|
+
{ match: /^\s*DROP\s+USER\s+MAPPING\b/i, label: "Extra user mapping", name: userMappingLabel, unqualified: true },
|
|
1394
|
+
{ match: /^\s*CREATE\s+FOREIGN\s+TABLE\b/i, label: "Foreign table missing", name: named(AFTER.foreignTable) },
|
|
1395
|
+
{ match: /^\s*DROP\s+FOREIGN\s+TABLE\b/i, label: "Extra foreign table", name: named(AFTER.foreignTable) },
|
|
1181
1396
|
{ match: /^\s*CREATE\s+SEQUENCE\b/i, label: "Sequence missing", name: named(AFTER.sequence) },
|
|
1182
1397
|
{ match: /^\s*ALTER\s+SEQUENCE\b/i, label: "Sequence altered", name: named(AFTER.sequence) },
|
|
1183
1398
|
{ match: /^\s*DROP\s+SEQUENCE\b/i, label: "Extra sequence", name: named(AFTER.sequence) }
|
|
@@ -1225,7 +1440,8 @@ function summariseStatement(sql, check, downSql) {
|
|
|
1225
1440
|
for (const rule of SCHEMA_RULES) {
|
|
1226
1441
|
if (rule.match.test(sql)) {
|
|
1227
1442
|
const label = rule.name === commentLabel ? commentChange(sql, downSql) : rule.label;
|
|
1228
|
-
|
|
1443
|
+
const name = rule.name(sql, downSql);
|
|
1444
|
+
return `${label}: ${rule.unqualified ? name : qualifySchemaName(name)}`;
|
|
1229
1445
|
}
|
|
1230
1446
|
}
|
|
1231
1447
|
return `Schema change: ${qualifySchemaName(
|
|
@@ -1274,23 +1490,369 @@ function extractRoutineArgs(sql) {
|
|
|
1274
1490
|
return "";
|
|
1275
1491
|
}
|
|
1276
1492
|
|
|
1493
|
+
// src/pg-errors.ts
|
|
1494
|
+
var ABSENT_ON_TARGET = /* @__PURE__ */ new Set(["42P01", "42883", "3F000", "42704"]);
|
|
1495
|
+
var UNDEFINED_COLUMN = "42703";
|
|
1496
|
+
var EXTENSION_UNAVAILABLE = /* @__PURE__ */ new Set(["0A000", "58P01"]);
|
|
1497
|
+
function sqlState(err) {
|
|
1498
|
+
const code = err?.code;
|
|
1499
|
+
return typeof code === "string" ? code : void 0;
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
// src/promote.ts
|
|
1503
|
+
function outOfScopeReason(sql, filter) {
|
|
1504
|
+
if (!isFiltered(filter)) return null;
|
|
1505
|
+
const referenced = referencedTables(sql);
|
|
1506
|
+
if (referenced.length === 0) return null;
|
|
1507
|
+
const excluded = referenced.filter((t) => applyTableFilter([t], filter).length === 0);
|
|
1508
|
+
if (excluded.length === 0) return null;
|
|
1509
|
+
const names2 = excluded.map((t) => `'${t}'`).join(", ");
|
|
1510
|
+
const flag = filter?.tables?.length ? "--tables" : "--exclude-tables";
|
|
1511
|
+
const noun = excluded.length === 1 ? "table" : "tables";
|
|
1512
|
+
return `Depends on ${noun} ${names2}, excluded by ${flag}`;
|
|
1513
|
+
}
|
|
1514
|
+
function isSelected(issueId, only) {
|
|
1515
|
+
if (!only?.length) return true;
|
|
1516
|
+
return only.some((pattern) => matchesGlob(issueId, pattern));
|
|
1517
|
+
}
|
|
1518
|
+
function withDefaultGrantRevokes(scanResult, only) {
|
|
1519
|
+
if (!only?.length) return only;
|
|
1520
|
+
const issues = scanResult.checks.flatMap((c) => c.issues);
|
|
1521
|
+
const created = new Set(issues.filter((i) => i.sql?.up && isSelected(i.id, only)).flatMap((i) => relationsCreatedBy(i.sql.up)));
|
|
1522
|
+
if (created.size === 0) return only;
|
|
1523
|
+
const companions = issues.filter((i) => i.id.startsWith("roles-grant-default-") && [...created].some((rel) => rel.endsWith("(") ? i.id.toLowerCase().includes(`.${rel}`) : i.id.toLowerCase().endsWith(`.${rel}`))).map((i) => i.id);
|
|
1524
|
+
return [...only, ...companions];
|
|
1525
|
+
}
|
|
1526
|
+
function relationsCreatedBy(sql) {
|
|
1527
|
+
const statements = parseStatements(sql);
|
|
1528
|
+
if (!statements) return [];
|
|
1529
|
+
return statements.flatMap(({ kind, node }) => {
|
|
1530
|
+
if (kind === "CreateFunctionStmt") {
|
|
1531
|
+
const parts = (node.funcname ?? []).map((n) => n.String?.sval ?? "");
|
|
1532
|
+
const name = parts.at(-1);
|
|
1533
|
+
return name ? [`${(parts.length > 1 ? parts.at(-2) : "public").toLowerCase()}.${name.toLowerCase()}(`] : [];
|
|
1534
|
+
}
|
|
1535
|
+
const rel = kind === "CreateStmt" ? node.relation : kind === "ViewStmt" ? node.view : kind === "CreateTableAsStmt" ? node.into?.rel : void 0;
|
|
1536
|
+
return rel?.relname ? [`${(rel.schemaname || "public").toLowerCase()}.${String(rel.relname).toLowerCase()}`] : [];
|
|
1537
|
+
});
|
|
1538
|
+
}
|
|
1539
|
+
function classifyIssue(issue, options, recreatedPolicies = /* @__PURE__ */ new Set()) {
|
|
1540
|
+
if (!isSelected(issue.id, options.only)) {
|
|
1541
|
+
return { kind: "skip", reason: "Not selected by --only" };
|
|
1542
|
+
}
|
|
1543
|
+
if (issue.manualOnly) return { kind: "skip", reason: issue.manualOnly };
|
|
1544
|
+
if (!issue.sql?.up) {
|
|
1545
|
+
if (issue.action?.method === "DELETE" && !options.allowDestructive) {
|
|
1546
|
+
return { kind: "skip", reason: `Destructive \u2014 ${issue.action.label}; re-run with --allow-destructive to apply` };
|
|
1547
|
+
}
|
|
1548
|
+
if (issue.action) return { kind: "api", action: issue.action };
|
|
1549
|
+
return { kind: "skip", reason: issue.manualOnly ?? "No SQL fix or API action available" };
|
|
1550
|
+
}
|
|
1551
|
+
if (!options.allowDestructive) {
|
|
1552
|
+
const why = issue.destructive ?? destructiveReason(issue.sql.up, recreatedPolicies);
|
|
1553
|
+
if (why) {
|
|
1554
|
+
return { kind: "skip", reason: `Destructive \u2014 ${why}; re-run with --allow-destructive to apply` };
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
const outOfScope = outOfScopeReason(issue.sql.up, options.tableFilter);
|
|
1558
|
+
if (outOfScope) return { kind: "skip", reason: outOfScope };
|
|
1559
|
+
return { kind: "sql", sql: issue.sql.up };
|
|
1560
|
+
}
|
|
1561
|
+
function policiesCreatedBySelectedFixes(checks, options) {
|
|
1562
|
+
const created = /* @__PURE__ */ new Set();
|
|
1563
|
+
for (const check of checks) {
|
|
1564
|
+
const askedForExplicitly = options.checks?.includes(check.check) ?? false;
|
|
1565
|
+
if (!isComparisonCheck(check.check) && !options.applyPosture && !askedForExplicitly) continue;
|
|
1566
|
+
for (const issue of check.issues) {
|
|
1567
|
+
const sql = issue.sql?.up;
|
|
1568
|
+
if (!sql || !isSelected(issue.id, options.only)) continue;
|
|
1569
|
+
if (!options.allowDestructive && destructiveReason(sql)) continue;
|
|
1570
|
+
if (outOfScopeReason(sql, options.tableFilter)) continue;
|
|
1571
|
+
for (const key of createdPolicies(sql)) created.add(key);
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
return created;
|
|
1575
|
+
}
|
|
1576
|
+
function planWork(scanResult, options = {}) {
|
|
1577
|
+
options = { ...options, only: withDefaultGrantRevokes(scanResult, options.only) };
|
|
1578
|
+
const plan = { sqlStatements: [], apiActions: [], skipped: [] };
|
|
1579
|
+
const relevant = scanResult.checks.filter(
|
|
1580
|
+
(c) => c.status === "drifted" && (!options.checks || options.checks.includes(c.check))
|
|
1581
|
+
);
|
|
1582
|
+
const recreatedPolicies = policiesCreatedBySelectedFixes(relevant, options);
|
|
1583
|
+
const keptBack = [];
|
|
1584
|
+
for (const checkResult of relevant) {
|
|
1585
|
+
const askedForExplicitly = options.checks?.includes(checkResult.check) ?? false;
|
|
1586
|
+
if (!isComparisonCheck(checkResult.check) && !options.applyPosture && !askedForExplicitly) {
|
|
1587
|
+
for (const issue of checkResult.issues) {
|
|
1588
|
+
plan.skipped.push({
|
|
1589
|
+
check: checkResult.check,
|
|
1590
|
+
issueId: issue.id,
|
|
1591
|
+
reason: "Posture finding about the target, not drift from the source \u2014 applying it would create drift. Use --apply-posture to apply anyway."
|
|
1592
|
+
});
|
|
1593
|
+
}
|
|
1594
|
+
continue;
|
|
1595
|
+
}
|
|
1596
|
+
for (const issue of checkResult.issues) {
|
|
1597
|
+
const at = { check: checkResult.check, issueId: issue.id };
|
|
1598
|
+
const outcome = classifyIssue(issue, options, recreatedPolicies);
|
|
1599
|
+
if (outcome.kind === "sql") plan.sqlStatements.push({ ...at, sql: outcome.sql });
|
|
1600
|
+
else if (outcome.kind === "api") plan.apiActions.push({ ...at, action: outcome.action });
|
|
1601
|
+
else {
|
|
1602
|
+
plan.skipped.push({ ...at, reason: outcome.reason });
|
|
1603
|
+
if (outcome.reason.startsWith("Destructive") && issue.sql?.down) {
|
|
1604
|
+
keptBack.push({ issueId: issue.id, definition: issue.sql.down });
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
plan.sqlStatements = holdBackDropsStillInUse(plan.sqlStatements, keptBack, plan.skipped, downsOf(relevant));
|
|
1610
|
+
const dbdiffOrder = new Map(plan.sqlStatements.filter((s) => s.check === "schema").map((s, i) => [s, i]));
|
|
1611
|
+
plan.sqlStatements = orderStatements(plan.sqlStatements, (s) => s.sql, (s) => dbdiffOrder.get(s));
|
|
1612
|
+
plan.sqlStatements = dropDuplicateObjectFixes(plan.sqlStatements, plan.skipped);
|
|
1613
|
+
return plan;
|
|
1614
|
+
}
|
|
1615
|
+
var DROPS_SUPPORTING_OBJECT = /^\s*DROP\s+(?:TYPE|DOMAIN|FUNCTION|PROCEDURE|ROUTINE|SEQUENCE)\s+(?:IF\s+EXISTS\s+)?((?:"[^"]+"|[\w$]+)(?:\s*\.\s*(?:"[^"]+"|[\w$]+))?)/i;
|
|
1616
|
+
function holdBackDropsStillInUse(statements, keptBack, skipped, downs = /* @__PURE__ */ new Map()) {
|
|
1617
|
+
const kept = keptBack.map((k) => ({ issueId: k.issueId, skeleton: sqlSkeleton(k.definition) }));
|
|
1618
|
+
let remaining = statements;
|
|
1619
|
+
for (let changed = kept.length > 0; changed; ) {
|
|
1620
|
+
changed = false;
|
|
1621
|
+
remaining = remaining.filter((statement) => {
|
|
1622
|
+
const names2 = droppedSupportingObjects(statement.sql);
|
|
1623
|
+
if (names2.length === 0) return true;
|
|
1624
|
+
const user = kept.find((k) => names2.some((n) => identifierMatcher(bareName(n)).test(k.skeleton)));
|
|
1625
|
+
if (!user) return true;
|
|
1626
|
+
skipped.push({
|
|
1627
|
+
check: statement.check,
|
|
1628
|
+
issueId: statement.issueId,
|
|
1629
|
+
reason: `Still used by what ${user.issueId} keeps, which is held back as destructive; re-run with --allow-destructive to apply both`
|
|
1630
|
+
});
|
|
1631
|
+
const definition = downs.get(statement.issueId);
|
|
1632
|
+
if (definition) kept.push({ issueId: user.issueId, skeleton: sqlSkeleton(definition) });
|
|
1633
|
+
changed = true;
|
|
1634
|
+
return false;
|
|
1635
|
+
});
|
|
1636
|
+
}
|
|
1637
|
+
return remaining;
|
|
1638
|
+
}
|
|
1639
|
+
function droppedSupportingObjects(sql) {
|
|
1640
|
+
const parts = splitSqlStatements(sql).filter((s) => !isCommentOnly(s));
|
|
1641
|
+
const names2 = parts.map((s) => DROPS_SUPPORTING_OBJECT.exec(sqlSkeleton(s))?.[1]);
|
|
1642
|
+
return names2.length > 0 && names2.every((n) => n !== void 0) ? names2 : [];
|
|
1643
|
+
}
|
|
1644
|
+
function downsOf(checks) {
|
|
1645
|
+
const downs = /* @__PURE__ */ new Map();
|
|
1646
|
+
for (const check of checks) {
|
|
1647
|
+
for (const issue of check.issues) {
|
|
1648
|
+
if (issue.sql?.down) downs.set(issue.id, issue.sql.down);
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
return downs;
|
|
1652
|
+
}
|
|
1653
|
+
function dropDuplicateObjectFixes(statements, skipped) {
|
|
1654
|
+
const kinds = [
|
|
1655
|
+
{ what: "policy", created: createdPolicies, onlyThis: createsOnlyPolicies, seen: /* @__PURE__ */ new Set() },
|
|
1656
|
+
{ what: "trigger", created: createdTriggers, onlyThis: createsOnlyTriggers, seen: /* @__PURE__ */ new Set() }
|
|
1657
|
+
];
|
|
1658
|
+
const kept = [];
|
|
1659
|
+
for (const statement of statements) {
|
|
1660
|
+
const duplicate = kinds.find((kind) => {
|
|
1661
|
+
const names2 = kind.created(statement.sql);
|
|
1662
|
+
return names2.length > 0 && names2.every((n) => kind.seen.has(n)) && kind.onlyThis(statement.sql);
|
|
1663
|
+
});
|
|
1664
|
+
if (duplicate) {
|
|
1665
|
+
skipped.push({
|
|
1666
|
+
check: statement.check,
|
|
1667
|
+
issueId: statement.issueId,
|
|
1668
|
+
reason: `Already created by the schema fix for the same ${duplicate.what}`
|
|
1669
|
+
});
|
|
1670
|
+
continue;
|
|
1671
|
+
}
|
|
1672
|
+
for (const kind of kinds) {
|
|
1673
|
+
for (const name of kind.created(statement.sql)) kind.seen.add(name);
|
|
1674
|
+
}
|
|
1675
|
+
kept.push(statement);
|
|
1676
|
+
}
|
|
1677
|
+
return kept;
|
|
1678
|
+
}
|
|
1679
|
+
async function executeSql(dbUrl, statements, result, transactional) {
|
|
1680
|
+
if (statements.length === 0) return;
|
|
1681
|
+
const client = newPgClient(dbUrl);
|
|
1682
|
+
await client.connect();
|
|
1683
|
+
try {
|
|
1684
|
+
if (transactional) await runInTransaction(client, statements, result);
|
|
1685
|
+
else await runIndependently(client, statements, result);
|
|
1686
|
+
} finally {
|
|
1687
|
+
await client.end();
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
async function runLikeApply(dbUrl, statements) {
|
|
1691
|
+
const result = { applied: [], skipped: [], errors: [] };
|
|
1692
|
+
await executeSql(dbUrl, statements, result, true);
|
|
1693
|
+
return result;
|
|
1694
|
+
}
|
|
1695
|
+
async function runIndependently(client, statements, result) {
|
|
1696
|
+
for (const stmt of statements) {
|
|
1697
|
+
try {
|
|
1698
|
+
await client.query(stmt.sql);
|
|
1699
|
+
result.applied.push({ check: stmt.check, issueId: stmt.issueId, sql: stmt.sql });
|
|
1700
|
+
} catch (err) {
|
|
1701
|
+
result.errors.push({ check: stmt.check, issueId: stmt.issueId, error: errMsg(err) });
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
function isEnumValueAddition(sql) {
|
|
1706
|
+
const statements = splitSqlStatements(sql).filter((s) => !isCommentOnly(s));
|
|
1707
|
+
return statements.length > 0 && statements.every((s) => /^\s*ALTER\s+TYPE\s+\S+\s+ADD\s+VALUE\b/i.test(sqlSkeleton(s)));
|
|
1708
|
+
}
|
|
1709
|
+
async function runInTransaction(client, statements, result) {
|
|
1710
|
+
const additions = statements.filter((s) => isEnumValueAddition(s.sql));
|
|
1711
|
+
const rest = statements.filter((s) => !isEnumValueAddition(s.sql));
|
|
1712
|
+
if (additions.length > 0 && !await runBatch(client, additions, result)) return;
|
|
1713
|
+
await runBatch(client, rest, result);
|
|
1714
|
+
}
|
|
1715
|
+
function unmetDependency(sql, err) {
|
|
1716
|
+
const code = sqlState(err);
|
|
1717
|
+
if (!code) return void 0;
|
|
1718
|
+
if (/^\s*CREATE\s+EXTENSION\b/i.test(sqlSkeleton(sql))) {
|
|
1719
|
+
return EXTENSION_UNAVAILABLE.has(code) ? "this server does not ship it" : void 0;
|
|
1720
|
+
}
|
|
1721
|
+
if (ABSENT_ON_TARGET.has(code) || code === UNDEFINED_COLUMN) {
|
|
1722
|
+
return "it needs something the target does not have";
|
|
1723
|
+
}
|
|
1724
|
+
return void 0;
|
|
1725
|
+
}
|
|
1726
|
+
async function runBatch(client, statements, result) {
|
|
1727
|
+
if (statements.length === 0) return true;
|
|
1728
|
+
const done = [];
|
|
1729
|
+
await client.query("BEGIN");
|
|
1730
|
+
let pending = statements;
|
|
1731
|
+
let unmet = [];
|
|
1732
|
+
while (pending.length > 0) {
|
|
1733
|
+
unmet = [];
|
|
1734
|
+
for (const stmt of pending) {
|
|
1735
|
+
await client.query("SAVEPOINT sf_fix");
|
|
1736
|
+
try {
|
|
1737
|
+
await client.query(stmt.sql);
|
|
1738
|
+
await client.query("RELEASE SAVEPOINT sf_fix");
|
|
1739
|
+
done.push({ check: stmt.check, issueId: stmt.issueId, sql: stmt.sql });
|
|
1740
|
+
} catch (err) {
|
|
1741
|
+
const why = unmetDependency(stmt.sql, err);
|
|
1742
|
+
if (why && await client.query("ROLLBACK TO SAVEPOINT sf_fix").then(() => true, () => false)) {
|
|
1743
|
+
unmet.push({ stmt, err, why });
|
|
1744
|
+
continue;
|
|
1745
|
+
}
|
|
1746
|
+
await client.query("ROLLBACK").catch(() => {
|
|
1747
|
+
});
|
|
1748
|
+
result.errors.push({ check: stmt.check, issueId: stmt.issueId, error: errMsg(err) });
|
|
1749
|
+
result.rolledBack = [...result.rolledBack ?? [], ...done];
|
|
1750
|
+
return false;
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
if (unmet.length === pending.length) break;
|
|
1754
|
+
pending = unmet.map((u) => u.stmt);
|
|
1755
|
+
}
|
|
1756
|
+
await client.query("COMMIT");
|
|
1757
|
+
result.applied.push(...done);
|
|
1758
|
+
for (const { stmt, err, why } of unmet) {
|
|
1759
|
+
result.errors.push({
|
|
1760
|
+
check: stmt.check,
|
|
1761
|
+
issueId: stmt.issueId,
|
|
1762
|
+
error: `${errMsg(err)} \u2014 not applied: ${why}. The other fixes were.`
|
|
1763
|
+
});
|
|
1764
|
+
}
|
|
1765
|
+
return true;
|
|
1766
|
+
}
|
|
1767
|
+
async function executeApiActions(actions, fetchFn, result) {
|
|
1768
|
+
for (const act of actions) {
|
|
1769
|
+
try {
|
|
1770
|
+
const init = {
|
|
1771
|
+
method: act.action.method,
|
|
1772
|
+
headers: { "Content-Type": "application/json", ...act.action.headers }
|
|
1773
|
+
};
|
|
1774
|
+
if (act.action.body !== void 0) {
|
|
1775
|
+
init.body = JSON.stringify(act.action.body);
|
|
1776
|
+
}
|
|
1777
|
+
const res = await fetchFn(act.action.url, init);
|
|
1778
|
+
if (!res.ok) {
|
|
1779
|
+
const text = await res.text().catch(() => res.statusText);
|
|
1780
|
+
throw new Error(`${act.action.method} ${act.action.url} \u2192 ${res.status}: ${text}`);
|
|
1781
|
+
}
|
|
1782
|
+
result.applied.push({ check: act.check, issueId: act.issueId, action: act.action.label });
|
|
1783
|
+
} catch (err) {
|
|
1784
|
+
result.errors.push({ check: act.check, issueId: act.issueId, error: errMsg(err) });
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
async function promote(options) {
|
|
1789
|
+
const {
|
|
1790
|
+
dbUrl,
|
|
1791
|
+
scanResult,
|
|
1792
|
+
checks,
|
|
1793
|
+
dryRun = false,
|
|
1794
|
+
allowDestructive = false,
|
|
1795
|
+
tableFilter,
|
|
1796
|
+
only,
|
|
1797
|
+
applyPosture = false,
|
|
1798
|
+
transactional = true,
|
|
1799
|
+
fetchFn = globalThis.fetch.bind(globalThis)
|
|
1800
|
+
} = options;
|
|
1801
|
+
const { sqlStatements, apiActions, skipped } = planWork(scanResult, {
|
|
1802
|
+
checks,
|
|
1803
|
+
allowDestructive,
|
|
1804
|
+
tableFilter,
|
|
1805
|
+
only,
|
|
1806
|
+
applyPosture
|
|
1807
|
+
});
|
|
1808
|
+
const result = { applied: [], skipped, errors: [] };
|
|
1809
|
+
if (dryRun) {
|
|
1810
|
+
for (const stmt of sqlStatements) {
|
|
1811
|
+
result.applied.push({ check: stmt.check, issueId: stmt.issueId, sql: stmt.sql });
|
|
1812
|
+
}
|
|
1813
|
+
for (const act of apiActions) {
|
|
1814
|
+
result.applied.push({ check: act.check, issueId: act.issueId, action: act.action.label });
|
|
1815
|
+
}
|
|
1816
|
+
return result;
|
|
1817
|
+
}
|
|
1818
|
+
await executeSql(dbUrl, sqlStatements, result, transactional);
|
|
1819
|
+
if (result.rolledBack) {
|
|
1820
|
+
for (const act of apiActions) {
|
|
1821
|
+
result.skipped.push({
|
|
1822
|
+
check: act.check,
|
|
1823
|
+
issueId: act.issueId,
|
|
1824
|
+
reason: "Not attempted \u2014 the SQL fixes were rolled back"
|
|
1825
|
+
});
|
|
1826
|
+
}
|
|
1827
|
+
return result;
|
|
1828
|
+
}
|
|
1829
|
+
await executeApiActions(apiActions, fetchFn, result);
|
|
1830
|
+
return result;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1277
1833
|
export {
|
|
1278
1834
|
parseStatements,
|
|
1835
|
+
relationName,
|
|
1279
1836
|
sqlSkeleton,
|
|
1280
|
-
bareName,
|
|
1281
|
-
createdPolicies,
|
|
1282
|
-
createsOnlyPolicies,
|
|
1283
|
-
createdTriggers,
|
|
1284
|
-
createsOnlyTriggers,
|
|
1285
|
-
referencedTables,
|
|
1286
|
-
identifierMatcher,
|
|
1287
|
-
orderStatements,
|
|
1288
1837
|
statementSubject,
|
|
1289
1838
|
policyOnlyKeys,
|
|
1290
1839
|
rolesNamedBy,
|
|
1840
|
+
resolveTableFilter,
|
|
1841
|
+
isFiltered,
|
|
1842
|
+
describeTableFilter,
|
|
1843
|
+
applyTableFilter,
|
|
1844
|
+
CHECK_NAMES,
|
|
1845
|
+
CHECK_META,
|
|
1846
|
+
isComparisonCheck,
|
|
1291
1847
|
runDbDiff,
|
|
1292
1848
|
isDestructiveSql,
|
|
1293
1849
|
destructiveReason,
|
|
1294
|
-
sqlToIssues
|
|
1850
|
+
sqlToIssues,
|
|
1851
|
+
schemaNotices,
|
|
1852
|
+
ABSENT_ON_TARGET,
|
|
1853
|
+
EXTENSION_UNAVAILABLE,
|
|
1854
|
+
planWork,
|
|
1855
|
+
runLikeApply,
|
|
1856
|
+
promote
|
|
1295
1857
|
};
|
|
1296
|
-
//# sourceMappingURL=chunk-
|
|
1858
|
+
//# sourceMappingURL=chunk-T7GMPGUO.js.map
|