backupdrill 2.8.0 → 2.8.1
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 +1 -1
- package/dist/drill.d.ts +2 -7
- package/dist/drill.js +5 -8
- package/dist/orphaned-foreign-keys.d.ts +10 -1
- package/dist/orphaned-foreign-keys.js +14 -1
- package/dist/restore-engine.d.ts +1 -3
- package/dist/restore-engine.js +3 -11
- package/dist/restore.js +2 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -207,7 +207,7 @@ backupdrill restore --database --target-supabase-url https://<ref>.supabase.co -
|
|
|
207
207
|
|
|
208
208
|
Prefer files on disk instead? Skip the Storage target and pass `--storage-dir ./recovered`.
|
|
209
209
|
|
|
210
|
-
`--dry-run` writes nothing to the target. Besides checking the target (not the source project, schemas empty, Postgres and `pg_restore` versions, extensions available), it downloads the dump to a temporary directory, checks its sha256 and looks for foreign keys that point outside the backup (see [Tables that reference auth.users](#tables-that-reference-authusers)). The real run repeats those checks before it writes anything; a foreign key into a table that one of the recorded extensions creates is checked again once the extensions are installed. The database restore then runs in a single transaction: if any statement fails, nothing from the snapshot stays on the target, and you can run it again on the same project once you've fixed the cause. (If the connection drops while it commits, the error says so and asks you to check the target first.) Foreign keys between two tables in the backup are the exception: they're added right after that transaction commits, in a second one. If rows in your data already break one of them (deleting with triggers switched off, for example under `session_replication_role = replica`, can leave rows like that), the key is added as `NOT VALID` instead of failing the restore. Those rows come back as they are, new writes are still checked, and the report names the key and, when it can count them, how many rows break it.
|
|
210
|
+
`--dry-run` writes nothing to the target. Besides checking the target (not the source project, schemas empty, Postgres and `pg_restore` versions, extensions available), it downloads the dump to a temporary directory, checks its sha256 and looks for foreign keys that point outside the backup (see [Tables that reference auth.users](#tables-that-reference-authusers)). The real run repeats those checks before it writes anything; a foreign key into a table that one of the recorded extensions creates is checked again once the extensions are installed. The database restore then runs in a single transaction: if any statement fails, nothing from the snapshot stays on the target, and you can run it again on the same project once you've fixed the cause. (If the connection drops while it commits, the error says so and asks you to check the target first.) Foreign keys between two tables in the backup are the exception: they're added right after that transaction commits, in a second one. If rows in your data already break one of them (deleting with triggers switched off, for example under `session_replication_role = replica`, can leave rows like that), the key is added as `NOT VALID` instead of failing the restore. Those rows come back as they are, new writes are still checked, and the report names the key and, when it can count them, how many rows break it. Keys on or into partitioned tables, keys into an extension's tables, and every key in an archive whose foreign key statements BackupDrill can't fully read stay in the main transaction, so a broken one rolls the whole restore back. A key over a generated column is added in the second step but is never relaxed, because the restore recomputes those values; if it breaks, that step fails. Keys added as `NOT VALID` don't fail a drill: the drill passes and lists them in its report. If the second step fails for any other reason, the tables and data are already on the target and the error says so. Extensions the run installed stay installed, since the next run needs them too, and the empty-target check doesn't count objects that belong to an extension (except an extension's configuration tables, whose rows a dump can carry). One transaction holds a lock on every table, index and sequence until it commits, so a very large schema can run out of lock slots: on Supabase's smallest compute size, about a thousand tables (partitions count as tables). The restore then stops with nothing written and tells you how to raise `max_locks_per_transaction`.
|
|
211
211
|
|
|
212
212
|
Or restore the dump by hand — into an **empty** database, without `--clean`:
|
|
213
213
|
|
package/dist/drill.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { BackupConfig } from "./config.js";
|
|
2
2
|
import type { Manifest } from "./manifest.js";
|
|
3
3
|
import { type SnapshotStore } from "./snapshot-store.js";
|
|
4
|
-
import type
|
|
4
|
+
import { type OrphanedForeignKey } from "./orphaned-foreign-keys.js";
|
|
5
5
|
/**
|
|
6
6
|
* 无放回随机抽样:部分 Fisher-Yates,只洗出前 n 位(O(n) 次交换,不必全 shuffle)。
|
|
7
7
|
* 此前恒取 manifest 顺序的前 CAP 个 → 超过 CAP 的项目里排序靠后的文件永远不被校验;
|
|
@@ -41,13 +41,8 @@ export interface DrillReport {
|
|
|
41
41
|
containerId: string;
|
|
42
42
|
connString: string;
|
|
43
43
|
};
|
|
44
|
-
/**
|
|
44
|
+
/** 源库自带孤儿行的外键:演练和真实恢复都把它们建成 NOT VALID,不算失败。没有时不出现 */
|
|
45
45
|
orphanedForeignKeys?: OrphanedForeignKey[];
|
|
46
|
-
/**
|
|
47
|
-
* 失败的唯一原因是这些孤儿行:备份完整、恢复能成功,是用户库里的数据违反了自己的外键。
|
|
48
|
-
* worker 据此把失败报告直接发给用户,不当成我们要排查的故障。
|
|
49
|
-
*/
|
|
50
|
-
failureCause?: "source-data";
|
|
51
46
|
}
|
|
52
47
|
export interface SandboxTuning {
|
|
53
48
|
settings: string[];
|
package/dist/drill.js
CHANGED
|
@@ -9,6 +9,7 @@ import { hashObject } from "./snapshots.js";
|
|
|
9
9
|
import { s3SnapshotStore } from "./snapshot-store.js";
|
|
10
10
|
import { SANDBOX_MANAGED_ERROR, classifyBlocks, finalizePass, installExtensions, quoteIdent, restoreDatabaseArtifact, installSandboxShim, tableDataInDump, } from "./restore-engine.js";
|
|
11
11
|
import { connectPg } from "./supabase-ca.js";
|
|
12
|
+
import { orphanedForeignKeysCheck } from "./orphaned-foreign-keys.js";
|
|
12
13
|
import { localPgRestoreMajor, restoreToolBreaksOnTarget, sandboxMajorFor } from "./pgbin.js";
|
|
13
14
|
import { log } from "./log.js";
|
|
14
15
|
import { onInterrupt, stopOnInterrupt, throwIfInterrupted } from "./interrupt.js";
|
|
@@ -616,6 +617,9 @@ export async function drillDump(dumpPath, manifest, snapshot, preChecks = [], op
|
|
|
616
617
|
await opts.supervision?.observe({ phase: "verify" });
|
|
617
618
|
const verify = await verifyRestored(pg.connString, manifest, dumpPath);
|
|
618
619
|
checks.push(...verify.checks);
|
|
620
|
+
const orphaned = postData.orphanedForeignKeys ?? [];
|
|
621
|
+
if (orphaned.length)
|
|
622
|
+
checks.push(orphanedForeignKeysCheck(orphaned));
|
|
619
623
|
// 语义层钩子:只在结构层全绿时才跑——结构已失败时再跑应用检查,只会把
|
|
620
624
|
// 一次失败报成两层失败,误导排查方向。没配置则不跑、报告里不出现该行。
|
|
621
625
|
if (appCheckCommand && checks.every((c) => c.pass)) {
|
|
@@ -632,15 +636,8 @@ export async function drillDump(dumpPath, manifest, snapshot, preChecks = [], op
|
|
|
632
636
|
restoredTableCount: verify.tableCount,
|
|
633
637
|
restoredRowTotal: verify.rowTotal,
|
|
634
638
|
};
|
|
635
|
-
|
|
636
|
-
if (orphaned.length) {
|
|
639
|
+
if (orphaned.length)
|
|
637
640
|
report.orphanedForeignKeys = orphaned;
|
|
638
|
-
// 孤儿外键是 post-data 里仅有的失败、其余检查全过 → 失败只因用户数据
|
|
639
|
-
const onlyOrphans = postData.failures.length === orphaned.length;
|
|
640
|
-
if (!pass && onlyOrphans && checks.every((c) => c.pass || c.name === "post-data objects")) {
|
|
641
|
-
report.failureCause = "source-data";
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
641
|
if (opts.keepSandboxOnFailure && !pass) {
|
|
645
642
|
// 写进报告让 --keep 可被脚本消费(拿连接串排查),不只靠人眼看日志
|
|
646
643
|
report.keptSandbox = { containerId: pg.containerId, connString: pg.connString };
|
|
@@ -49,8 +49,17 @@ export declare const COMPARISON_PLAN_SQL = "\n select n.nspname as schema, o.op
|
|
|
49
49
|
* MATCH SIMPLE 只查引用列全非空的行;MATCH FULL 另算"部分为空"的行。
|
|
50
50
|
*/
|
|
51
51
|
export declare function orphanCountSql(fk: ForeignKeyDef, plan: ComparisonPlan): string;
|
|
52
|
-
/**
|
|
52
|
+
/** 一条孤儿外键给人看的说法。 */
|
|
53
53
|
export declare function describeOrphanedForeignKey(orphaned: OrphanedForeignKey): string;
|
|
54
|
+
/**
|
|
55
|
+
* 演练和真实恢复报告里共用的一项:算通过。我们管的是备份完整、能完整恢复;源库里本来就有的这些行
|
|
56
|
+
* 原样回来,不让用户去改自己的数据(创始人 2026-10-31)。
|
|
57
|
+
*/
|
|
58
|
+
export declare function orphanedForeignKeysCheck(orphaned: OrphanedForeignKey[]): {
|
|
59
|
+
name: string;
|
|
60
|
+
pass: boolean;
|
|
61
|
+
detail: string;
|
|
62
|
+
};
|
|
54
63
|
export interface AddForeignKeysResult {
|
|
55
64
|
orphaned: OrphanedForeignKey[];
|
|
56
65
|
/** 沙箱托管豁免命中数(与 classifyBlocks 同口径) */
|
|
@@ -68,7 +68,7 @@ export function orphanCountSql(fk, plan) {
|
|
|
68
68
|
const where = fk.matchFull ? `(${anySet}) and (not (${allSet}) or ${missing})` : `${allSet} and ${missing}`;
|
|
69
69
|
return `select count(*)::bigint as n from only ${quoteIdent(fk.table.schema)}.${quoteIdent(fk.table.name)} c where ${where}`;
|
|
70
70
|
}
|
|
71
|
-
/**
|
|
71
|
+
/** 一条孤儿外键给人看的说法。 */
|
|
72
72
|
export function describeOrphanedForeignKey(orphaned) {
|
|
73
73
|
const rows = orphaned.orphanRows === null ? "Some rows" : `${orphaned.orphanRows} row(s)`;
|
|
74
74
|
// 不说"指向不存在的行":MATCH FULL 下部分为空的行也算违反,它们未必缺对应的行(文案审查)
|
|
@@ -85,6 +85,19 @@ async function hasGeneratedKeyColumn(client, fk) {
|
|
|
85
85
|
) as generated`, [qualified(fk.table), fk.columns, qualified(fk.references), fk.referencedColumns]);
|
|
86
86
|
return rows[0].generated;
|
|
87
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* 演练和真实恢复报告里共用的一项:算通过。我们管的是备份完整、能完整恢复;源库里本来就有的这些行
|
|
90
|
+
* 原样回来,不让用户去改自己的数据(创始人 2026-10-31)。
|
|
91
|
+
*/
|
|
92
|
+
export function orphanedForeignKeysCheck(orphaned) {
|
|
93
|
+
return {
|
|
94
|
+
name: "foreign keys",
|
|
95
|
+
pass: true,
|
|
96
|
+
detail: `${orphaned.length} added as NOT VALID: ${orphaned.map(describeOrphanedForeignKey).join("; ")}. ` +
|
|
97
|
+
`Those rows were already in the database when it was backed up and are restored as they are. ` +
|
|
98
|
+
`New writes are still checked.`,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
88
101
|
/**
|
|
89
102
|
* 在一个事务里逐条加外键,每条一个 savepoint。23503 → 改加 NOT VALID 并数孤儿行。
|
|
90
103
|
* 其余错误:stopOnError(真实目标)= 回滚整个事务并抛出;否则(沙箱)按 allow 分类后继续。
|
package/dist/restore-engine.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ export interface EngineResult {
|
|
|
35
35
|
preData: ClassifiedPass;
|
|
36
36
|
postData: ClassifiedPass;
|
|
37
37
|
ok: boolean;
|
|
38
|
-
/** 因源库自带孤儿行而以 NOT VALID 建成的外键(见 orphaned-foreign-keys.ts)
|
|
38
|
+
/** 因源库自带孤儿行而以 NOT VALID 建成的外键(见 orphaned-foreign-keys.ts);沙箱和真实目标一样,不算失败 */
|
|
39
39
|
orphanedForeignKeys?: OrphanedForeignKey[];
|
|
40
40
|
/**
|
|
41
41
|
* 真实目标:失败发生在主事务提交之后(外键那一步),表和数据已经在目标上。
|
|
@@ -43,8 +43,6 @@ export interface EngineResult {
|
|
|
43
43
|
*/
|
|
44
44
|
committed?: boolean;
|
|
45
45
|
}
|
|
46
|
-
/** 演练里孤儿外键记成失败项时的说法:数据问题出在源库,真实恢复会怎样。 */
|
|
47
|
-
export declare function sandboxOrphanFailure(orphaned: OrphanedForeignKey): string;
|
|
48
46
|
/**
|
|
49
47
|
* 新版 pg_restore 的脚本首尾各有一条 psql 元命令(\restrict <key> / \unrestrict <key>),node-pg 执行不了。
|
|
50
48
|
* 只认位置:开头那条是第一行非空、非 -- 注释的行,结尾那条是最后一行非空行。注释正文是字符串,
|
package/dist/restore-engine.js
CHANGED
|
@@ -19,7 +19,7 @@ import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
|
19
19
|
import { tmpdir } from "node:os";
|
|
20
20
|
import { join } from "node:path";
|
|
21
21
|
import { MAX_COPY_HEADER, parseCopyHeader, parseCreatedTables, parseCreateSchemas, parseForeignKeys, } from "./dump-sql.js";
|
|
22
|
-
import { addForeignKeys,
|
|
22
|
+
import { addForeignKeys, fullyDumpedTableEntries, isDeferrableForeignKey, tableKey, } from "./orphaned-foreign-keys.js";
|
|
23
23
|
import { resolvePgRestoreBin } from "./pgbin.js";
|
|
24
24
|
import { dumpUrlFor, connectPg, isSupabaseHost } from "./supabase-ca.js";
|
|
25
25
|
import { interrupted, onInterrupt, stopOnInterrupt } from "./interrupt.js";
|
|
@@ -100,11 +100,6 @@ export function refFromStorageEndpoint(endpoint) {
|
|
|
100
100
|
return null;
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
-
/** 演练里孤儿外键记成失败项时的说法:数据问题出在源库,真实恢复会怎样。 */
|
|
104
|
-
export function sandboxOrphanFailure(orphaned) {
|
|
105
|
-
return (`${describeOrphanedForeignKey(orphaned)}. Your database already had these rows when it was backed up. ` +
|
|
106
|
-
`A restore brings them back and adds this foreign key as NOT VALID; clean up the rows to make the drill pass.`);
|
|
107
|
-
}
|
|
108
103
|
/**
|
|
109
104
|
* 从目录条目里挑出交给 addForeignKeys 的外键(isDeferrableForeignKey)。让 pg_restore 把全部外键条目
|
|
110
105
|
* 写成 SQL,第 i 条语句对第 i 行(-L 按清单顺序输出)。对不上数或对不上名字(认不出的外键形态)
|
|
@@ -956,11 +951,8 @@ export async function restoreDatabaseArtifact(opts) {
|
|
|
956
951
|
stopOnError: false,
|
|
957
952
|
commentSql: deferred.commentSql,
|
|
958
953
|
});
|
|
959
|
-
//
|
|
960
|
-
passes.push({
|
|
961
|
-
expectedSkips: added.expectedSkips,
|
|
962
|
-
failures: [...added.failures, ...added.orphaned.map(sandboxOrphanFailure)],
|
|
963
|
-
});
|
|
954
|
+
// 孤儿外键不算失败:真实恢复同样建成 NOT VALID、数据一行不少,演练报告里另列一项说明
|
|
955
|
+
passes.push({ expectedSkips: added.expectedSkips, failures: added.failures });
|
|
964
956
|
orphanedForeignKeys = added.orphaned;
|
|
965
957
|
}
|
|
966
958
|
finally {
|
package/dist/restore.js
CHANGED
|
@@ -11,7 +11,7 @@ import { checkExternalReferences, describe } from "./external-refs.js";
|
|
|
11
11
|
import { verifyRestored } from "./drill.js";
|
|
12
12
|
import { restoreStorage, planStorageRestore, } from "./storage-restore.js";
|
|
13
13
|
import { connectPg } from "./supabase-ca.js";
|
|
14
|
-
import {
|
|
14
|
+
import { orphanedForeignKeysCheck } from "./orphaned-foreign-keys.js";
|
|
15
15
|
import { localPgRestoreMajor, restoreToolTargetBlocker } from "./pgbin.js";
|
|
16
16
|
import { parsePgDumpMajor } from "./backup.js";
|
|
17
17
|
import { log } from "./log.js";
|
|
@@ -709,13 +709,7 @@ export async function runRestore(config, opts) {
|
|
|
709
709
|
if (orphaned.length) {
|
|
710
710
|
result.orphanedForeignKeys = orphaned;
|
|
711
711
|
// 数据一行不少,外键也在(只是不回头查已有的行):算通过,但写进报告让人看见
|
|
712
|
-
result.databaseChecks.push(
|
|
713
|
-
name: "foreign keys",
|
|
714
|
-
pass: true,
|
|
715
|
-
detail: `${orphaned.length} added as NOT VALID because the snapshot holds rows that break them: ` +
|
|
716
|
-
`${orphaned.map(describeOrphanedForeignKey).join("; ")}. New writes are still checked. ` +
|
|
717
|
-
`Clean up those rows, then run ALTER TABLE … VALIDATE CONSTRAINT to check the old ones too.`,
|
|
718
|
-
});
|
|
712
|
+
result.databaseChecks.push(orphanedForeignKeysCheck(orphaned));
|
|
719
713
|
}
|
|
720
714
|
for (const check of verified.checks) {
|
|
721
715
|
(check.pass ? log.ok : log.warn)(`${check.name}: ${check.detail}`);
|
package/package.json
CHANGED