@akal/supaforge 0.0.34 → 0.0.36
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 +32 -13
- package/dist/base-command.d.ts +1 -1
- package/dist/base-command.js +4 -4
- package/dist/{branch-C4ndfoj5.d.ts → branch-BppjQBv_.d.ts} +1 -1
- package/dist/{chunk-ICSCHS72.js → chunk-5UHIWFQG.js} +76 -186
- package/dist/{chunk-S5L4A3UR.js → chunk-AL6INEFX.js} +3 -3
- package/dist/{chunk-OB5GP7UB.js → chunk-CAQ3QQFP.js} +437 -45
- package/dist/{chunk-TPX2MAO4.js → chunk-CCB7ARZU.js} +54 -5
- package/dist/{chunk-V2SBZERO.js → chunk-EWNZIRCD.js} +4 -4
- package/dist/{chunk-EFKQOQGG.js → chunk-FD2JKEJA.js} +7 -5
- package/dist/{chunk-3WKWPSFN.js → chunk-GGZ7YWBZ.js} +191 -32
- package/dist/{chunk-F46FFUKC.js → chunk-JX5L253P.js} +56 -18
- package/dist/{chunk-GQPGH6YR.js → chunk-KHXEES2M.js} +2 -2
- package/dist/{chunk-MG3SFV5J.js → chunk-KT7NMELZ.js} +77 -7
- package/dist/{chunk-PYAGSMFB.js → chunk-M2564IGT.js} +159 -19
- package/dist/{chunk-7DL63NWP.js → chunk-MRXIE55Y.js} +7 -3
- package/dist/{chunk-6ERVCWLY.js → chunk-MZPWRWBL.js} +7 -7
- package/dist/{chunk-5STH2A6K.js → chunk-OXS42LJH.js} +4 -4
- package/dist/{chunk-HNJ6BNNZ.js → chunk-Q43MN6NE.js} +2 -2
- package/dist/{chunk-ZI435CAU.js → chunk-RQCXGO37.js} +2 -2
- package/dist/{chunk-KNXHLVF7.js → chunk-SQ52YBKK.js} +3 -2
- package/dist/{chunk-N5VNNOA2.js → chunk-YUJ2MHLM.js} +1236 -22
- package/dist/chunk-Z2YUOA4W.js +242 -0
- package/dist/commands/clone.d.ts +2 -2
- package/dist/commands/clone.js +10 -9
- package/dist/commands/diff.d.ts +25 -22
- package/dist/commands/diff.js +14 -15
- package/dist/commands/hukam.d.ts +2 -3
- package/dist/commands/hukam.js +14 -15
- package/dist/commands/init.d.ts +1 -1
- package/dist/commands/init.js +2 -2
- package/dist/commands/mcp.js +9 -10
- package/dist/commands/migrate/baseline.d.ts +1 -1
- package/dist/commands/migrate/baseline.js +5 -5
- package/dist/commands/migrate/create.d.ts +1 -1
- package/dist/commands/migrate/create.js +27 -13
- package/dist/commands/migrate/list.d.ts +1 -1
- package/dist/commands/migrate/list.js +5 -5
- package/dist/commands/migrate/run.d.ts +4 -1
- package/dist/commands/migrate/run.js +9 -8
- package/dist/commands/report.d.ts +1 -1
- package/dist/commands/report.js +5 -5
- package/dist/commands/restore.d.ts +1 -1
- package/dist/commands/restore.js +14 -13
- package/dist/commands/snapshot.d.ts +1 -1
- package/dist/commands/snapshot.js +8 -8
- package/dist/commands/sync.d.ts +2 -3
- package/dist/commands/sync.js +14 -15
- package/dist/{config-CTbeRlJk.d.ts → config-l3ggjw9r.d.ts} +2 -0
- package/dist/index.d.ts +38 -4
- package/dist/index.js +13 -14
- package/package.json +2 -2
- package/dist/chunk-DQJSSTIJ.js +0 -940
- package/dist/chunk-DQQ5E563.js +0 -64
|
@@ -3,27 +3,50 @@ import {
|
|
|
3
3
|
getAppliedVersions,
|
|
4
4
|
getPendingMigrations,
|
|
5
5
|
runMigration
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-AL6INEFX.js";
|
|
7
|
+
import {
|
|
8
|
+
comparedSchemas,
|
|
9
|
+
fingerprint
|
|
10
|
+
} from "./chunk-GGZ7YWBZ.js";
|
|
7
11
|
import {
|
|
8
12
|
isDestructiveSql
|
|
9
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-YUJ2MHLM.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-OXS42LJH.js";
|
|
16
20
|
import {
|
|
21
|
+
DEFAULT_IGNORE_SCHEMAS,
|
|
17
22
|
bold,
|
|
18
23
|
dim,
|
|
19
24
|
errMsg,
|
|
20
25
|
ok,
|
|
21
26
|
warn
|
|
22
|
-
} from "./chunk-
|
|
27
|
+
} from "./chunk-SQ52YBKK.js";
|
|
23
28
|
|
|
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-CCB7ARZU.js.map
|
|
@@ -6,14 +6,14 @@ import {
|
|
|
6
6
|
captureSnapshot,
|
|
7
7
|
findLatestSnapshot,
|
|
8
8
|
loadSnapshot
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-5UHIWFQG.js";
|
|
10
10
|
import {
|
|
11
11
|
slugify
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-Z2YUOA4W.js";
|
|
13
13
|
import {
|
|
14
14
|
MIGRATIONS_SUBDIR,
|
|
15
15
|
SUPAFORGE_DIR
|
|
16
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-SQ52YBKK.js";
|
|
17
17
|
|
|
18
18
|
// src/migration.ts
|
|
19
19
|
import { mkdir, writeFile, readFile, readdir } from "fs/promises";
|
|
@@ -245,4 +245,4 @@ export {
|
|
|
245
245
|
loadMigrations,
|
|
246
246
|
listMigrationFiles
|
|
247
247
|
};
|
|
248
|
-
//# sourceMappingURL=chunk-
|
|
248
|
+
//# sourceMappingURL=chunk-EWNZIRCD.js.map
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import {
|
|
2
|
-
captureSnapshot
|
|
2
|
+
captureSnapshot
|
|
3
|
+
} from "./chunk-5UHIWFQG.js";
|
|
4
|
+
import {
|
|
3
5
|
checkPgDumpCompat
|
|
4
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-Z2YUOA4W.js";
|
|
5
7
|
import {
|
|
6
8
|
newPgClient,
|
|
7
9
|
pgQuery,
|
|
8
10
|
quoteName
|
|
9
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-KT7NMELZ.js";
|
|
10
12
|
import {
|
|
11
13
|
BRANCHES_FILE,
|
|
12
14
|
CLONE_PROGRESS_INTERVAL_MS,
|
|
13
15
|
PG_PIPELINE_TIMEOUT_MS,
|
|
14
16
|
SUPABASE_PLATFORM_SCHEMAS
|
|
15
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-SQ52YBKK.js";
|
|
16
18
|
|
|
17
19
|
// src/branch.ts
|
|
18
20
|
import { spawn } from "child_process";
|
|
@@ -638,4 +640,4 @@ export {
|
|
|
638
640
|
isCloneDatabase,
|
|
639
641
|
sourceLooksLikeCloneOf
|
|
640
642
|
};
|
|
641
|
-
//# sourceMappingURL=chunk-
|
|
643
|
+
//# sourceMappingURL=chunk-FD2JKEJA.js.map
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
2
|
+
parseStatements,
|
|
3
|
+
relationName,
|
|
5
4
|
rolesNamedBy,
|
|
5
|
+
runLikeApply,
|
|
6
6
|
sqlSkeleton
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-YUJ2MHLM.js";
|
|
8
8
|
import {
|
|
9
9
|
isCommentOnly,
|
|
10
10
|
splitSqlStatements
|
|
@@ -12,23 +12,32 @@ import {
|
|
|
12
12
|
import {
|
|
13
13
|
describeMissingPgDump,
|
|
14
14
|
getServerMajorVersion,
|
|
15
|
+
globToRegExp,
|
|
15
16
|
resolvePgDumpPath
|
|
16
|
-
} from "./chunk-
|
|
17
|
-
import {
|
|
18
|
-
globToRegExp
|
|
19
|
-
} from "./chunk-DQQ5E563.js";
|
|
17
|
+
} from "./chunk-Z2YUOA4W.js";
|
|
20
18
|
import {
|
|
21
19
|
pgQuery,
|
|
20
|
+
quoteIdent,
|
|
21
|
+
quoteLiteral,
|
|
22
22
|
quoteName
|
|
23
|
-
} from "./chunk-
|
|
23
|
+
} from "./chunk-KT7NMELZ.js";
|
|
24
24
|
|
|
25
25
|
// src/prove.ts
|
|
26
26
|
import { randomBytes } from "crypto";
|
|
27
|
-
import { execFile, spawn } from "child_process";
|
|
27
|
+
import { execFile, spawn as spawn2 } from "child_process";
|
|
28
28
|
import { promisify } from "util";
|
|
29
29
|
import { fingerprintSql, stateSql } from "@akal/pg-conformance";
|
|
30
30
|
|
|
31
|
+
// src/expression-forms.ts
|
|
32
|
+
var LITERAL = `'(?:[^']|'')*'(?:::[a-z_][\\w ]*(?:\\(\\d+(?:,\\s*\\d+)?\\))?)?`;
|
|
33
|
+
var CAST_ARRAY = new RegExp(`\\(ARRAY\\[(${LITERAL}(?:, ${LITERAL})*)\\]\\)::([a-z_][\\w ]*?(?:\\(\\d+(?:,\\s*\\d+)?\\))?)\\[\\]`, "g");
|
|
34
|
+
var ELEMENT = new RegExp(LITERAL, "g");
|
|
35
|
+
function canonicalArrays(text) {
|
|
36
|
+
return text.replace(CAST_ARRAY, (_match, elements, type) => `ARRAY[${(elements.match(ELEMENT) ?? []).map((e) => `(${e})::${type}`).join(", ")}]`);
|
|
37
|
+
}
|
|
38
|
+
|
|
31
39
|
// src/state-diff.ts
|
|
40
|
+
var canonical = (d) => typeof d === "string" ? canonicalArrays(d) : d;
|
|
32
41
|
var show = (v) => {
|
|
33
42
|
if (v === null || v === void 0) return "none";
|
|
34
43
|
if (typeof v === "boolean") return v ? "yes" : "no";
|
|
@@ -41,15 +50,15 @@ function compareField(out, subject, label, before, after) {
|
|
|
41
50
|
const a = show(before), b = show(after);
|
|
42
51
|
if (a !== b) out.push(`${subject}: ${label} ${a} \u2192 ${b}`);
|
|
43
52
|
}
|
|
44
|
-
function compareKeyed(out, kind, before, after, keyOf, compare) {
|
|
53
|
+
function compareKeyed(out, kind, before, after, keyOf, compare, note = () => "") {
|
|
45
54
|
const a = new Map((before ?? []).map((x) => [keyOf(x), x]));
|
|
46
55
|
const b = new Map((after ?? []).map((x) => [keyOf(x), x]));
|
|
47
56
|
for (const [key, item] of a) {
|
|
48
57
|
if (!b.has(key)) out.push(`${kind} ${key}: missing`);
|
|
49
58
|
else compare(out, `${kind} ${key}`, item, b.get(key));
|
|
50
59
|
}
|
|
51
|
-
for (const key of b
|
|
52
|
-
if (!a.has(key)) out.push(`${kind} ${key}: unexpected`);
|
|
60
|
+
for (const [key, item] of b) {
|
|
61
|
+
if (!a.has(key)) out.push(`${kind} ${key}: unexpected${note(item)}`);
|
|
53
62
|
}
|
|
54
63
|
}
|
|
55
64
|
function compareColumn(out, subject, a, b) {
|
|
@@ -89,9 +98,9 @@ function compareTable(out, subject, a, b) {
|
|
|
89
98
|
`constraint on ${prefix}`,
|
|
90
99
|
a.constraints,
|
|
91
100
|
b.constraints,
|
|
92
|
-
(c) => c.name,
|
|
101
|
+
(c) => c.type === "not_null" ? c.definition : c.name,
|
|
93
102
|
(o, s, x, y) => {
|
|
94
|
-
compareField(o, s, "definition", x.definition, y.definition);
|
|
103
|
+
compareField(o, s, "definition", canonical(x.definition), canonical(y.definition));
|
|
95
104
|
compareField(o, s, "validated", x.validated, y.validated);
|
|
96
105
|
compareField(o, s, "deferrable", x.deferrable, y.deferrable);
|
|
97
106
|
compareField(o, s, "deferred", x.deferred, y.deferred);
|
|
@@ -103,7 +112,7 @@ function compareTable(out, subject, a, b) {
|
|
|
103
112
|
a.indexes,
|
|
104
113
|
b.indexes,
|
|
105
114
|
(i) => i.name,
|
|
106
|
-
(o, s, x, y) => compareField(o, s, "definition", x.definition, y.definition)
|
|
115
|
+
(o, s, x, y) => compareField(o, s, "definition", canonical(x.definition), canonical(y.definition))
|
|
107
116
|
);
|
|
108
117
|
compareKeyed(
|
|
109
118
|
out,
|
|
@@ -115,8 +124,8 @@ function compareTable(out, subject, a, b) {
|
|
|
115
124
|
compareField(o, s, "command", x.command, y.command);
|
|
116
125
|
compareField(o, s, "permissive", x.permissive, y.permissive);
|
|
117
126
|
compareField(o, s, "roles", x.roles, y.roles);
|
|
118
|
-
compareField(o, s, "using", x.using, y.using);
|
|
119
|
-
compareField(o, s, "with check", x.with_check, y.with_check);
|
|
127
|
+
compareField(o, s, "using", canonical(x.using), canonical(y.using));
|
|
128
|
+
compareField(o, s, "with check", canonical(x.with_check), canonical(y.with_check));
|
|
120
129
|
}
|
|
121
130
|
);
|
|
122
131
|
compareKeyed(
|
|
@@ -125,7 +134,7 @@ function compareTable(out, subject, a, b) {
|
|
|
125
134
|
a.triggers,
|
|
126
135
|
b.triggers,
|
|
127
136
|
(t) => t.name,
|
|
128
|
-
(o, s, x, y) => compareField(o, s, "definition", x.definition, y.definition)
|
|
137
|
+
(o, s, x, y) => compareField(o, s, "definition", canonical(x.definition), canonical(y.definition))
|
|
129
138
|
);
|
|
130
139
|
}
|
|
131
140
|
function diffState(before, after) {
|
|
@@ -146,7 +155,7 @@ function diffState(before, after) {
|
|
|
146
155
|
(v) => qualify(v.schema, v.name),
|
|
147
156
|
(o, s, a, b) => {
|
|
148
157
|
compareField(o, s, "materialized", a.materialized, b.materialized);
|
|
149
|
-
compareField(o, s, "definition", a.definition, b.definition);
|
|
158
|
+
compareField(o, s, "definition", canonical(a.definition), canonical(b.definition));
|
|
150
159
|
compareField(o, s, "options", a.options, b.options);
|
|
151
160
|
}
|
|
152
161
|
);
|
|
@@ -160,7 +169,9 @@ function diffState(before, after) {
|
|
|
160
169
|
for (const k of ["type", "start", "increment", "min", "max", "cache", "cycle", "owned_by"]) {
|
|
161
170
|
compareField(o, s, k.replace("_", " "), a[k], b[k]);
|
|
162
171
|
}
|
|
163
|
-
}
|
|
172
|
+
},
|
|
173
|
+
// A serial or identity column's: it goes with its table.
|
|
174
|
+
(q) => q.owned_by ? ` (owned by ${q.owned_by})` : ""
|
|
164
175
|
);
|
|
165
176
|
compareKeyed(
|
|
166
177
|
out,
|
|
@@ -170,7 +181,7 @@ function diffState(before, after) {
|
|
|
170
181
|
(r) => `${qualify(r.schema, r.name)}(${r.arguments})`,
|
|
171
182
|
(o, s, a, b) => {
|
|
172
183
|
compareField(o, s, "kind", a.kind, b.kind);
|
|
173
|
-
compareField(o, s, "definition", a.definition, b.definition);
|
|
184
|
+
compareField(o, s, "definition", canonical(a.definition), canonical(b.definition));
|
|
174
185
|
}
|
|
175
186
|
);
|
|
176
187
|
compareKeyed(
|
|
@@ -202,6 +213,132 @@ function diffState(before, after) {
|
|
|
202
213
|
|
|
203
214
|
// src/prove.ts
|
|
204
215
|
import { join, dirname } from "path";
|
|
216
|
+
|
|
217
|
+
// src/prove-data.ts
|
|
218
|
+
import { spawn } from "child_process";
|
|
219
|
+
var DEFAULT_SAMPLE_ROWS = 1e3;
|
|
220
|
+
function sampleRows() {
|
|
221
|
+
const raw = process.env.SUPAFORGE_PROVE_ROWS;
|
|
222
|
+
const n = raw === void 0 ? DEFAULT_SAMPLE_ROWS : Number(raw);
|
|
223
|
+
return Number.isInteger(n) && n >= 0 ? n : DEFAULT_SAMPLE_ROWS;
|
|
224
|
+
}
|
|
225
|
+
var TABLES_SQL = `
|
|
226
|
+
SELECT n.nspname || '.' || c.relname AS name, quote_ident(n.nspname) || '.' || quote_ident(c.relname) AS ident,
|
|
227
|
+
(SELECT string_agg(quote_ident(a.attname), ', ' ORDER BY a.attnum) FROM pg_attribute a
|
|
228
|
+
WHERE a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped AND a.attgenerated = '') AS columns
|
|
229
|
+
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
230
|
+
WHERE c.relkind = 'r' AND NOT c.relispartition AND n.nspname = ANY($1::text[])
|
|
231
|
+
ORDER BY 1`;
|
|
232
|
+
async function sampleInto(psql, fromUrl, intoUrl, schemas, limit) {
|
|
233
|
+
const tables = await pgQuery(fromUrl, TABLES_SQL, [schemas]);
|
|
234
|
+
const sampled = [];
|
|
235
|
+
const unchecked = [];
|
|
236
|
+
for (const t of tables) {
|
|
237
|
+
if (!t.columns) continue;
|
|
238
|
+
try {
|
|
239
|
+
await pipeCopy(
|
|
240
|
+
psql,
|
|
241
|
+
fromUrl,
|
|
242
|
+
intoUrl,
|
|
243
|
+
`\\copy (SELECT ${t.columns} FROM ${t.ident} LIMIT ${limit}) TO STDOUT`,
|
|
244
|
+
`\\copy ${t.ident} (${t.columns}) FROM STDIN`
|
|
245
|
+
);
|
|
246
|
+
sampled.push(t.name);
|
|
247
|
+
} catch {
|
|
248
|
+
unchecked.push(t.name);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return { sampled, unchecked };
|
|
252
|
+
}
|
|
253
|
+
function pipeCopy(psql, fromUrl, intoUrl, out, into) {
|
|
254
|
+
return new Promise((resolve, reject) => {
|
|
255
|
+
const reader = spawn(psql, [fromUrl, "-X", "-q", "-v", "ON_ERROR_STOP=1", "-c", out], { stdio: ["ignore", "pipe", "pipe"] });
|
|
256
|
+
const writer = spawn(psql, [intoUrl, "-X", "-q", "-v", "ON_ERROR_STOP=1", "-f", "-"], { stdio: ["pipe", "pipe", "pipe"] });
|
|
257
|
+
let stderr = "";
|
|
258
|
+
reader.stderr.on("data", (d) => {
|
|
259
|
+
stderr += d.toString();
|
|
260
|
+
});
|
|
261
|
+
writer.stderr.on("data", (d) => {
|
|
262
|
+
stderr += d.toString();
|
|
263
|
+
});
|
|
264
|
+
reader.on("error", reject);
|
|
265
|
+
writer.on("error", reject);
|
|
266
|
+
writer.stdin.on("error", () => void 0);
|
|
267
|
+
writer.stdin.write(`\\set ON_ERROR_STOP 0
|
|
268
|
+
SET session_replication_role = replica;
|
|
269
|
+
\\set ON_ERROR_STOP 1
|
|
270
|
+
${into}
|
|
271
|
+
`);
|
|
272
|
+
reader.stdout.pipe(writer.stdin);
|
|
273
|
+
let done = 0;
|
|
274
|
+
let failed = false;
|
|
275
|
+
const finish = (code) => {
|
|
276
|
+
if (code !== 0) failed = true;
|
|
277
|
+
if (++done === 2) failed ? reject(new Error(stderr.trim() || "copy failed")) : resolve();
|
|
278
|
+
};
|
|
279
|
+
reader.on("close", finish);
|
|
280
|
+
writer.on("close", finish);
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
async function snapshotTables(dbUrl, schemas) {
|
|
284
|
+
const rows = await pgQuery(dbUrl, `
|
|
285
|
+
SELECT n.nspname || '.' || c.relname AS name, quote_ident(n.nspname) || '.' || quote_ident(c.relname) AS ident,
|
|
286
|
+
json_agg(json_build_array(a.attname, format_type(a.atttypid, a.atttypmod)) ORDER BY a.attnum) AS columns
|
|
287
|
+
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
288
|
+
JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped AND a.attgenerated = ''
|
|
289
|
+
WHERE c.relkind = 'r' AND NOT c.relispartition AND n.nspname = ANY($1::text[])
|
|
290
|
+
GROUP BY 1, 2 ORDER BY 1`, [schemas]);
|
|
291
|
+
const out = /* @__PURE__ */ new Map();
|
|
292
|
+
for (const t of rows) {
|
|
293
|
+
const hashes = t.columns.map(([col], i) => {
|
|
294
|
+
const value = `coalesce(md5(x.${quoteIdent(col)}::text), 'n')`;
|
|
295
|
+
return `coalesce(md5(string_agg(${value}, '' ORDER BY ${value})), '') AS h${i}`;
|
|
296
|
+
});
|
|
297
|
+
const [row] = await pgQuery(
|
|
298
|
+
dbUrl,
|
|
299
|
+
`SELECT count(*)::text AS rows, pg_relation_filenode(${quoteLiteral(t.ident)}::regclass)::text AS filenode${hashes.length ? ", " + hashes.join(", ") : ""}
|
|
300
|
+
FROM ${t.ident} x`
|
|
301
|
+
);
|
|
302
|
+
out.set(t.name, {
|
|
303
|
+
rows: Number(row.rows),
|
|
304
|
+
filenode: row.filenode,
|
|
305
|
+
columns: Object.fromEntries(t.columns.map(([col, type], i) => [col, { type, hash: row[`h${i}`] }]))
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
return out;
|
|
309
|
+
}
|
|
310
|
+
function emptiedTables(sql) {
|
|
311
|
+
const out = /* @__PURE__ */ new Set();
|
|
312
|
+
for (const s of parseStatements(sql) ?? []) {
|
|
313
|
+
if (s.kind === "DeleteStmt") out.add(relationName(s.node.relation));
|
|
314
|
+
if (s.kind === "TruncateStmt") for (const r of s.node.relations ?? []) out.add(relationName(r.RangeVar));
|
|
315
|
+
}
|
|
316
|
+
return out;
|
|
317
|
+
}
|
|
318
|
+
function compareTables(before, after, sampled, migrationSql) {
|
|
319
|
+
const emptied = emptiedTables(migrationSql);
|
|
320
|
+
const lost = [];
|
|
321
|
+
const rewritten = [];
|
|
322
|
+
for (const name of sampled) {
|
|
323
|
+
const b = before.get(name);
|
|
324
|
+
const a = after.get(name);
|
|
325
|
+
if (!b || !a) continue;
|
|
326
|
+
if (b.filenode !== a.filenode) rewritten.push(name);
|
|
327
|
+
if (emptied.has(name.slice(name.indexOf(".") + 1))) continue;
|
|
328
|
+
if (a.rows < b.rows) {
|
|
329
|
+
lost.push(`${name}: ${b.rows} row(s) before, ${a.rows} after`);
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (a.rows !== b.rows) continue;
|
|
333
|
+
const changed = Object.entries(b.columns).filter(([col, c]) => a.columns[col] && a.columns[col].type === c.type && a.columns[col].hash !== c.hash).map(([col]) => col);
|
|
334
|
+
if (changed.length > 0) {
|
|
335
|
+
lost.push(`${name}: values changed in ${changed.join(", ")}`);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return { lost, rewritten };
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// src/prove.ts
|
|
205
342
|
var exec = promisify(execFile);
|
|
206
343
|
var TARGET_EXTENSIONS_SQL = `
|
|
207
344
|
SELECT e.extname AS name, n.nspname AS schema
|
|
@@ -314,9 +451,12 @@ async function proveConvergence(opts) {
|
|
|
314
451
|
await copyStructure(opts.targetUrl, cloneUrl, schemas, tools, opts.sourceUrl);
|
|
315
452
|
const scoped = opts.checks !== void 0 && !opts.checks.includes("schema");
|
|
316
453
|
const before = scoped ? await schemaState(cloneUrl, schemas) : void 0;
|
|
454
|
+
const limit = sampleRows();
|
|
455
|
+
const sample = limit > 0 ? await sampleInto(psql, opts.targetUrl, cloneUrl, schemas, limit) : void 0;
|
|
456
|
+
const tablesBefore = sample ? await snapshotTables(cloneUrl, schemas) : void 0;
|
|
317
457
|
let notApplied;
|
|
318
458
|
if (opts.fixes) {
|
|
319
|
-
const replayed = await runLikeApply(cloneUrl, opts.fixes);
|
|
459
|
+
const replayed = await runLikeApply(cloneUrl, opts.fixes, sourceMajor);
|
|
320
460
|
const unmet = replayed.errors.filter((e) => / — not applied: /.test(e.error));
|
|
321
461
|
const failed = replayed.errors.filter((e) => !unmet.includes(e));
|
|
322
462
|
if (failed.length > 0) {
|
|
@@ -331,12 +471,22 @@ async function proveConvergence(opts) {
|
|
|
331
471
|
} else {
|
|
332
472
|
await runSql(psql, cloneUrl, opts.migrationSql);
|
|
333
473
|
}
|
|
334
|
-
|
|
474
|
+
let data;
|
|
475
|
+
if (sample && tablesBefore) {
|
|
476
|
+
data = { ...sample, ...compareTables(tablesBefore, await snapshotTables(cloneUrl, schemas), sample.sampled, opts.migrationSql) };
|
|
477
|
+
if (data.lost.length > 0) {
|
|
478
|
+
return { converged: false, cloneName, data, residual: data.lost.map((l) => `rows lost in ${l}`) };
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
const reported = (r) => {
|
|
482
|
+
const withData = data ? { ...r, data } : r;
|
|
483
|
+
return notApplied?.length ? { ...withData, notApplied } : withData;
|
|
484
|
+
};
|
|
335
485
|
const [want, got] = await Promise.all([
|
|
336
486
|
fingerprint(opts.sourceUrl, schemas),
|
|
337
487
|
fingerprint(cloneUrl, schemas)
|
|
338
488
|
]);
|
|
339
|
-
if (want === got) return reported({ converged: true, residual: [], cloneName });
|
|
489
|
+
if (canonicalArrays(want) === canonicalArrays(got)) return reported({ converged: true, residual: [], cloneName });
|
|
340
490
|
roundTripName = `${cloneName}_src`;
|
|
341
491
|
const roundTripUrl = withDatabase(opts.targetUrl, roundTripName);
|
|
342
492
|
const roundTrip = await copyOfSource(adminUrl, opts.sourceUrl, roundTripUrl, roundTripName, schemas, tools);
|
|
@@ -344,7 +494,7 @@ async function proveConvergence(opts) {
|
|
|
344
494
|
sourceCopyCreated = true;
|
|
345
495
|
if (roundTrip.ok) {
|
|
346
496
|
const wantRoundTripped = await fingerprint(roundTripUrl, schemas);
|
|
347
|
-
if (wantRoundTripped === got) return reported({ converged: true, residual: [], cloneName });
|
|
497
|
+
if (canonicalArrays(wantRoundTripped) === canonicalArrays(got)) return reported({ converged: true, residual: [], cloneName });
|
|
348
498
|
const residual2 = await describeDifference(roundTripUrl, cloneUrl, schemas, wantRoundTripped, got);
|
|
349
499
|
return reported(withinScope(residual2, before && diffState(await schemaState(roundTripUrl, schemas), before), opts.checks, cloneName));
|
|
350
500
|
}
|
|
@@ -426,7 +576,7 @@ var SUPPORTING_SCHEMAS_SQL = (from) => `
|
|
|
426
576
|
FROM pg_depend d
|
|
427
577
|
JOIN pg_namespace n ON n.oid = ${NAMESPACE_OF("d.classid", "d.objid")}
|
|
428
578
|
JOIN pg_namespace rn ON rn.oid = ${NAMESPACE_OF("d.refclassid", "d.refobjid")}
|
|
429
|
-
WHERE n.nspname IN (${from.map(
|
|
579
|
+
WHERE n.nspname IN (${from.map(quoteLiteral2).join(", ")})
|
|
430
580
|
AND rn.nspname <> n.nspname
|
|
431
581
|
AND rn.nspname NOT IN ('pg_catalog', 'information_schema')
|
|
432
582
|
AND rn.nspname NOT LIKE 'pg\\_%'
|
|
@@ -434,7 +584,7 @@ var SUPPORTING_SCHEMAS_SQL = (from) => `
|
|
|
434
584
|
AND NOT EXISTS (SELECT 1 FROM pg_depend e
|
|
435
585
|
WHERE e.classid = d.refclassid AND e.objid = d.refobjid AND e.deptype = 'e')
|
|
436
586
|
ORDER BY 1`;
|
|
437
|
-
function
|
|
587
|
+
function quoteLiteral2(value) {
|
|
438
588
|
return `'${value.replace(/'/g, "''")}'`;
|
|
439
589
|
}
|
|
440
590
|
function tolerateExistingSchemas(sql) {
|
|
@@ -465,7 +615,7 @@ function runSql(psqlPath, dbUrl, sql) {
|
|
|
465
615
|
const trimmed = sql.trim();
|
|
466
616
|
if (!trimmed) return Promise.resolve();
|
|
467
617
|
return new Promise((resolve, reject) => {
|
|
468
|
-
const child =
|
|
618
|
+
const child = spawn2(psqlPath, [dbUrl, "-v", "ON_ERROR_STOP=1", "-q", "-f", "-"], {
|
|
469
619
|
stdio: ["pipe", "pipe", "pipe"]
|
|
470
620
|
});
|
|
471
621
|
let stderr = "";
|
|
@@ -527,6 +677,9 @@ function explainStructureFailure(message, clientMajor, serverMajor) {
|
|
|
527
677
|
return `could not replay the target's structure onto the clone: ${message}${versionGap}`;
|
|
528
678
|
}
|
|
529
679
|
var DROPPED_NAME = /\bDROP\s+(?:TABLE|VIEW|MATERIALIZED\s+VIEW|TYPE|DOMAIN|SEQUENCE|FUNCTION|PROCEDURE|INDEX|COLUMN|CONSTRAINT|POLICY|TRIGGER)\s+(?:IF\s+EXISTS\s+)?(?:(?:"[^"]+"|[\w$]+)\s*\.\s*)?("[^"]+"|[\w$]+)/gi;
|
|
680
|
+
function identParts(qualified) {
|
|
681
|
+
return [...qualified.matchAll(/"((?:[^"]|"")+)"|([^."]+)/g)].map((m) => (m[1]?.replace(/""/g, '"') ?? m[2]).toLowerCase());
|
|
682
|
+
}
|
|
530
683
|
var CREATED_NAME = /\bCREATE\s+(?:OR\s+REPLACE\s+)?(?:UNIQUE\s+)?(?:TABLE|VIEW|MATERIALIZED\s+VIEW|TYPE|DOMAIN|SEQUENCE|FUNCTION|PROCEDURE|INDEX|POLICY|TRIGGER|EXTENSION)\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:(?:"[^"]+"|[\w$]+)\s*\.\s*)?("[^"]+"|[\w$]+)/gi;
|
|
531
684
|
function residualHeldBack(residual, heldBackSql) {
|
|
532
685
|
const dropped = /* @__PURE__ */ new Set();
|
|
@@ -540,10 +693,11 @@ function residualHeldBack(residual, heldBackSql) {
|
|
|
540
693
|
const heldBack = [];
|
|
541
694
|
const unexplained = [];
|
|
542
695
|
for (const line of residual) {
|
|
543
|
-
const subject = /^[a-z ]+?\s([^\s:]+(?:\s[^\s:]+)?):\s*unexpected
|
|
696
|
+
const [, subject, owner] = /^[a-z ]+?\s([^\s:]+(?:\s[^\s:]+)?):\s*unexpected(?: \(owned by (.+)\))?$/i.exec(line) ?? [];
|
|
544
697
|
const name = subject?.split(/[.\s]/).pop()?.replace(/^"|"$/g, "").toLowerCase();
|
|
698
|
+
const owners = owner ? identParts(owner) : [];
|
|
545
699
|
const missing = /^(.*):\s*missing$/i.exec(line)?.[1].toLowerCase();
|
|
546
|
-
if (name && dropped.has(name)) heldBack.push(line);
|
|
700
|
+
if (name && dropped.has(name) || owners.some((o) => dropped.has(o))) heldBack.push(line);
|
|
547
701
|
else if (missing && created.some((n) => missing.endsWith(` ${n}`) || missing.endsWith(`.${n}`))) heldBack.push(line);
|
|
548
702
|
else unexplained.push(line);
|
|
549
703
|
}
|
|
@@ -592,11 +746,16 @@ function outsideTheRun(line, checks) {
|
|
|
592
746
|
}
|
|
593
747
|
|
|
594
748
|
export {
|
|
749
|
+
withDatabase,
|
|
750
|
+
fingerprint,
|
|
595
751
|
comparedSchemas,
|
|
596
752
|
proveConvergence,
|
|
753
|
+
copyStructure,
|
|
754
|
+
dropDatabase,
|
|
755
|
+
psqlBeside,
|
|
597
756
|
dropUnsupportedSetStatements,
|
|
598
757
|
knownParameters,
|
|
599
758
|
residualHeldBack,
|
|
600
759
|
proofScope
|
|
601
760
|
};
|
|
602
|
-
//# sourceMappingURL=chunk-
|
|
761
|
+
//# sourceMappingURL=chunk-GGZ7YWBZ.js.map
|