backupdrill 2.11.0 → 2.11.2
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 +23 -9
- package/dist/auth-archive.d.ts +31 -0
- package/dist/auth-archive.js +423 -0
- package/dist/auth-backup.d.ts +10 -1
- package/dist/auth-backup.js +134 -0
- package/dist/auth-drill.d.ts +41 -2
- package/dist/auth-drill.js +135 -22
- package/dist/auth-restore.d.ts +5 -0
- package/dist/auth-restore.js +11 -0
- package/dist/backup.js +46 -3
- package/dist/drill.d.ts +30 -1
- package/dist/drill.js +280 -15
- package/dist/recovery-doc.d.ts +7 -0
- package/dist/recovery-doc.js +13 -1
- package/dist/restore.d.ts +1 -1
- package/dist/restore.js +14 -5
- package/package.json +4 -2
- package/plugin/README.md +1 -1
- package/plugin/skills/supabase-backup/SKILL.md +5 -5
- package/plugin/skills/supabase-backup/assets/scheduled-backup.yml +1 -1
package/dist/auth-backup.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import pg from "pg";
|
|
2
2
|
import { attachPgErrorGuard, pgConnectOptions } from "./supabase-ca.js";
|
|
3
|
+
import { PENDING_AUTH_COPY_VALUES } from "./auth-archive.js";
|
|
3
4
|
export const AUTH_TABLES = ["users", "identities"];
|
|
4
5
|
/** auth.dump 的技术上限:对应数百万用户或巨大的 metadata。超过就跳过,不拖慢和撑爆演练与恢复 */
|
|
5
6
|
export const AUTH_DUMP_MAX_BYTES = 2 * 1024 * 1024 * 1024;
|
|
@@ -271,9 +272,15 @@ export async function inspectAuth(databaseUrl, snapshotId, signal) {
|
|
|
271
272
|
from pg_class c, aclexplode(c.relacl) a
|
|
272
273
|
where c.oid in ('auth.users'::regclass, 'auth.identities'::regclass)
|
|
273
274
|
order by 1, 2, 3`);
|
|
275
|
+
// 比对的是 pg_get_*def 打印出来的原文:让它和白名单记下时的打印方式一样,不受角色/库级设置影响
|
|
276
|
+
// (quote_all_identifiers 会给每个名字加引号;search_path 决定名字前带不带 schema)
|
|
277
|
+
await client.query("set local quote_all_identifiers = off");
|
|
278
|
+
await client.query("set local search_path = ''");
|
|
279
|
+
const nonStockObject = await firstNonStockObject(client);
|
|
274
280
|
await client.query("commit");
|
|
275
281
|
return {
|
|
276
282
|
readable: true,
|
|
283
|
+
nonStockObject,
|
|
277
284
|
grants: grants.rows,
|
|
278
285
|
users: Number(c.users),
|
|
279
286
|
identities: Number(c.identities),
|
|
@@ -305,6 +312,133 @@ export async function inspectAuth(databaseUrl, snapshotId, signal) {
|
|
|
305
312
|
await client.end().catch(() => { });
|
|
306
313
|
}
|
|
307
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* 把系统目录里的名字(约束名、索引名、列名)写进日志、manifest 和 RECOVERY.md 之前用:加双引号,
|
|
317
|
+
* 反斜杠、引号和所有控制字符(C0、DEL、C1、U+2028/2029)都转义成可见的文字。名字可以含换行,
|
|
318
|
+
* 原样写进终端就能伪造一行日志
|
|
319
|
+
*/
|
|
320
|
+
export function quotedName(name) {
|
|
321
|
+
return `"${name.replace(/[\\"\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, (ch) => {
|
|
322
|
+
if (ch === "\\" || ch === '"')
|
|
323
|
+
return `\\${ch}`;
|
|
324
|
+
if (ch === "\n")
|
|
325
|
+
return "\\n";
|
|
326
|
+
if (ch === "\r")
|
|
327
|
+
return "\\r";
|
|
328
|
+
if (ch === "\t")
|
|
329
|
+
return "\\t";
|
|
330
|
+
return `\\u${ch.charCodeAt(0).toString(16).padStart(4, "0")}`;
|
|
331
|
+
})}"`;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Supabase(GoTrue)自带的 auth.users:2026-11-21 在 supabase/postgres 17.6 上跑完 GoTrue v2.186.0 的全部迁移后
|
|
335
|
+
* 从系统目录读出来的原文(pg_get_constraintdef / pg_get_indexdef / pg_get_expr;PG16、PG17 输出一致)。
|
|
336
|
+
* 索引去掉名字只比定义,名字改过不算不同
|
|
337
|
+
*/
|
|
338
|
+
const STOCK_AUTH_USERS = {
|
|
339
|
+
/** CHECK / FOREIGN KEY / EXCLUDE 约束;自带的只有这一条 CHECK,没有外键和排他约束 */
|
|
340
|
+
constraints: ["CHECK (((email_change_confirm_status >= 0) AND (email_change_confirm_status <= 2)))"],
|
|
341
|
+
/** 索引:唯一索引(含主键、UNIQUE 约束背后的索引),以及带表达式的普通索引 */
|
|
342
|
+
indexes: [
|
|
343
|
+
"CREATE UNIQUE INDEX ON auth.users USING btree (id)",
|
|
344
|
+
"CREATE UNIQUE INDEX ON auth.users USING btree (phone)",
|
|
345
|
+
"CREATE UNIQUE INDEX ON auth.users USING btree (email) WHERE (is_sso_user = false)",
|
|
346
|
+
...["confirmation_token", "recovery_token", "email_change_token_current", "email_change_token_new", "reauthentication_token"].map((column) => `CREATE UNIQUE INDEX ON auth.users USING btree (${column}) WHERE ((${column})::text !~ '^[0-9 ]*$'::text)`),
|
|
347
|
+
"CREATE INDEX ON auth.users USING btree (instance_id, lower((email)::text))",
|
|
348
|
+
],
|
|
349
|
+
/** 生成列 → 表达式 */
|
|
350
|
+
generated: new Map([["confirmed_at", "LEAST(email_confirmed_at, phone_confirmed_at)"]]),
|
|
351
|
+
};
|
|
352
|
+
/** pg_get_indexdef 的结果去掉索引名:"CREATE [UNIQUE] INDEX <名字> ON ..." → "CREATE [UNIQUE] INDEX ON ..." */
|
|
353
|
+
function indexDefWithoutName(def) {
|
|
354
|
+
return def.replace(/^(CREATE (?:UNIQUE )?INDEX )(?:"(?:[^"]|"")*"|\S+) ON /, "$1ON ");
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* 2.11.1 起 auth.dump 里待处理的 token 被抹成空串、发送时间成 NULL、确认状态成 0(见 auth-archive.ts)。
|
|
358
|
+
* 用户自己在 auth.users 上加的约束可能不接受这些值(例如 CHECK (recovery_token <> '')、token 列上不带条件的
|
|
359
|
+
* 唯一索引、从 token 算出来的生成列再加 CHECK):原样的归档恢复得了、抹过的却恢复不了,这比不抹更糟。
|
|
360
|
+
*
|
|
361
|
+
* 规则:**可能碰到被抹的列**的东西,必须和 Supabase 自带的一模一样,否则不写 auth.dump、Auth 这一节记失败并点名
|
|
362
|
+
* 第一个对不上的对象。碰不到的(UNIQUE (email)、新加的非 token 列上的唯一索引、instance_id 上的外键)一律放过——
|
|
363
|
+
* 不然和 token 无关的改动、或者 GoTrue 以后新加的索引,都会让 Auth 备份悄悄停掉。"可能碰到"按系统目录判断:
|
|
364
|
+
* - 约束的 conkey、索引的键列里有被抹的列(CHECK 的 conkey 就是它引用的全部列,0 = 整行);
|
|
365
|
+
* - 索引的表达式和 WHERE 条件、生成列的表达式:读系统目录里存的表达式树(indexprs / indpred / adbin 的文本),
|
|
366
|
+
* 里面每个列引用都是 ":varattno N",0 = 整行。不用 pg_depend:全是表达式的索引在那里总记着对整张表(列号 0)
|
|
367
|
+
* 的依赖,分不出 lower(email) 和真的整行引用;
|
|
368
|
+
* - 排他约束按它背后的索引判断;
|
|
369
|
+
* - 生成列的表达式引用了被抹的列:它本身要和自带的一样,用到它的约束、索引也算可能碰到;
|
|
370
|
+
* - 外键:只看 conkey;
|
|
371
|
+
* - 被抹成 NULL 的列不能是 NOT NULL,被抹的列不能是域类型。
|
|
372
|
+
* 要比对的对象类型:CHECK、外键、排他约束(pg_get_constraintdef 原文),唯一索引和排他约束背后的索引
|
|
373
|
+
* (去掉名字的 pg_get_indexdef 原文),带表达式或 WHERE 条件的普通索引(恢复时重建要算它们;只按列建的普通索引
|
|
374
|
+
* 重建不会失败,不管),生成列(列名和表达式原文)。和 pg_dump 一样只看有效的索引。
|
|
375
|
+
*
|
|
376
|
+
* **只读系统目录,不执行任何用户定义的东西**:约束表达式里可以调用用户函数,不能拿备份角色的权限在生产库上跑。
|
|
377
|
+
* 跑在 inspectAuth 已经开着的 REPEATABLE READ READ ONLY 事务里,不另开连接。返回第一个对不上的对象(没有就是 null)
|
|
378
|
+
*/
|
|
379
|
+
async function firstNonStockObject(client) {
|
|
380
|
+
const blanked = new Map(PENDING_AUTH_COPY_VALUES);
|
|
381
|
+
const columns = await client.query(`select a.attnum, a.attname as name, a.attnotnull as notnull, t.typtype = 'd' as domain
|
|
382
|
+
from pg_attribute a join pg_type t on t.oid = a.atttypid
|
|
383
|
+
where a.attrelid = 'auth.users'::regclass and a.attnum > 0 and not a.attisdropped
|
|
384
|
+
order by a.attnum`);
|
|
385
|
+
// 被抹的列,加上从它们算出来的生成列:碰到这些列的东西才要比对
|
|
386
|
+
const touched = new Set();
|
|
387
|
+
for (const c of columns.rows) {
|
|
388
|
+
if (!blanked.has(c.name))
|
|
389
|
+
continue;
|
|
390
|
+
touched.add(c.attnum);
|
|
391
|
+
if (c.domain)
|
|
392
|
+
return `a domain-typed column ${quotedName(c.name)}`;
|
|
393
|
+
if (blanked.get(c.name) === "\\N" && c.notnull)
|
|
394
|
+
return `a NOT NULL column ${quotedName(c.name)}`;
|
|
395
|
+
}
|
|
396
|
+
// 列号 0 = 整行,什么列都可能碰到
|
|
397
|
+
const mayTouch = (referenced) => referenced.some((n) => n === 0 || touched.has(n));
|
|
398
|
+
/** 表达式树(pg_node_tree 的文本)里引用的列号;表达式里的常量是按字节存的,不会被误认成列引用 */
|
|
399
|
+
const columnsIn = (...trees) => trees.flatMap((tree) => [...(tree ?? "").matchAll(/:varattno (-?\d+)/g)].map((m) => Number(m[1])));
|
|
400
|
+
const generated = await client.query(`select a.attnum, a.attname as name, pg_get_expr(d.adbin, d.adrelid) as expr, d.adbin::text as tree
|
|
401
|
+
from pg_attribute a join pg_attrdef d on d.adrelid = a.attrelid and d.adnum = a.attnum
|
|
402
|
+
where a.attrelid = 'auth.users'::regclass and a.attgenerated <> '' and not a.attisdropped
|
|
403
|
+
order by a.attnum`);
|
|
404
|
+
for (const g of generated.rows) {
|
|
405
|
+
if (!mayTouch(columnsIn(g.tree)))
|
|
406
|
+
continue;
|
|
407
|
+
if (STOCK_AUTH_USERS.generated.get(g.name) !== g.expr)
|
|
408
|
+
return `a custom generated column ${quotedName(g.name)}`;
|
|
409
|
+
touched.add(g.attnum);
|
|
410
|
+
}
|
|
411
|
+
// CHECK 的 conkey 是它引用的全部列;外键只有自己的列。排他约束在下面按它背后的索引判断
|
|
412
|
+
const constraints = await client.query(`select conname as name, contype::text as type, pg_get_constraintdef(oid) as def, coalesce(conkey::int[], '{}') as keys
|
|
413
|
+
from pg_constraint
|
|
414
|
+
where conrelid = 'auth.users'::regclass and contype in ('c', 'f')
|
|
415
|
+
order by conname`);
|
|
416
|
+
for (const k of constraints.rows) {
|
|
417
|
+
const could = k.type === "f" ? k.keys.some((n) => touched.has(n)) : mayTouch(k.keys);
|
|
418
|
+
if (!could || STOCK_AUTH_USERS.constraints.includes(k.def))
|
|
419
|
+
continue;
|
|
420
|
+
return `a custom ${k.type === "c" ? "check constraint" : "foreign key"} ${quotedName(k.name)}`;
|
|
421
|
+
}
|
|
422
|
+
// pg_dump 导出的索引(和它一样只要 indisvalid、indisready 的:CONCURRENTLY 建失败留下的无效索引不进归档)。
|
|
423
|
+
// 恢复时每个索引都要在抹过的数据上重建:唯一索引、排他约束会因重复值失败;普通索引只要有表达式或 WHERE
|
|
424
|
+
// 条件,也会在重建时算它们(例如 1 / length(recovery_token) 碰到 '' 就除零)。只按列建的普通索引重建不会失败,不管
|
|
425
|
+
const indexes = await client.query(`select c.relname as name, pg_get_indexdef(i.indexrelid) as def, i.indkey::int2[] as keys,
|
|
426
|
+
i.indexprs::text as exprs, i.indpred::text as pred, i.indisunique as unique, i.indisexclusion as exclusion
|
|
427
|
+
from pg_index i join pg_class c on c.oid = i.indexrelid
|
|
428
|
+
where i.indrelid = 'auth.users'::regclass and i.indisvalid and i.indisready
|
|
429
|
+
order by c.relname`);
|
|
430
|
+
for (const ix of indexes.rows) {
|
|
431
|
+
if (!ix.unique && !ix.exclusion && ix.exprs === null && ix.pred === null)
|
|
432
|
+
continue;
|
|
433
|
+
// 键列里的 0 是"这一列是表达式"的占位,表达式用到的列从表达式树里读
|
|
434
|
+
const referenced = [...ix.keys.filter((n) => n !== 0), ...columnsIn(ix.exprs, ix.pred)];
|
|
435
|
+
if (!mayTouch(referenced) || STOCK_AUTH_USERS.indexes.includes(indexDefWithoutName(ix.def)))
|
|
436
|
+
continue;
|
|
437
|
+
const kind = ix.exclusion ? "exclusion constraint" : ix.unique ? "unique index" : "index";
|
|
438
|
+
return `a custom ${kind} ${quotedName(ix.name)}`;
|
|
439
|
+
}
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
308
442
|
/**
|
|
309
443
|
* schema 是否在这次备份里:按 pg_dump 读模式的规则匹配 schemas(含通配)。平台 schema 不算:
|
|
310
444
|
* 用户自己建在 extensions 之类里的函数不在任何一份归档里,新项目里也没有。
|
package/dist/auth-drill.d.ts
CHANGED
|
@@ -4,7 +4,8 @@ import { type OrphanedForeignKey } from "./orphaned-foreign-keys.js";
|
|
|
4
4
|
* 演练里的 Auth 校验(2.10.0 起,附加模式)。
|
|
5
5
|
*
|
|
6
6
|
* 在演练照旧做完、通过与否已经定下之后,在同一个沙箱里:把 auth.dump 的两张表建出来、灌数据,
|
|
7
|
-
* 补上之前因为沙箱没有 auth 表而跳过的外键,建可以自动恢复的触发器,再以 GoTrue
|
|
7
|
+
* 补上之前因为沙箱没有 auth 表而跳过的外键,建可以自动恢复的触发器,再以 GoTrue 的身份模拟一次注册
|
|
8
|
+
* (2.11.1 起断网跑,见 OfflineSandbox)。
|
|
8
9
|
* 结果只写进报告的 auth 一节,**不改变演练结论**,自己出错也只记在这一节里——上线初期先观察,
|
|
9
10
|
* 是否计入结论由以后决定(PRD 增补 §0.1 第 5 条)。
|
|
10
11
|
*/
|
|
@@ -41,9 +42,45 @@ export interface AuthDumpForDrill {
|
|
|
41
42
|
path?: string;
|
|
42
43
|
error?: string;
|
|
43
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* 2.11.1 起:模拟注册只在断了网的沙箱里跑。注册会触发用户挂在 auth.users 上的触发器,触发器经 dblink、
|
|
47
|
+
* postgres_fdw、http 之类的扩展连出去写的东西,不会跟着事务回滚——演练不能替用户往外部系统里写一个测试用户。
|
|
48
|
+
* 由演练(drill.ts)提供:它管着沙箱容器。
|
|
49
|
+
*/
|
|
50
|
+
export interface OfflineSandbox {
|
|
51
|
+
/**
|
|
52
|
+
* 断开沙箱容器的全部网络,用容器里的 psql(连容器自己的 127.0.0.1)跑 script。marker 是这次注册的随机标记
|
|
53
|
+
* (也是 script 里 DO 块的引号标签),被叫停或超时时靠它找到并停掉容器里那次注册。**之后再也不接回网络**:
|
|
54
|
+
* 只确认 SQL 会话都退出了,不能证明触发器没有留下别的东西(还没起来的 docker exec、后台进程),
|
|
55
|
+
* 接回去它们就又能连出去。所以模拟注册是这个沙箱上做的最后一件事。断不开就抛错,一句 SQL 都不跑
|
|
56
|
+
*/
|
|
57
|
+
runOffline(script: string, marker: string, signal?: AbortSignal): Promise<{
|
|
58
|
+
code: number | null;
|
|
59
|
+
stdout: string;
|
|
60
|
+
}>;
|
|
61
|
+
/** 是否已经开始断网(断网后经主机端口的连接都连不上了) */
|
|
62
|
+
offline(): boolean;
|
|
63
|
+
/** 断网之后叫停时用:经 docker exec 终止沙箱里所有客户端会话 */
|
|
64
|
+
stopOfflineSessions(): Promise<void>;
|
|
65
|
+
}
|
|
44
66
|
/** SQLSTATE → 固定说法 + 代码本身;不是合法的 SQLSTATE 就说"意外错误"。恢复端写 Auth 数据出错时也用它 */
|
|
45
67
|
export declare function describeSqlstate(code: unknown): string;
|
|
46
|
-
|
|
68
|
+
/**
|
|
69
|
+
* 模拟注册的 SQL,交给容器里的 psql 跑。注册放在 DO 块的子事务里:出错时接住,把 SQLSTATE、出错位置
|
|
70
|
+
* (错误上下文)、错误消息和 DETAIL 写进四个会话变量(消息和 DETAIL 只用来判断是不是连不上,不进报告);
|
|
71
|
+
* 最后一行输出由我们自己的 select 拼出来——触发器的 NOTICE
|
|
72
|
+
* 只进 stderr,改不了这一行。成功路径最后覆盖写一遍,触发器事先写进去的值不算数。整个事务最后回滚。
|
|
73
|
+
* 美元引号的标签每次随机:插入语句里的列名来自归档,不能让它把 DO 块提前截断
|
|
74
|
+
*/
|
|
75
|
+
export declare function signupScript(userInsert: string, identityInsert: string, marker: string): string;
|
|
76
|
+
/** psql 的输出 → 报告里的 signup。只用 SQLSTATE 和"错在不在触发器里",错误原文不进报告(见 safeError) */
|
|
77
|
+
export declare function signupOutcome(result: {
|
|
78
|
+
code: number | null;
|
|
79
|
+
stdout: string;
|
|
80
|
+
}): NonNullable<AuthDrillReport["signup"]>;
|
|
81
|
+
export declare function runAuthDrill(sandboxConnString: string, manifest: Manifest, mainDumpPath: string, authDump: AuthDumpForDrill, signal?: AbortSignal, sandbox?: OfflineSandbox,
|
|
82
|
+
/** 不跑模拟注册的原因(例如 --keep 要留下一个连得上的沙箱);报告里记 ran: false */
|
|
83
|
+
skipSignup?: string): Promise<AuthDrillReport>;
|
|
47
84
|
/**
|
|
48
85
|
* 终止 Auth 这一节在沙箱里开的会话(按 application_name 认),别人的会话不动。
|
|
49
86
|
* 整个过程(连接、查询、断开)有硬时限:到点直接销毁 socket,沙箱没回应也挂不住进程
|
|
@@ -66,4 +103,6 @@ export declare function authDrillSection(sandboxConnString: string, manifest: Ma
|
|
|
66
103
|
runner?: {
|
|
67
104
|
begin?: () => Promise<void>;
|
|
68
105
|
stop?: AbortSignal;
|
|
106
|
+
sandbox?: OfflineSandbox;
|
|
107
|
+
skipSignup?: string;
|
|
69
108
|
}): Promise<AuthDrillReport | undefined>;
|
package/dist/auth-drill.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
2
|
import { createReadStream } from "node:fs";
|
|
3
3
|
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
@@ -28,6 +28,9 @@ const SQLSTATE_TEXT = {
|
|
|
28
28
|
"57014": "a statement timed out or was cancelled",
|
|
29
29
|
"40P01": "a deadlock",
|
|
30
30
|
P0001: "an exception was raised",
|
|
31
|
+
P0004: "an assertion failed",
|
|
32
|
+
"08001": "a connection couldn't be made",
|
|
33
|
+
"08006": "a connection failed",
|
|
31
34
|
};
|
|
32
35
|
function safeError(error) {
|
|
33
36
|
// 只用 SQLSTATE:触发器能用 RAISE ... USING 伪造 TABLE / COLUMN / CONSTRAINT 这些字段,它们同样不可信
|
|
@@ -126,7 +129,113 @@ async function insertStatement(client, table) {
|
|
|
126
129
|
}
|
|
127
130
|
return `insert into auth.${table} (${names.join(", ")}) values (${values.join(", ")})`;
|
|
128
131
|
}
|
|
129
|
-
|
|
132
|
+
/**
|
|
133
|
+
* 模拟注册的 SQL,交给容器里的 psql 跑。注册放在 DO 块的子事务里:出错时接住,把 SQLSTATE、出错位置
|
|
134
|
+
* (错误上下文)、错误消息和 DETAIL 写进四个会话变量(消息和 DETAIL 只用来判断是不是连不上,不进报告);
|
|
135
|
+
* 最后一行输出由我们自己的 select 拼出来——触发器的 NOTICE
|
|
136
|
+
* 只进 stderr,改不了这一行。成功路径最后覆盖写一遍,触发器事先写进去的值不算数。整个事务最后回滚。
|
|
137
|
+
* 美元引号的标签每次随机:插入语句里的列名来自归档,不能让它把 DO 块提前截断
|
|
138
|
+
*/
|
|
139
|
+
export function signupScript(userInsert, identityInsert, marker) {
|
|
140
|
+
const tag = `$bd_${marker}$`;
|
|
141
|
+
return [
|
|
142
|
+
"set statement_timeout = '5min';",
|
|
143
|
+
"set lock_timeout = '1min';",
|
|
144
|
+
"begin;",
|
|
145
|
+
`do ${tag}`,
|
|
146
|
+
"declare",
|
|
147
|
+
" state text;",
|
|
148
|
+
" context text;",
|
|
149
|
+
" message text;",
|
|
150
|
+
" detail text;",
|
|
151
|
+
"begin",
|
|
152
|
+
" begin",
|
|
153
|
+
" set local role supabase_auth_admin;",
|
|
154
|
+
` ${userInsert};`,
|
|
155
|
+
` ${identityInsert};`,
|
|
156
|
+
// 延迟检查的约束和约束触发器平时在提交时才跑:这里立刻跑完,才算这次注册真的能成
|
|
157
|
+
" set constraints all immediate;",
|
|
158
|
+
// WHEN OTHERS 接不住 ASSERT 失败(P0004),单独接;两个分支做同一件事
|
|
159
|
+
" exception",
|
|
160
|
+
...["assert_failure", "others"].flatMap((condition) => [
|
|
161
|
+
` when ${condition} then`,
|
|
162
|
+
" get stacked diagnostics state = returned_sqlstate, context = pg_exception_context,",
|
|
163
|
+
" message = message_text, detail = pg_exception_detail;",
|
|
164
|
+
" perform set_config('backupdrill.signup_state', state, true);",
|
|
165
|
+
" perform set_config('backupdrill.signup_context', context, true);",
|
|
166
|
+
" perform set_config('backupdrill.signup_message', message, true);",
|
|
167
|
+
" perform set_config('backupdrill.signup_detail', detail, true);",
|
|
168
|
+
" return;",
|
|
169
|
+
]),
|
|
170
|
+
" end;",
|
|
171
|
+
" perform set_config('backupdrill.signup_state', 'ok', true);",
|
|
172
|
+
" perform set_config('backupdrill.signup_context', '', true);",
|
|
173
|
+
" perform set_config('backupdrill.signup_message', '', true);",
|
|
174
|
+
" perform set_config('backupdrill.signup_detail', '', true);",
|
|
175
|
+
"end",
|
|
176
|
+
`${tag};`,
|
|
177
|
+
"select 'backupdrill-signup ' || current_setting('backupdrill.signup_state') || ' ' ||",
|
|
178
|
+
" encode(convert_to(current_setting('backupdrill.signup_context'), 'UTF8'), 'hex') || ' ' ||",
|
|
179
|
+
" encode(convert_to(current_setting('backupdrill.signup_message'), 'UTF8'), 'hex') || ' ' ||",
|
|
180
|
+
" encode(convert_to(current_setting('backupdrill.signup_detail'), 'UTF8'), 'hex');",
|
|
181
|
+
"rollback;",
|
|
182
|
+
"",
|
|
183
|
+
].join("\n");
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* "没测成、可能是因为断网"只认有连不上的证据的情况,其余一律按真的注册失败报(23xxx、P0001、42xxx 等都是真问题)。
|
|
187
|
+
* 证据要在错误消息或 DETAIL 里:解析不了主机名、网络不可达、没有路由、连接被拒、连接超时这类
|
|
188
|
+
* (libc 的措辞不同:glibc 写 "Network is unreachable",musl 写 "Network unreachable",两种都认)。然后:
|
|
189
|
+
* - SQLSTATE 08 类(dblink、postgres_fdw 连不上);同样是 08 类的认证、配置失败(密码不对、没有 pg_hba
|
|
190
|
+
* 条目)没有这类证据,按真失败报;
|
|
191
|
+
* - 或者出错的那条语句(错误上下文的第一行)本身是联网调用(dblink、http 扩展、pg_net)或 COPY ... PROGRAM
|
|
192
|
+
* (http 扩展的 libcurl 错误码是通用的 XX000,程序失败是 38000)。COPY ... PROGRAM 的消息通常只说
|
|
193
|
+
* "程序失败",看不出原因,这时也按真失败报。
|
|
194
|
+
* 上下文是用户函数里出错那条语句的原文,写成别的样子(动态 SQL 等)就认不出,同样按真失败报——宁可报一次
|
|
195
|
+
* 需要人看的失败,也不把真问题藏成"没测成"。错误原文只在这里用来判断,不进报告(见 safeError)
|
|
196
|
+
*/
|
|
197
|
+
const NETWORK_CALL = /\b(dblink\w*|http(_\w+)?|net\.http_\w+)\s*\(|\b(to|from)\s+program\b/i;
|
|
198
|
+
const CONNECTION_FAILURE = /could not translate host name|could not resolve|couldn't resolve|resolve host|bad address|name or service not known|temporary failure in name resolution|network (is )?unreachable|host (is )?unreachable|no route to host|connection refused|connection timed out|timeout expired|timeout was reached|couldn't connect to server/i;
|
|
199
|
+
function failedForLackOfNetwork(state, innermost, evidence) {
|
|
200
|
+
if (!CONNECTION_FAILURE.test(evidence))
|
|
201
|
+
return false;
|
|
202
|
+
if (state.startsWith("08"))
|
|
203
|
+
return true;
|
|
204
|
+
return (state === "XX000" || state === "38000") && NETWORK_CALL.test(innermost);
|
|
205
|
+
}
|
|
206
|
+
/** psql 的输出 → 报告里的 signup。只用 SQLSTATE 和"错在不在触发器里",错误原文不进报告(见 safeError) */
|
|
207
|
+
export function signupOutcome(result) {
|
|
208
|
+
const line = /^backupdrill-signup (\S+) ([0-9a-f]*) ([0-9a-f]*) ([0-9a-f]*)$/m.exec(result.stdout);
|
|
209
|
+
if (result.code !== 0 || !line) {
|
|
210
|
+
// 超时(57014 接不住)或 psql 本身出错:没模拟成
|
|
211
|
+
return { ran: false, detail: "psql did not confirm a sign-up result" };
|
|
212
|
+
}
|
|
213
|
+
const [, state, contextHex, messageHex, detailHex] = line;
|
|
214
|
+
if (state === "ok")
|
|
215
|
+
return { ran: true, ok: true };
|
|
216
|
+
// 去掉我们自己那层 DO 块的上下文,剩下的才是表结构或用户的函数;第一行是真正出错的那条语句
|
|
217
|
+
const where = Buffer.from(contextHex, "hex")
|
|
218
|
+
.toString("utf8")
|
|
219
|
+
.split("\n")
|
|
220
|
+
.filter((l) => !/inline_code_block/.test(l))
|
|
221
|
+
.join("\n");
|
|
222
|
+
// 消息和 DETAIL 只用来判断是不是连不上,不进报告
|
|
223
|
+
const evidence = `${Buffer.from(messageHex, "hex").toString("utf8")}\n${Buffer.from(detailHex, "hex").toString("utf8")}`;
|
|
224
|
+
const triggerPath = /PL\/pgSQL function|SQL function|trigger/i.test(where) ||
|
|
225
|
+
["23503", "23505", "42501", "42883", "42P01", "42703", "3F000", "P0001", "P0004"].includes(state);
|
|
226
|
+
if (triggerPath && failedForLackOfNetwork(state, where.split("\n")[0] ?? "", evidence)) {
|
|
227
|
+
return {
|
|
228
|
+
ran: false,
|
|
229
|
+
detail: `a database operation failed to connect while the sandbox was offline (SQLSTATE ${state})`,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
return triggerPath
|
|
233
|
+
? { ran: true, ok: false, detail: `a new sign-up would fail: ${describeSqlstate(state)}${SIGNUP_TESTED_AS}` }
|
|
234
|
+
: { ran: false, detail: `couldn't simulate a sign-up: ${describeSqlstate(state)}` };
|
|
235
|
+
}
|
|
236
|
+
export async function runAuthDrill(sandboxConnString, manifest, mainDumpPath, authDump, signal, sandbox,
|
|
237
|
+
/** 不跑模拟注册的原因(例如 --keep 要留下一个连得上的沙箱);报告里记 ran: false */
|
|
238
|
+
skipSignup) {
|
|
130
239
|
const auth = manifest.auth;
|
|
131
240
|
// 这一节的所有会话都带上 application_name,超时时 authDrillSection 只终止它们
|
|
132
241
|
sandboxConnString = taggedUrl(sandboxConnString);
|
|
@@ -278,6 +387,7 @@ export async function runAuthDrill(sandboxConnString, manifest, mainDumpPath, au
|
|
|
278
387
|
}
|
|
279
388
|
stopIfCancelled();
|
|
280
389
|
// ── 4. 以 GoTrue 的身份(supabase_auth_admin,非超级用户)模拟一次邮箱注册,事后回滚 ──
|
|
390
|
+
// 断网跑(见 OfflineSandbox):触发器的副作用只能留在沙箱里
|
|
281
391
|
let signup = { ran: false, detail: "no triggers of your own on the Auth tables" };
|
|
282
392
|
if (triggers.created.length) {
|
|
283
393
|
await client.query("alter table auth.users owner to supabase_auth_admin; alter table auth.identities owner to supabase_auth_admin");
|
|
@@ -286,28 +396,27 @@ export async function runAuthDrill(sandboxConnString, manifest, mainDumpPath, au
|
|
|
286
396
|
if (!userInsert || !identityInsert) {
|
|
287
397
|
signup = { ran: false, detail: "couldn't build a test sign-up for this Auth table layout" };
|
|
288
398
|
}
|
|
399
|
+
else if (skipSignup) {
|
|
400
|
+
signup = { ran: false, detail: skipSignup };
|
|
401
|
+
}
|
|
402
|
+
else if (!sandbox) {
|
|
403
|
+
signup = { ran: false, detail: "offline testing is unavailable for this sandbox" };
|
|
404
|
+
}
|
|
289
405
|
else {
|
|
290
|
-
|
|
406
|
+
// 断网之后这条 TCP 连接就断了:先关掉,后面不再用它
|
|
407
|
+
await client.end().catch(() => { });
|
|
408
|
+
client = undefined;
|
|
409
|
+
stopIfCancelled();
|
|
410
|
+
let result = null;
|
|
291
411
|
try {
|
|
292
|
-
|
|
293
|
-
await
|
|
294
|
-
await client.query(identityInsert);
|
|
295
|
-
// 延迟检查的约束和约束触发器平时在提交时才跑:这里立刻跑完,才算这次注册真的能成
|
|
296
|
-
await client.query("set constraints all immediate");
|
|
297
|
-
signup = { ran: true, ok: true };
|
|
412
|
+
const marker = randomBytes(8).toString("hex");
|
|
413
|
+
result = await sandbox.runOffline(signupScript(userInsert, identityInsert, marker), marker, signal);
|
|
298
414
|
}
|
|
299
|
-
catch
|
|
300
|
-
|
|
301
|
-
// 只有错在触发器里才算触发器的问题;造的测试行本身不合表结构,算没模拟成
|
|
302
|
-
const triggerPath = /PL\/pgSQL function|SQL function|trigger/i.test(e.where ?? "") ||
|
|
303
|
-
["23503", "23505", "42501", "42883", "42P01", "42703", "3F000", "P0001"].includes(e.code ?? "");
|
|
304
|
-
signup = triggerPath
|
|
305
|
-
? { ran: true, ok: false, detail: `a new sign-up would fail: ${safeError(e)}${SIGNUP_TESTED_AS}` }
|
|
306
|
-
: { ran: false, detail: `couldn't simulate a sign-up: ${safeError(e)}` };
|
|
307
|
-
}
|
|
308
|
-
finally {
|
|
309
|
-
await client.query("rollback").catch(() => { });
|
|
415
|
+
catch {
|
|
416
|
+
signup = { ran: false, detail: "the offline test stopped before returning a result" };
|
|
310
417
|
}
|
|
418
|
+
if (result)
|
|
419
|
+
signup = signupOutcome(result);
|
|
311
420
|
}
|
|
312
421
|
if (signup.ran && !signup.ok)
|
|
313
422
|
problems.push(signup.detail);
|
|
@@ -412,7 +521,11 @@ runner = {}) {
|
|
|
412
521
|
if (started) {
|
|
413
522
|
void (async () => {
|
|
414
523
|
await waitAtMost(started, 10_000);
|
|
415
|
-
|
|
524
|
+
// 模拟注册已经把沙箱断了网:经主机端口连不上了,改经 docker exec 终止
|
|
525
|
+
if (runner.sandbox?.offline())
|
|
526
|
+
await runner.sandbox.stopOfflineSessions();
|
|
527
|
+
else
|
|
528
|
+
await stopAuthSessions(sandboxConnString);
|
|
416
529
|
})();
|
|
417
530
|
}
|
|
418
531
|
};
|
|
@@ -434,7 +547,7 @@ runner = {}) {
|
|
|
434
547
|
// 等 begin 的时候已经被叫停或到点了:比赛已经有了结果,这里不再开始
|
|
435
548
|
if (cancel.signal.aborted)
|
|
436
549
|
return stoppedByRunner();
|
|
437
|
-
work = runAuthDrill(sandboxConnString, manifest, mainDumpPath, await authDump, cancel.signal);
|
|
550
|
+
work = runAuthDrill(sandboxConnString, manifest, mainDumpPath, await authDump, cancel.signal, runner.sandbox, runner.skipSignup);
|
|
438
551
|
return await work;
|
|
439
552
|
})(),
|
|
440
553
|
new Promise((resolve) => {
|
package/dist/auth-restore.d.ts
CHANGED
|
@@ -44,6 +44,11 @@ export declare function psqlProblem(): Promise<string | null>;
|
|
|
44
44
|
* 返回 "more"。不是它就不交给 psql:没有受限模式,归档内容里的元命令会被执行
|
|
45
45
|
*/
|
|
46
46
|
export declare function restrictHeader(text: string): "ok" | "missing" | "more";
|
|
47
|
+
/**
|
|
48
|
+
* 失败说明 "<阶段> failed: <主错误>" 里的主错误(按已知阶段前缀去掉);别的形状的失败原样返回。
|
|
49
|
+
* 给 restoreFailureAdvice 用:它只看主错误开头,语句正文里碰巧出现的字不能触发建议
|
|
50
|
+
*/
|
|
51
|
+
export declare function primaryAuthRestoreError(failure: string): string;
|
|
47
52
|
/**
|
|
48
53
|
* 等 stdin 能再写(drain)或者 psql 已经退出。监听用完就摘掉:大库恢复要等几十万次,
|
|
49
54
|
* 挂在一个直到最后才结束的 Promise 上的回调会一直攒着
|
package/dist/auth-restore.js
CHANGED
|
@@ -201,6 +201,17 @@ const STAGE_TEXT = {
|
|
|
201
201
|
commit: "committing",
|
|
202
202
|
committed: "committing",
|
|
203
203
|
};
|
|
204
|
+
/**
|
|
205
|
+
* 失败说明 "<阶段> failed: <主错误>" 里的主错误(按已知阶段前缀去掉);别的形状的失败原样返回。
|
|
206
|
+
* 给 restoreFailureAdvice 用:它只看主错误开头,语句正文里碰巧出现的字不能触发建议
|
|
207
|
+
*/
|
|
208
|
+
export function primaryAuthRestoreError(failure) {
|
|
209
|
+
for (const text of [...Object.values(STAGE_TEXT), "connecting to the target"]) {
|
|
210
|
+
if (failure.startsWith(`${text} failed: `))
|
|
211
|
+
return failure.slice(text.length + " failed: ".length);
|
|
212
|
+
}
|
|
213
|
+
return failure;
|
|
214
|
+
}
|
|
204
215
|
const STAGE_MARK = "backupdrill-stage:";
|
|
205
216
|
const XID_MARK = "backupdrill-xid:";
|
|
206
217
|
/**
|
package/dist/backup.js
CHANGED
|
@@ -5,6 +5,7 @@ import { promisify } from "node:util";
|
|
|
5
5
|
import { s3SnapshotStore } from "./snapshot-store.js";
|
|
6
6
|
import { MANIFEST_SCHEMA_VERSION } from "./manifest.js";
|
|
7
7
|
import { AUTH_DUMP_MAX_BYTES, authDumpArgs, exportSharedSnapshot, inspectAuth, authInSchemas, manualAuthObjects, redactValues, } from "./auth-backup.js";
|
|
8
|
+
import { scrubPendingAuthTokens } from "./auth-archive.js";
|
|
8
9
|
import { syncStorage } from "./storage.js";
|
|
9
10
|
import { inspectBucketAttrs } from "./storage-catalog.js";
|
|
10
11
|
import { projectRefOf, refFromStorageEndpoint, assertNoHostOverride } from "./restore-engine.js";
|
|
@@ -174,15 +175,35 @@ async function streamPgDump(opts) {
|
|
|
174
175
|
});
|
|
175
176
|
// endOnlyOnSuccess(auth.dump 用):pg_dump 成功退出才结束上传流,失败就带错中断,上传因此完不成——
|
|
176
177
|
// 不在用户桶里留下一个空的或半截的 auth.dump。主 dump 不传这个选项,行为与之前一致
|
|
177
|
-
dump.stdout
|
|
178
|
+
const output = opts.rewrite ? opts.rewrite(dump.stdout) : dump.stdout;
|
|
179
|
+
output.pipe(meter, { end: !opts.endOnlyOnSuccess });
|
|
178
180
|
if (opts.endOnlyOnSuccess) {
|
|
181
|
+
// 改写的那一步要把最后一段也交给 meter 才算完:pg_dump 0 退出 + 改写端读完,两样都到了才结束上传流
|
|
182
|
+
const rewriteFinished = opts.rewrite
|
|
183
|
+
? new Promise((resolve) => output.once("end", resolve))
|
|
184
|
+
: Promise.resolve();
|
|
179
185
|
dump.on("close", (code) => {
|
|
180
186
|
if (code === 0)
|
|
181
|
-
meter.end();
|
|
187
|
+
void rewriteFinished.then(() => meter.end());
|
|
182
188
|
else
|
|
183
189
|
meter.destroy(new Error(`pg_dump exited with code ${code}`));
|
|
184
190
|
});
|
|
185
191
|
}
|
|
192
|
+
if (opts.rewrite) {
|
|
193
|
+
// 改写失败 = 没能抹干净:这份归档绝不能上传成功。pg_dump 自己中途失败时改写端也会先报"归档被截断",
|
|
194
|
+
// 那时等它退出(最多 2 秒),原因以 pg_dump 的报错为准(上面的 close 处理);否则停掉 pg_dump、带着改写的错误中止
|
|
195
|
+
output.on("error", (error) => {
|
|
196
|
+
void (async () => {
|
|
197
|
+
if (dump.exitCode === null && dump.signalCode === null) {
|
|
198
|
+
await waitAtMost(new Promise((resolve) => dump.once("close", resolve)), 2000);
|
|
199
|
+
}
|
|
200
|
+
if (dump.exitCode !== null && dump.exitCode !== 0)
|
|
201
|
+
return;
|
|
202
|
+
dump.kill("SIGKILL");
|
|
203
|
+
meter.destroy(error);
|
|
204
|
+
})();
|
|
205
|
+
});
|
|
206
|
+
}
|
|
186
207
|
const dumpDone = new Promise((resolve, reject) => {
|
|
187
208
|
dump.on("error", reject); // 启动失败(如 ENOENT)
|
|
188
209
|
dump.on("close", (code) => code === 0
|
|
@@ -212,6 +233,8 @@ async function streamPgDump(opts) {
|
|
|
212
233
|
dumpDone.catch(() => { }); // 没走到 Promise.all 时,被杀的 pg_dump 的退出也得有人接住
|
|
213
234
|
dump.kill("SIGKILL");
|
|
214
235
|
meter.destroy();
|
|
236
|
+
if (output !== dump.stdout)
|
|
237
|
+
output.destroy();
|
|
215
238
|
// 被叫停时中止上传但不等它(可能正卡在那)。可叫停的调用(Auth、共享快照那次)自己失败时,
|
|
216
239
|
// 等中止最多 5 秒——中止本身也可能卡住,不能让调用方跟着卡。主 dump 的老路径照旧等中止完成
|
|
217
240
|
if (opts.signal?.aborted)
|
|
@@ -269,6 +292,12 @@ function shortReason(error) {
|
|
|
269
292
|
* 主 dump 永远不因 Auth 受影响。返回 dump 时,共享快照已经导出,调用方负责释放。
|
|
270
293
|
*/
|
|
271
294
|
async function planAuth(config, writer, mode) {
|
|
295
|
+
// 2.11.1 起只有 auth.dump 抹掉待处理 token;schemas 里明写了 auth 时,主 dump 里的 Auth 表还是原样(含 token),
|
|
296
|
+
// 每次备份提醒一句。不管 Auth 备份开没开都提醒:主 dump 的内容和它无关
|
|
297
|
+
if (authInSchemas(config.schemas) === "yes") {
|
|
298
|
+
log.warn("Your schema list includes auth, so dump.pgcustom holds the Auth tables as they are, " +
|
|
299
|
+
"including pending password-reset and confirmation tokens. Only auth.dump has them cleared.");
|
|
300
|
+
}
|
|
272
301
|
if (mode === "off")
|
|
273
302
|
return { kind: "none" };
|
|
274
303
|
// schemas 已经(可能)把 auth 带进主 dump:今天怎样以后还怎样,不另备一份。
|
|
@@ -322,6 +351,17 @@ async function prepareAuthDump(config, writer, mode, signal) {
|
|
|
322
351
|
await shared.release();
|
|
323
352
|
return { kind: "settled", section: authNotDone(mode, inspection.reason) };
|
|
324
353
|
}
|
|
354
|
+
// auth.users 上有不是 Supabase 自带的约束、唯一索引或生成列:抹过的 auth.dump 可能恢复不了,不写(见 firstNonStockObject)
|
|
355
|
+
if (inspection.nonStockObject) {
|
|
356
|
+
await shared.release();
|
|
357
|
+
return {
|
|
358
|
+
kind: "settled",
|
|
359
|
+
section: {
|
|
360
|
+
status: "failed",
|
|
361
|
+
reason: `BackupDrill stopped before writing auth.dump. Clearing pending Auth tokens could cause a restore to fail because of ${inspection.nonStockObject} on auth.users.`,
|
|
362
|
+
},
|
|
363
|
+
};
|
|
364
|
+
}
|
|
325
365
|
const uploadAuthDump = writer.uploadAuthDump.bind(writer);
|
|
326
366
|
return { kind: "dump", shared, inspection, key: writer.authDumpKey, upload: (body) => uploadAuthDump(body) };
|
|
327
367
|
}
|
|
@@ -348,6 +388,8 @@ async function dumpWithAuth(config, writer, pgDumpBin, relationBytes, mode, plan
|
|
|
348
388
|
maxBytes: AUTH_DUMP_MAX_BYTES,
|
|
349
389
|
signal: authAbort.signal,
|
|
350
390
|
endOnlyOnSuccess: true,
|
|
391
|
+
// 2.11.1 起:待处理的找回密码、邮箱确认等 token 不进桶(见 auth-archive.ts)
|
|
392
|
+
rewrite: scrubPendingAuthTokens,
|
|
351
393
|
}).then((result) => ({ ok: true, ...result }), (error) => ({ ok: false, error }));
|
|
352
394
|
void authRun.finally(() => markAuthStarted());
|
|
353
395
|
// 看门狗:主 dump 迟迟没开始,就是被 Auth 的连接挤住了,先让路(见 AUTH_TIMEOUTS.mainStartGraceMs)
|
|
@@ -480,7 +522,8 @@ function logAuthOutcome(auth, schemas) {
|
|
|
480
522
|
}
|
|
481
523
|
return;
|
|
482
524
|
}
|
|
483
|
-
|
|
525
|
+
// 有的原因本身就是完整的句子(以句号结尾),后面还要接一句,去掉它的句号免得出现 ".."
|
|
526
|
+
const why = (AUTH_SKIP_TEXT[auth.reason ?? ""] ?? auth.reason ?? "unknown reason").replace(/\.$/, "");
|
|
484
527
|
if (auth.status === "skipped") {
|
|
485
528
|
// 不是 Supabase 库时这是正常情况,不该显得像个问题
|
|
486
529
|
if (auth.reason === "no_auth_schema")
|
package/dist/drill.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { Manifest } from "./manifest.js";
|
|
|
3
3
|
import { type SnapshotStore } from "./snapshot-store.js";
|
|
4
4
|
import { type OrphanedForeignKey } from "./orphaned-foreign-keys.js";
|
|
5
5
|
import { type GrantRestore } from "./grants.js";
|
|
6
|
-
import { type AuthDrillReport, type AuthDumpForDrill } from "./auth-drill.js";
|
|
6
|
+
import { type AuthDrillReport, type AuthDumpForDrill, type OfflineSandbox } from "./auth-drill.js";
|
|
7
7
|
/**
|
|
8
8
|
* 无放回随机抽样:部分 Fisher-Yates,只洗出前 n 位(O(n) 次交换,不必全 shuffle)。
|
|
9
9
|
* 此前恒取 manifest 顺序的前 CAP 个 → 超过 CAP 的项目里排序靠后的文件永远不被校验;
|
|
@@ -44,9 +44,11 @@ export interface DrillReport {
|
|
|
44
44
|
restoredTableCount: number;
|
|
45
45
|
restoredRowTotal: number;
|
|
46
46
|
/** --keep 且演练失败时:被保留的沙箱(供排查/脚本消费);其余情况不存在 */
|
|
47
|
+
/** inspect(2.11.1 起):进沙箱的 docker exec 命令 */
|
|
47
48
|
keptSandbox?: {
|
|
48
49
|
containerId: string;
|
|
49
50
|
connString: string;
|
|
51
|
+
inspect?: string;
|
|
50
52
|
};
|
|
51
53
|
/** 源库自带孤儿行的外键:演练和真实恢复都把它们建成 NOT VALID,不算失败。没有时不出现 */
|
|
52
54
|
orphanedForeignKeys?: OrphanedForeignKey[];
|
|
@@ -65,6 +67,31 @@ export interface SandboxTuning {
|
|
|
65
67
|
}
|
|
66
68
|
export declare function sandboxTuning(totalMemBytes: number, cpus?: number): SandboxTuning;
|
|
67
69
|
export declare function sandboxDataVolumeTarget(major: number): string;
|
|
70
|
+
/**
|
|
71
|
+
* 模拟注册本地那一侧(docker exec 客户端)最多等多久;导出供测试调短,产品里不改。
|
|
72
|
+
* launchPauseSeconds 只给测试用:在启动脚本"看过取消标记"和"exec psql"之间停一会儿,复现最坏的时序
|
|
73
|
+
*/
|
|
74
|
+
export declare const OFFLINE_SIGNUP_LIMITS: {
|
|
75
|
+
psqlMs: number;
|
|
76
|
+
launchPauseSeconds: number;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* 模拟注册用的"断网跑 SQL"(见 auth-drill.ts OfflineSandbox)。
|
|
80
|
+
*
|
|
81
|
+
* 为什么是临时断网,而不是一开始就不给沙箱联网:CLI 和 pg_restore 都从主机经发布的端口连沙箱,
|
|
82
|
+
* --network none 和 --internal 网络都发布不了端口。所以恢复、校验照旧,只在最后的模拟注册之前把容器从所有
|
|
83
|
+
* 网络上摘下来(只剩 loopback),经 docker exec 里的 psql 连 127.0.0.1 跑(见 signupLauncher)。
|
|
84
|
+
*
|
|
85
|
+
* **断了就不再接回**:能确认的只有会话和那个 psql 都没了,确认不了触发器有没有留下别的东西(后台进程等),
|
|
86
|
+
* 一接回它们就又能连出去。所以模拟注册是这个沙箱上的最后一件事,跑完沙箱就销毁。--keep 要留下沙箱时
|
|
87
|
+
* 根本不跑模拟注册(见 drillDump),留下的沙箱和 2.11.0 一样连得上。
|
|
88
|
+
* 每一步之前都看一眼是否已被叫停:叫停后(报告可能已经出了)不再断开任何网络。摘不干净就不跑。
|
|
89
|
+
*/
|
|
90
|
+
/** @internal 演练内部用;导出只为测试能直接对一个容器验证断网、叫停和超时收尾 */
|
|
91
|
+
export declare function offlineSandbox(pg: {
|
|
92
|
+
containerId: string;
|
|
93
|
+
offline: boolean;
|
|
94
|
+
}): OfflineSandbox;
|
|
68
95
|
/** post-data 第二遍恢复的结果:用户对象失败 vs Supabase 托管对象的沙箱预期跳过。 */
|
|
69
96
|
interface PostDataResult {
|
|
70
97
|
supabaseSkipped: number;
|
|
@@ -74,6 +101,8 @@ interface PostDataResult {
|
|
|
74
101
|
/** 授权重建结果;失败时不存在,原因在 failures 里 */
|
|
75
102
|
grants?: GrantRestore;
|
|
76
103
|
}
|
|
104
|
+
/** pre-data 失败里有没有"沙箱不认 pg_attver"这一条(导出给测试) */
|
|
105
|
+
export declare function sandboxRejectsAttver(preDataFailures: string[]): boolean;
|
|
77
106
|
/** 兼容层:历史签名保留,分类逻辑的单一权威在统一引擎(restore-engine.ts)。 */
|
|
78
107
|
export declare function classifyPostDataErrors(stderr: string): PostDataResult;
|
|
79
108
|
/** 兼容层:见 restore-engine.ts finalizePass(非零退出且零错误块 = 通用失败)。 */
|