backupdrill 0.2.0 → 0.2.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 CHANGED
@@ -150,13 +150,15 @@ Backups record your installed Postgres extensions in the manifest, and the drill
150
150
 
151
151
  ### Run your own checks against the restored copy
152
152
 
153
- The drill's structural checks (tables, rows, checksums) can't know what "working" means for *your* app — RLS behavior and business workflows only have answers in your code. `--check-cmd` hands you the restored sandbox before it's destroyed:
153
+ The drill's structural checks (tables, rows, checksums) can't know what "working" means for *your* app — business invariants only have answers in your code. `--check-cmd` hands you the restored sandbox before it's destroyed:
154
154
 
155
155
  ```bash
156
156
  backupdrill drill --check-cmd "npm run smoke" # BACKUPDRILL_SANDBOX_URL points at the restored copy
157
157
  ```
158
158
 
159
- Your command runs after all structural checks pass, with `BACKUPDRILL_SANDBOX_URL` set to the sandbox's connection string. Exit 0 means pass; the result shows up in the report as an `app checks` line, and a non-zero exit fails the drill (the report keeps structural and app-level failures clearly separate). Entirely optional — without the flag, nothing changes and the report has no `app checks` line. There's a 10-minute timeout, and `--keep` leaves the sandbox container running when a drill fails so you can inspect it (`docker rm -f <id>` when done).
159
+ Your command runs after all structural checks pass, with `BACKUPDRILL_SANDBOX_URL` set to the sandbox's connection string. Exit 0 means pass; the result shows up in the report as an `app checks` line, and a non-zero exit fails the drill (the report keeps structural and app-level failures clearly separate). Entirely optional — without the flag, nothing changes and the report has no `app checks` line. There's a 10-minute timeout (the whole process tree is killed), and `--keep` leaves the sandbox container running when a drill fails so you can inspect it — the report JSON then includes a `keptSandbox` object with the container id and connection string (`docker rm -f <id>` when done).
160
+
161
+ **What this can and cannot verify, honestly:** `BACKUPDRILL_SANDBOX_URL` connects as the sandbox **superuser**, and superusers bypass row-level security — plus policies referencing Supabase-managed roles (`authenticated`, `anon`) are skipped during restore, since those roles don't exist outside the platform. So use `--check-cmd` for data and business invariants ("orders reference existing users", "the row count I care about is sane"). It is **not** an RLS behavior test — verifying RLS takes an authenticated client stack with real JWTs, which no restore sandbox can fake generically.
160
162
 
161
163
  When you actually need to recover, `restore` puts the database back into a target you name and pulls the Storage files down to a local folder:
162
164
 
package/dist/drill.d.ts CHANGED
@@ -20,6 +20,11 @@ export interface DrillReport {
20
20
  restoreSeconds: number;
21
21
  restoredTableCount: number;
22
22
  restoredRowTotal: number;
23
+ /** --keep 且演练失败时:被保留的沙箱(供排查/脚本消费);其余情况不存在 */
24
+ keptSandbox?: {
25
+ containerId: string;
26
+ connString: string;
27
+ };
23
28
  }
24
29
  /** post-data 第二遍恢复的结果:用户对象失败 vs Supabase 托管对象的沙箱预期跳过。 */
25
30
  interface PostDataResult {
@@ -36,8 +41,9 @@ export declare function postDataResult(code: number | null, stderr: string): Pos
36
41
  export declare const APP_CHECK_TIMEOUT_MS: number;
37
42
  /**
38
43
  * 语义层钩子:结构检查全绿后、销毁沙箱前,把沙箱连接串经 BACKUPDRILL_SANDBOX_URL
39
- * 交给用户命令执行(RLS 行为、业务流可用性——判据只存在于应用侧,引擎不解释
40
- * 命令输出,只裁决退出码:0 = pass)。
44
+ * 交给用户命令执行,校验数据/业务不变量(判据只存在于应用侧,引擎不解释命令输出,
45
+ * 只裁决退出码:0 = pass)。注意这不是 RLS 行为测试:连接身份是沙箱超级用户
46
+ * (绕过 RLS),且引用 Supabase 托管角色的策略在恢复时已被归类跳过。
41
47
  * 子进程输出定向到 stderr:stdout 是 CLI 的机器可读 JSON 通道,不能被污染。
42
48
  * 红线:托管 worker 绝不能把用户输入传到这里(任意代码执行);此钩子只属于
43
49
  * 用户在自己机器上运行的 CLI(见 PRD §1.4 演练后应用自检钩子)。
package/dist/drill.js CHANGED
@@ -313,8 +313,9 @@ async function verifyStorage(s3, config, manifest, verifyAll) {
313
313
  export const APP_CHECK_TIMEOUT_MS = 10 * 60 * 1000;
314
314
  /**
315
315
  * 语义层钩子:结构检查全绿后、销毁沙箱前,把沙箱连接串经 BACKUPDRILL_SANDBOX_URL
316
- * 交给用户命令执行(RLS 行为、业务流可用性——判据只存在于应用侧,引擎不解释
317
- * 命令输出,只裁决退出码:0 = pass)。
316
+ * 交给用户命令执行,校验数据/业务不变量(判据只存在于应用侧,引擎不解释命令输出,
317
+ * 只裁决退出码:0 = pass)。注意这不是 RLS 行为测试:连接身份是沙箱超级用户
318
+ * (绕过 RLS),且引用 Supabase 托管角色的策略在恢复时已被归类跳过。
318
319
  * 子进程输出定向到 stderr:stdout 是 CLI 的机器可读 JSON 通道,不能被污染。
319
320
  * 红线:托管 worker 绝不能把用户输入传到这里(任意代码执行);此钩子只属于
320
321
  * 用户在自己机器上运行的 CLI(见 PRD §1.4 演练后应用自检钩子)。
@@ -326,10 +327,22 @@ export function runAppCheck(command, connString, timeoutMs = APP_CHECK_TIMEOUT_M
326
327
  shell: true,
327
328
  stdio: ["ignore", 2, 2],
328
329
  env: { ...process.env, BACKUPDRILL_SANDBOX_URL: connString },
329
- timeout: timeoutMs,
330
- killSignal: "SIGKILL",
330
+ // 独立进程组:超时必须杀整棵进程树。只杀 shell 的话,npm/测试运行器这类
331
+ // 孙进程会在沙箱销毁后继续跑(xreview 抓出的资源泄漏)。
332
+ detached: true,
331
333
  });
334
+ let timedOut = false;
335
+ const timer = setTimeout(() => {
336
+ timedOut = true;
337
+ try {
338
+ process.kill(-proc.pid, "SIGKILL"); // 负 pid = 杀整个进程组
339
+ }
340
+ catch {
341
+ proc.kill("SIGKILL");
342
+ }
343
+ }, timeoutMs);
332
344
  proc.on("error", (error) => {
345
+ clearTimeout(timer);
333
346
  resolve({
334
347
  name: "app checks",
335
348
  pass: false,
@@ -337,12 +350,21 @@ export function runAppCheck(command, connString, timeoutMs = APP_CHECK_TIMEOUT_M
337
350
  });
338
351
  });
339
352
  proc.on("close", (code, signal) => {
353
+ clearTimeout(timer);
340
354
  const seconds = Math.round((Date.now() - started) / 100) / 10;
355
+ if (timedOut) {
356
+ resolve({
357
+ name: "app checks",
358
+ pass: false,
359
+ detail: `timed out after ${timeoutMs / 1000}s — process tree killed`,
360
+ });
361
+ return;
362
+ }
341
363
  if (signal) {
342
364
  resolve({
343
365
  name: "app checks",
344
366
  pass: false,
345
- detail: `terminated by ${signal} after ${seconds}s (timeout ${timeoutMs / 1000}s)`,
367
+ detail: `terminated by ${signal} after ${seconds}s`,
346
368
  });
347
369
  return;
348
370
  }
@@ -362,6 +384,12 @@ export function runAppCheck(command, connString, timeoutMs = APP_CHECK_TIMEOUT_M
362
384
  * 与 S3 解耦,便于离线测试。`preChecks` 用于把下载阶段的校验(如归档校验和)带进报告。
363
385
  */
364
386
  export async function drillDump(dumpPath, manifest, snapshot, preChecks = [], opts = {}) {
387
+ // 空/纯空白命令 = 危险的假配置(CI 里变量没展开的典型形态):跑了会以 exit 0
388
+ // 假通过,忽略会让用户以为检查在跑。拒绝,且要在起沙箱之前拒绝。
389
+ const appCheckCommand = opts.appCheckCommand?.trim();
390
+ if (opts.appCheckCommand !== undefined && !appCheckCommand) {
391
+ throw new Error("--check-cmd is empty (did an env var fail to expand?) — refusing to report a check that would never run");
392
+ }
365
393
  const checks = [...preChecks];
366
394
  const pg = await startEphemeralPostgres(sandboxImage(manifest));
367
395
  let pass = false; // 异常路径视为失败,供 finally 决定 --keep 是否保留沙箱
@@ -414,12 +442,12 @@ export async function drillDump(dumpPath, manifest, snapshot, preChecks = [], op
414
442
  checks.push(...verify.checks);
415
443
  // 语义层钩子:只在结构层全绿时才跑——结构已失败时再跑应用检查,只会把
416
444
  // 一次失败报成两层失败,误导排查方向。没配置则不跑、报告里不出现该行。
417
- if (opts.appCheckCommand && checks.every((c) => c.pass)) {
445
+ if (appCheckCommand && checks.every((c) => c.pass)) {
418
446
  log.step("Running app checks…");
419
- checks.push(await runAppCheck(opts.appCheckCommand, pg.connString));
447
+ checks.push(await runAppCheck(appCheckCommand, pg.connString));
420
448
  }
421
449
  pass = checks.every((c) => c.pass);
422
- return {
450
+ const report = {
423
451
  snapshot,
424
452
  pass,
425
453
  checks,
@@ -427,6 +455,20 @@ export async function drillDump(dumpPath, manifest, snapshot, preChecks = [], op
427
455
  restoredTableCount: verify.tableCount,
428
456
  restoredRowTotal: verify.rowTotal,
429
457
  };
458
+ if (opts.keepSandboxOnFailure && !pass) {
459
+ // 写进报告让 --keep 可被脚本消费(拿连接串排查),不只靠人眼看日志
460
+ report.keptSandbox = { containerId: pg.containerId, connString: pg.connString };
461
+ }
462
+ return report;
463
+ }
464
+ catch (error) {
465
+ // 异常路径(恢复/校验抛出)没有报告对象可挂 keptSandbox;把沙箱坐标并入
466
+ // 错误信息,让脚本和人都拿得到被保留容器的清理线索
467
+ if (opts.keepSandboxOnFailure) {
468
+ error.message +=
469
+ ` [sandbox kept: ${pg.connString} — remove with: docker rm -f ${pg.containerId.slice(0, 12)}]`;
470
+ }
471
+ throw error;
430
472
  }
431
473
  finally {
432
474
  if (opts.keepSandboxOnFailure && !pass) {
package/dist/index.js CHANGED
@@ -76,10 +76,16 @@ program
76
76
  .option("--project-name <name>", "project name used in the object key")
77
77
  .option("--verify-all-files", "checksum every Storage file, not just a sample")
78
78
  .option("--check-cmd <command>", "after structural checks pass, run your own smoke test against the restored sandbox " +
79
- "(connection string in BACKUPDRILL_SANDBOX_URL; exit 0 = pass, reported as 'app checks')")
79
+ "(connection string in BACKUPDRILL_SANDBOX_URL; exit 0 = pass, reported as 'app checks'). " +
80
+ "Note: connects as the sandbox superuser, so RLS policies are NOT enforced — " +
81
+ "verify data and business invariants here, not RLS behavior")
80
82
  .option("--keep", "if the drill fails, keep the sandbox container running for inspection")
81
83
  .action(async (opts) => {
82
84
  try {
85
+ if (opts.checkCmd !== undefined && !String(opts.checkCmd).trim()) {
86
+ throw new Error("--check-cmd is empty (did an env var fail to expand?) — refusing to run a drill " +
87
+ "that would report a check that never executes");
88
+ }
83
89
  const config = await loadConfig({
84
90
  configPath: opts.config,
85
91
  overrides: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backupdrill",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Open-source CLI to back up a Supabase project — Postgres database and Storage files — to your own S3/R2/B2 bucket, with a checksummed manifest.",
5
5
  "type": "module",
6
6
  "bin": {