@lark-apaas/miaoda-cli 0.1.2 → 0.1.3-alpha.40be425

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.
Files changed (70) hide show
  1. package/README.md +8 -7
  2. package/dist/api/app/api.js +25 -0
  3. package/dist/api/app/index.js +15 -0
  4. package/dist/api/app/schemas.js +79 -0
  5. package/dist/api/app/types.js +58 -0
  6. package/dist/api/db/api.js +331 -24
  7. package/dist/api/db/client.js +36 -0
  8. package/dist/api/db/index.js +11 -1
  9. package/dist/api/deploy/api.js +60 -0
  10. package/dist/api/deploy/index.js +16 -0
  11. package/dist/api/deploy/schemas.js +105 -0
  12. package/dist/api/deploy/types.js +22 -0
  13. package/dist/api/file/api.js +25 -23
  14. package/dist/api/file/index.js +2 -1
  15. package/dist/api/file/parsers.js +14 -3
  16. package/dist/api/index.js +7 -1
  17. package/dist/api/observability/api.js +52 -0
  18. package/dist/api/observability/index.js +16 -0
  19. package/dist/api/observability/schemas.js +60 -0
  20. package/dist/api/observability/types.js +27 -0
  21. package/dist/cli/commands/app/index.js +62 -0
  22. package/dist/cli/commands/db/index.js +577 -5
  23. package/dist/cli/commands/deploy/index.js +155 -0
  24. package/dist/cli/commands/file/index.js +22 -7
  25. package/dist/cli/commands/index.js +10 -6
  26. package/dist/cli/commands/observability/index.js +240 -0
  27. package/dist/cli/commands/shared.js +83 -7
  28. package/dist/cli/handlers/app/get.js +47 -0
  29. package/dist/cli/handlers/app/index.js +7 -0
  30. package/dist/cli/handlers/app/update.js +59 -0
  31. package/dist/cli/handlers/db/audit.js +294 -0
  32. package/dist/cli/handlers/db/changelog.js +130 -0
  33. package/dist/cli/handlers/db/data.js +6 -6
  34. package/dist/cli/handlers/db/index.js +17 -1
  35. package/dist/cli/handlers/db/migration.js +235 -0
  36. package/dist/cli/handlers/db/quota.js +68 -0
  37. package/dist/cli/handlers/db/recovery.js +357 -0
  38. package/dist/cli/handlers/db/schema.js +2 -3
  39. package/dist/cli/handlers/db/sql.js +2 -3
  40. package/dist/cli/handlers/deploy/deploy.js +84 -0
  41. package/dist/cli/handlers/deploy/error-log.js +60 -0
  42. package/dist/cli/handlers/deploy/format.js +39 -0
  43. package/dist/cli/handlers/deploy/get.js +71 -0
  44. package/dist/cli/handlers/deploy/helpers.js +41 -0
  45. package/dist/cli/handlers/deploy/history.js +70 -0
  46. package/dist/cli/handlers/deploy/index.js +14 -0
  47. package/dist/cli/handlers/deploy/polling.js +162 -0
  48. package/dist/cli/handlers/file/cp.js +1 -2
  49. package/dist/cli/handlers/file/index.js +3 -1
  50. package/dist/cli/handlers/file/ls.js +1 -2
  51. package/dist/cli/handlers/file/quota.js +66 -0
  52. package/dist/cli/handlers/file/rm.js +1 -2
  53. package/dist/cli/handlers/file/sign.js +1 -2
  54. package/dist/cli/handlers/file/stat.js +3 -3
  55. package/dist/cli/handlers/observability/analytics.js +212 -0
  56. package/dist/cli/handlers/observability/helpers.js +66 -0
  57. package/dist/cli/handlers/observability/index.js +12 -0
  58. package/dist/cli/handlers/observability/log.js +94 -0
  59. package/dist/cli/handlers/observability/metric.js +208 -0
  60. package/dist/cli/handlers/observability/trace.js +102 -0
  61. package/dist/main.js +6 -2
  62. package/dist/utils/args.js +8 -0
  63. package/dist/utils/devops-error.js +28 -0
  64. package/dist/utils/git.js +29 -0
  65. package/dist/utils/http.js +118 -0
  66. package/dist/utils/index.js +15 -1
  67. package/dist/utils/output.js +360 -7
  68. package/dist/utils/poll.js +27 -0
  69. package/dist/utils/time.js +208 -0
  70. package/package.json +7 -5
@@ -1,7 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.registerDbCommands = registerDbCommands;
4
+ const error_1 = require("../../../utils/error");
4
5
  const index_1 = require("../../../cli/handlers/db/index");
6
+ const shared_1 = require("../../../cli/commands/shared");
7
+ function parsePositiveInt(raw) {
8
+ const n = Number(raw);
9
+ if (!Number.isInteger(n) || n < 1) {
10
+ throw new error_1.AppError("ARGS_INVALID", `--limit must be a positive integer (got '${raw}')`);
11
+ }
12
+ return n;
13
+ }
5
14
  function registerDbCommands(program) {
6
15
  const dbCmd = program
7
16
  .command("db")
@@ -22,7 +31,7 @@ function registerDbCommands(program) {
22
31
  .usage("<query> [flags]")
23
32
  .argument("[query]", "要执行的 SQL 语句;省略时从标准输入读取")
24
33
  .action(async function (query) {
25
- await (0, index_1.handleDbSql)(query, this.optsWithGlobals());
34
+ await (0, index_1.handleDbSql)(query, { ...this.optsWithGlobals(), appId: (0, shared_1.resolveAppId)({}) });
26
35
  })
27
36
  .addHelpText("after", `
28
37
  Notes:
@@ -73,7 +82,7 @@ Examples:
73
82
  "查看单表完整结构请用 `schema get`;查看 DDL 变更历史请用 `changelog`(P1)。")
74
83
  .usage("[flags]")
75
84
  .action(async function () {
76
- await (0, index_1.handleDbSchemaList)(this.optsWithGlobals());
85
+ await (0, index_1.handleDbSchemaList)({ ...this.optsWithGlobals(), appId: (0, shared_1.resolveAppId)({}) });
77
86
  })
78
87
  .addHelpText("after", `
79
88
  Examples:
@@ -103,7 +112,7 @@ Examples:
103
112
  .argument("<table>", "表名(无需带 schema 前缀)")
104
113
  .option("--ddl", "强制输出 CREATE TABLE 建表语句(pretty 默认就是 DDL,--json 时配合此 flag 返 SQL 文本)")
105
114
  .action(async function (table) {
106
- await (0, index_1.handleDbSchemaGet)(table, this.optsWithGlobals());
115
+ await (0, index_1.handleDbSchemaGet)(table, { ...this.optsWithGlobals(), appId: (0, shared_1.resolveAppId)({}) });
107
116
  })
108
117
  .addHelpText("after", `
109
118
  Examples:
@@ -144,7 +153,7 @@ Examples:
144
153
  .option("--table <name>", "目标表名;未指定时按文件名(不含扩展名)推断(如 users.csv → users)")
145
154
  .option("--format <fmt>", "文件格式 csv / json;未指定时按文件扩展名推断")
146
155
  .action(async function (file) {
147
- await (0, index_1.handleDbDataImport)(file, this.optsWithGlobals());
156
+ await (0, index_1.handleDbDataImport)(file, { ...this.optsWithGlobals(), appId: (0, shared_1.resolveAppId)({}) });
148
157
  })
149
158
  .addHelpText("after", `
150
159
  Notes:
@@ -181,7 +190,7 @@ Examples:
181
190
  .option("--limit <n>", "最多导出行数(不超过 5000)")
182
191
  .option("--force", "输出文件已存在时覆盖(默认报错)")
183
192
  .action(async function (table) {
184
- await (0, index_1.handleDbDataExport)(table, this.optsWithGlobals());
193
+ await (0, index_1.handleDbDataExport)(table, { ...this.optsWithGlobals(), appId: (0, shared_1.resolveAppId)({}) });
185
194
  })
186
195
  .addHelpText("after", `
187
196
  Notes:
@@ -205,5 +214,568 @@ Examples:
205
214
  $ miaoda db data export users
206
215
  Error: Output file 'users.csv' already exists
207
216
  hint: Use -f to specify a different path, or --force to overwrite.
217
+ `);
218
+ // ── changelog ──
219
+ dbCmd
220
+ .command("changelog")
221
+ .summary("查看 DDL 变更历史")
222
+ .description("查看 DDL 变更记录(建表、改表、删表等)。默认按时间倒序显示,可按表名或时间范围过滤。")
223
+ .usage("[flags]")
224
+ .option("--table <name>", "按表名过滤")
225
+ .option("--since <time>", "起始时间;格式见命令底部 Time formats 段")
226
+ .option("--until <time>", "截止时间,格式同 --since")
227
+ .option("--limit <n>", "返回条数上限(默认 20)", parsePositiveInt, 20)
228
+ .option("--cursor <token>", "从上一页返回的游标位置继续获取")
229
+ .option("--all", "获取全部结果,自动翻页")
230
+ .action(async function () {
231
+ await (0, index_1.handleDbChangelog)(this.optsWithGlobals());
232
+ })
233
+ .addHelpText("after", `
234
+ Notes:
235
+ - DDL 变更由系统自动记录,不可关闭,无需单独开启。
236
+ - 默认输出展示摘要(summary),完整 SQL 原文(statement)通过 --json 获取。
237
+
238
+ Examples:
239
+ # 列出近期变更(statement 截断展示,完整 SQL 用 --json 获取)
240
+ $ miaoda db changelog
241
+ change_id changed_at operator target_table change_type summary
242
+ 1862462453263587 2026-04-16 13:24:59 zqy users ALTER_AUDIT_RETENTION 修改记录日志周期
243
+ 1861814222210116 2026-04-08 20:12:37 zqy users CREATE_TABLE 创建并修改数据表
244
+
245
+ # 按条件过滤
246
+ $ miaoda db changelog --table users --since 2026-04-01
247
+ $ miaoda db changelog --limit 5
248
+
249
+ # --json 输出包含完整 statement
250
+ $ miaoda db changelog --limit 1 --json
251
+ {
252
+ "data": [{
253
+ "change_id": "1861814222210116",
254
+ "changed_at": "2026-04-08T20:12:37Z",
255
+ "operator": "zqy",
256
+ "target_table": "users",
257
+ "change_type": "CREATE_TABLE",
258
+ "summary": "创建并修改数据表",
259
+ "statement": "CREATE TABLE ... ;"
260
+ }],
261
+ "next_cursor": null,
262
+ "has_more": false
263
+ }
264
+
265
+ Time formats (--since / --until):
266
+ relative 30s / 5m / 2h / 3d / 1w (从现在往前推)
267
+ date 2026-04-15 (本地时区 00:00:00)
268
+ local datetime 2026-04-15T10:00:00 (本地时区,T 分隔)
269
+ ISO 8601 with TZ 2026-04-15T10:00:00Z (UTC)
270
+ 2026-04-15T10:00:00+08:00 (显式偏移)
271
+ `);
272
+ // ── audit ──
273
+ const auditCmd = dbCmd
274
+ .command("audit")
275
+ .summary("表级数据审计开关与查询")
276
+ .description("配置表的数据审计 (enable/disable/retention),查询审计状态与日志。")
277
+ .usage("<command> [flags]");
278
+ auditCmd.action(() => {
279
+ auditCmd.outputHelp();
280
+ });
281
+ auditCmd
282
+ .command("status")
283
+ .summary("查看审计开关状态")
284
+ .description("查看表的审计开关状态。不指定表则显示全部。")
285
+ .usage("[table] [flags]")
286
+ .argument("[table]", "表名;省略时返所有已配置审计的表")
287
+ .action(async function (table) {
288
+ await (0, index_1.handleDbAuditStatus)(table, this.optsWithGlobals());
289
+ })
290
+ .addHelpText("after", `
291
+ Examples:
292
+ # 全部表
293
+ $ miaoda db audit status
294
+ table enabled enabled_at retention
295
+ users yes 2026-04-01 08:00:00 30d
296
+ orders no — —
297
+ products yes 2026-03-15 10:30:00 forever
298
+
299
+ # 单表
300
+ $ miaoda db audit status users
301
+ Table: users
302
+ Enabled: yes
303
+ Enabled at: 2026-04-01 08:00:00
304
+ Retention: 30d
305
+
306
+ # --json(全部表)
307
+ $ miaoda db audit status --json
308
+ {
309
+ "data": [
310
+ {"table": "users", "enabled": true, "enabled_at": "2026-04-01T08:00:00Z", "retention": "30d"},
311
+ {"table": "orders", "enabled": false, "enabled_at": null, "retention": null},
312
+ {"table": "products", "enabled": true, "enabled_at": "2026-03-15T10:30:00Z", "retention": "forever"}
313
+ ]
314
+ }
315
+
316
+ # --json(单表)
317
+ $ miaoda db audit status users --json
318
+ {
319
+ "data": {
320
+ "table": "users",
321
+ "enabled": true,
322
+ "enabled_at": "2026-04-01T08:00:00Z",
323
+ "retention": "30d"
324
+ }
325
+ }
326
+ `);
327
+ auditCmd
328
+ .command("enable")
329
+ .summary("启用表审计")
330
+ .description("开启指定表的审计,记录每次 INSERT / UPDATE / DELETE 操作。")
331
+ .usage("<table> [flags]")
332
+ .argument("<table>", "目标表名")
333
+ .option("--retention <period>", "审计日志保留时长,可选 7d / 30d / 180d / 360d / forever", "7d")
334
+ .action(async function (table) {
335
+ await (0, index_1.handleDbAuditEnable)(table, this.optsWithGlobals());
336
+ })
337
+ .addHelpText("after", `
338
+ Notes:
339
+ - 开启审计会占用额外存储空间,计入应用的存储配额。
340
+ - 用 \`miaoda db quota\` 查看用量;用 \`miaoda db audit disable\` 或调整 --retention 控制开销。
341
+
342
+ Examples:
343
+ # 默认保留 7 天
344
+ $ miaoda db audit enable orders
345
+ ✓ Audit enabled for table 'orders' (retention: 7d)
346
+
347
+ # 指定保留时长 / 永久保留
348
+ $ miaoda db audit enable orders --retention 180d
349
+ $ miaoda db audit enable orders --retention forever
350
+
351
+ # non-TTY
352
+ $ miaoda db audit enable orders | cat
353
+ OK Audit enabled for table 'orders' (retention: 7d)
354
+
355
+ # --json
356
+ $ miaoda db audit enable orders --retention 180d --json
357
+ {
358
+ "data": {
359
+ "table": "orders",
360
+ "enabled": true,
361
+ "retention": "180d"
362
+ }
363
+ }
364
+
365
+ # 报错:已经开启
366
+ $ miaoda db audit enable orders
367
+ Error: Audit is already enabled for table 'orders'
368
+ hint: Use \`miaoda db audit status orders\` to check current retention,
369
+ or run this command with a different --retention to update.
370
+
371
+ # 报错:retention 值非法
372
+ $ miaoda db audit enable orders --retention 60d
373
+ Error: Invalid retention '60d'
374
+ hint: Allowed values: 7d, 30d, 180d, 360d, forever.
375
+ `);
376
+ auditCmd
377
+ .command("disable")
378
+ .summary("关闭表审计")
379
+ .description("关闭指定表的审计。")
380
+ .usage("<table> [flags]")
381
+ .argument("<table>", "目标表名")
382
+ .action(async function (table) {
383
+ await (0, index_1.handleDbAuditDisable)(table, this.optsWithGlobals());
384
+ })
385
+ .addHelpText("after", `
386
+ Examples:
387
+ $ miaoda db audit disable orders
388
+ ✓ Audit disabled for table 'orders'
389
+
390
+ # non-TTY
391
+ $ miaoda db audit disable orders | cat
392
+ OK Audit disabled for table 'orders'
393
+
394
+ # --json
395
+ $ miaoda db audit disable orders --json
396
+ {
397
+ "data": {
398
+ "table": "orders",
399
+ "enabled": false
400
+ }
401
+ }
402
+
403
+ # 报错:未开启
404
+ $ miaoda db audit disable orders
405
+ Error: Audit is not enabled for table 'orders'
406
+ hint: Use \`miaoda db audit status\` to see which tables have audit enabled.
407
+ `);
408
+ auditCmd
409
+ .command("list")
410
+ .summary("查询审计日志")
411
+ .description("查询一个或多个表的审计日志记录。多表查询时输出会带 target_table 列标识每条记录所属的表。")
412
+ .usage("<table...> [flags]")
413
+ .argument("<tables...>", "一个或多个表名")
414
+ .option("--since <time>", "起始时间;格式见命令底部 Time formats 段")
415
+ .option("--until <time>", "截止时间,格式同 --since")
416
+ .option("--limit <n>", "返回条数上限(默认 20)", parsePositiveInt, 20)
417
+ .option("--cursor <token>", "从上一页返回的游标位置继续获取")
418
+ .option("--all", "获取全部结果,自动翻页")
419
+ .action(async function (tables) {
420
+ await (0, index_1.handleDbAuditList)(tables, this.optsWithGlobals());
421
+ })
422
+ .addHelpText("after", `
423
+ Notes:
424
+ - 默认输出展示变更摘要(summary),完整变更快照(details 含 before / after)通过 --json 获取。
425
+ - 多表查询时不可用的表(未开启审计或不存在)会被跳过而非整体失败,全部不可用才返回错误。
426
+ - 退出码:只要有任何一张表有结果即为 0(跳过不算失败);全部不可用才为 1。
427
+
428
+ Examples:
429
+ # 单表
430
+ $ miaoda db audit list users --limit 5
431
+
432
+ # 多表(输出带 target_table 列区分来源)
433
+ $ miaoda db audit list users orders --limit 5
434
+
435
+ # 按时间范围
436
+ $ miaoda db audit list users --since 2026-04-14 --limit 10
437
+
438
+ # --json(含完整 details / before / after)
439
+ $ miaoda db audit list users --limit 1 --json
440
+ {
441
+ "data": [{
442
+ "event_id": "01525416B44F87001509F20CD000000002",
443
+ "event_time": "2026-04-15T10:30:00Z",
444
+ "target_table": "users",
445
+ "type": "UPDATE",
446
+ "operator": "alice",
447
+ "details": {
448
+ "before": {"id": "ef4db805-...", "age": 28},
449
+ "after": {"id": "ef4db805-...", "age": 29}
450
+ }
451
+ }],
452
+ "next_cursor": null,
453
+ "has_more": false
454
+ }
455
+
456
+ # 报错:单表未开启
457
+ $ miaoda db audit list orders
458
+ Error: Audit is not enabled for table 'orders'
459
+ hint: Run \`miaoda db audit enable orders\` to start recording changes.
460
+
461
+ # 多表全部不可用才整体报错
462
+ $ miaoda db audit list orders invoices
463
+ Error: No audit data available for any of the specified tables
464
+ hint: Check audit status with \`miaoda db audit status\`.
465
+
466
+ Time formats (--since / --until):
467
+ relative 30s / 5m / 2h / 3d / 1w (从现在往前推)
468
+ date 2026-04-15 (本地时区 00:00:00)
469
+ local datetime 2026-04-15T10:00:00 (本地时区,T 分隔)
470
+ ISO 8601 with TZ 2026-04-15T10:00:00Z (UTC)
471
+ 2026-04-15T10:00:00+08:00 (显式偏移)
472
+ `);
473
+ // ── migration ──
474
+ const migrationCmd = dbCmd
475
+ .command("migration")
476
+ .summary("多环境管理(dev / online,仅专家模式应用)")
477
+ .description("init 拆分双环境,diff 预览待发布变更,apply 把 dev 同步到 online。")
478
+ .usage("<command> [flags]");
479
+ migrationCmd.action(() => {
480
+ migrationCmd.outputHelp();
481
+ });
482
+ migrationCmd
483
+ .command("init")
484
+ .summary("初始化多环境模式(dev / online)")
485
+ .description("初始化多环境模式(dev / online)。初始化后,dev 环境的数据库结构变更需要经 `migration apply` " +
486
+ "应用到 online;online 将无法直接更改数据库结构(仍可进行数据 DML 操作)。")
487
+ .usage("[flags]")
488
+ .option("--sync-data", "启用时将现有数据同步一份到 dev 环境")
489
+ .option("-y, --yes", "跳过确认提示直接执行")
490
+ .action(async function () {
491
+ await (0, index_1.handleDbMigrationInit)(this.optsWithGlobals());
492
+ })
493
+ .addHelpText("after", `
494
+ Notes:
495
+ - 多环境模式一旦启用无法恢复为单库模式,请确认后再启用。
496
+
497
+ Examples:
498
+ # TTY 下会要求确认
499
+ $ miaoda db migration init
500
+ ? This action is irreversible. Initialize multi-env (dev / online)? (y/N) y
501
+ ✓ Multi-env initialized (dev / online)
502
+
503
+ # --yes 跳过确认(Agent / CI 场景)
504
+ $ miaoda db migration init --sync-data --yes
505
+ ✓ Multi-env initialized, data synced to dev
506
+
507
+ # non-TTY
508
+ $ miaoda db migration init --yes | cat
509
+ OK Multi-env initialized (dev / online)
510
+
511
+ # --json
512
+ $ miaoda db migration init --yes --json
513
+ {
514
+ "data": {
515
+ "status": "initialized",
516
+ "environments": ["dev", "online"],
517
+ "data_synced": false
518
+ }
519
+ }
520
+
521
+ # 报错:已经初始化过
522
+ $ miaoda db migration init
523
+ Error: Multi-env is already initialized
524
+ hint: Run \`miaoda db migration diff\` to view pending changes.
525
+ `);
526
+ migrationCmd
527
+ .command("diff")
528
+ .summary("预览 dev → online 待发布变更")
529
+ .description("预览 dev → online 的待发布变更。只读预览,不会实际发布。")
530
+ .usage("[flags]")
531
+ .action(async function () {
532
+ await (0, index_1.handleDbMigrationDiff)(this.optsWithGlobals());
533
+ })
534
+ .addHelpText("after", `
535
+ Examples:
536
+ $ miaoda db migration diff
537
+ dev → online (2 changes):
538
+
539
+ ALTER TABLE users ADD COLUMN avatar_url text;
540
+ CREATE INDEX idx_users_avatar ON users(avatar_url);
541
+
542
+ # --json
543
+ $ miaoda db migration diff --json
544
+ {
545
+ "data": {
546
+ "from": "dev",
547
+ "to": "online",
548
+ "changes": [
549
+ {"type": "ALTER_TABLE", "table": "users", "statement": "ALTER TABLE users ADD COLUMN avatar_url text;"},
550
+ {"type": "CREATE_INDEX", "table": "users", "statement": "CREATE INDEX idx_users_avatar ON users(avatar_url);"}
551
+ ]
552
+ }
553
+ }
554
+
555
+ # 报错:无待发布变更
556
+ $ miaoda db migration diff
557
+ Error: No pending changes between dev and online
558
+ hint: Make schema changes in dev first (e.g. \`miaoda db sql "ALTER TABLE ..." --env dev\`).
559
+
560
+ # 报错:多环境未初始化
561
+ $ miaoda db migration diff
562
+ Error: Multi-env is not initialized
563
+ hint: Run \`miaoda db migration init\` to set up multi-env first.
564
+ `);
565
+ migrationCmd
566
+ .command("apply")
567
+ .summary("将 dev 的变更发布到 online(单事务原子)")
568
+ .description("将 dev 的变更发布到 online。")
569
+ .usage("[flags]")
570
+ .option("-y, --yes", "跳过确认提示直接执行")
571
+ .action(async function () {
572
+ await (0, index_1.handleDbMigrationApply)(this.optsWithGlobals());
573
+ })
574
+ .addHelpText("after", `
575
+ Notes:
576
+ - 该操作会直接改动线上数据库,建议先用 \`migration diff\` 确认变更。
577
+ - 变更以单事务执行:多条 DDL 任何一条失败整批回滚,online 保持原状态。
578
+ - 退出码:0 表示全部成功;1 表示已回滚。
579
+
580
+ Examples:
581
+ # TTY 下会要求确认
582
+ $ miaoda db migration apply
583
+ ? Apply 2 changes to online? (y/N) y
584
+ ✓ Applied dev → online (2 changes)
585
+
586
+ # --yes 跳过确认
587
+ $ miaoda db migration apply --yes
588
+ ✓ Applied dev → online (2 changes)
589
+
590
+ # non-TTY
591
+ $ miaoda db migration apply --yes | cat
592
+ OK Applied dev -> online (2 changes)
593
+
594
+ # --json
595
+ $ miaoda db migration apply --yes --json
596
+ {
597
+ "data": {
598
+ "status": "applied",
599
+ "from": "dev",
600
+ "to": "online",
601
+ "changes_applied": 2
602
+ }
603
+ }
604
+
605
+ # 报错:无待发布变更
606
+ $ miaoda db migration apply
607
+ Error: No pending changes to apply
608
+ hint: Make schema changes in dev first, then run \`miaoda db migration diff\` to preview.
609
+
610
+ # 报错:多环境未启用
611
+ $ miaoda db migration apply
612
+ Error: Multi-env is not initialized
613
+ hint: Run \`miaoda db migration init\` to set up multi-env first.
614
+ `);
615
+ // ── recovery(PITR)──
616
+ const recoveryCmd = dbCmd
617
+ .command("recovery")
618
+ .summary("基于时间点恢复(PITR)")
619
+ .description("把数据库整体恢复到指定时间点,diff 预览影响范围,apply 不可逆覆盖。")
620
+ .usage("<command> [flags]");
621
+ recoveryCmd.action(() => {
622
+ recoveryCmd.outputHelp();
623
+ });
624
+ recoveryCmd
625
+ .command("diff")
626
+ .summary("预览恢复到指定时间点的影响范围")
627
+ .description("预览恢复到指定时间点的影响范围(受影响的表、数据变更量、预计耗时)。只读预览,不改动数据。")
628
+ .usage("<timestamp> [flags]")
629
+ .argument("<timestamp>", "目标时间;格式见命令底部 Time formats 段")
630
+ .action(async function (target) {
631
+ await (0, index_1.handleDbRecoveryDiff)(target, this.optsWithGlobals());
632
+ })
633
+ .addHelpText("after", `
634
+ Notes:
635
+ - 超出可恢复窗口的时间点会报错,错误信息中会标注当前可恢复窗口。
636
+
637
+ Examples:
638
+ # TTY pretty
639
+ $ miaoda db recovery diff 2026-04-15T10:00:00Z
640
+ Recovery preview (→ 2026-04-15T10:00:00Z):
641
+
642
+ tables affected: 2
643
+ users: +3 rows, -1 row, ~5 rows modified
644
+ orders: table will be restored (was dropped at 10:25:00)
645
+
646
+ estimated time: ~30s
647
+
648
+ # non-TTY(管道,TAB 分列)
649
+ $ miaoda db recovery diff 2026-04-15T10:00:00Z | cat
650
+ Recovery preview (-> 2026-04-15T10:00:00Z):
651
+ tables_affected 2
652
+ users +3 rows, -1 row, ~5 rows modified
653
+ orders table will be restored (was dropped at 10:25:00)
654
+ estimated_time 30
655
+
656
+ # --json
657
+ $ miaoda db recovery diff 2026-04-15T10:00:00Z --json
658
+ {
659
+ "data": {
660
+ "target": "2026-04-15T10:00:00Z",
661
+ "tables_affected": 2,
662
+ "changes": [
663
+ {"table": "users", "inserted": 3, "deleted": 1, "modified": 5},
664
+ {"table": "orders", "action": "restore_table", "dropped_at": "2026-04-15T10:25:00Z"}
665
+ ],
666
+ "estimated_seconds": 30
667
+ }
668
+ }
669
+
670
+ # 无变更(目标时间点与当前状态一致)
671
+ $ miaoda db recovery diff 2026-04-15T14:00:00Z
672
+ Recovery preview (→ 2026-04-15T14:00:00Z):
673
+
674
+ No changes — database is already at this state.
675
+
676
+ # 超出可恢复窗口
677
+ $ miaoda db recovery diff 2026-04-05T10:00:00Z
678
+ Error: Timestamp is outside the recoverable window
679
+ hint: Current recoverable window: 2026-04-09T12:00:00Z ~ 2026-04-16T12:00:00Z
680
+ (limited by last migration apply at 2026-04-09T12:00:00Z)
681
+
682
+ # 时间格式错误
683
+ $ miaoda db recovery diff 2026/04/15
684
+ Error: Invalid timestamp '2026/04/15'
685
+ hint: Accepted formats: relative (30s / 5m / 2h / 3d / 1w); date YYYY-MM-DD (local 00:00:00); ...
686
+
687
+ Time formats (<timestamp>):
688
+ relative 30s / 5m / 2h / 3d / 1w (从现在往前推)
689
+ date 2026-04-15 (本地时区 00:00:00)
690
+ local datetime 2026-04-15T10:00:00 (本地时区,T 分隔)
691
+ ISO 8601 with TZ 2026-04-15T10:00:00Z (UTC)
692
+ 2026-04-15T10:00:00+08:00 (显式偏移)
693
+ `);
694
+ recoveryCmd
695
+ .command("apply")
696
+ .summary("将数据库恢复到指定时间点的状态")
697
+ .description("将数据库恢复到指定时间点的状态。")
698
+ .usage("<timestamp> [flags]")
699
+ .argument("<timestamp>", "目标时间;格式见命令底部 Time formats 段")
700
+ .option("-y, --yes", "跳过确认提示直接执行")
701
+ .action(async function (target) {
702
+ await (0, index_1.handleDbRecoveryApply)(target, this.optsWithGlobals());
703
+ })
704
+ .addHelpText("after", `
705
+ Notes:
706
+ - 该操作会覆盖当前数据且不可撤销,建议先用 \`recovery diff\` 预览影响。
707
+ - 恢复过程是原子的:中途失败时数据库回到操作前的状态,不会出现部分恢复。
708
+ - 退出码:0 表示完成;1 表示已回滚。
709
+
710
+ Examples:
711
+ # TTY 下会要求确认
712
+ $ miaoda db recovery apply 2026-04-15T10:00:00Z
713
+ ? Restore database to 2026-04-15T10:00:00Z? This will overwrite current data. (y/N) y
714
+ ✓ Database restored to 2026-04-15T10:00:00Z (2 tables affected, 30s elapsed)
715
+
716
+ # --yes 跳过确认
717
+ $ miaoda db recovery apply 2026-04-15T10:00:00Z --yes
718
+ ✓ Database restored to 2026-04-15T10:00:00Z (2 tables affected, 30s elapsed)
719
+
720
+ # non-TTY
721
+ $ miaoda db recovery apply 2026-04-15T10:00:00Z --yes | cat
722
+ OK Database restored to 2026-04-15T10:00:00Z (2 tables affected, 30s elapsed)
723
+
724
+ # --json
725
+ $ miaoda db recovery apply 2026-04-15T10:00:00Z --yes --json
726
+ {
727
+ "data": {
728
+ "status": "restored",
729
+ "target": "2026-04-15T10:00:00Z",
730
+ "tables_affected": 2,
731
+ "elapsed_seconds": 30
732
+ }
733
+ }
734
+
735
+ # 超出可恢复窗口
736
+ $ miaoda db recovery apply 2026-04-05T10:00:00Z
737
+ Error: Timestamp is outside the recoverable window
738
+ hint: Current recoverable window: 2026-04-09T12:00:00Z ~ 2026-04-16T12:00:00Z
739
+ Run \`miaoda db recovery diff <ts>\` within this window to preview impact.
740
+
741
+ # 并发冲突(已有恢复任务在执行)
742
+ $ miaoda db recovery apply 2026-04-15T10:00:00Z
743
+ Error: Another recovery is already in progress
744
+ hint: Wait for the current recovery to finish. Started at 2026-04-16T12:30:00Z.
745
+
746
+ Time formats (<timestamp>):
747
+ relative 30s / 5m / 2h / 3d / 1w (从现在往前推)
748
+ date 2026-04-15 (本地时区 00:00:00)
749
+ local datetime 2026-04-15T10:00:00 (本地时区,T 分隔)
750
+ ISO 8601 with TZ 2026-04-15T10:00:00Z (UTC)
751
+ 2026-04-15T10:00:00+08:00 (显式偏移)
752
+ `);
753
+ // ── quota ──
754
+ dbCmd
755
+ .command("quota")
756
+ .summary("查看数据库的存储用量与限额")
757
+ .description("查看数据库的存储用量、配额和表数量。")
758
+ .usage("[flags]")
759
+ .action(async function () {
760
+ await (0, index_1.handleDbQuota)(this.optsWithGlobals());
761
+ })
762
+ .addHelpText("after", `
763
+ Examples:
764
+ $ miaoda db quota
765
+ Storage: 14.9 MB / 1 GB (1.5%)
766
+ Tables: 3
767
+ Views: 10
768
+
769
+ # --json
770
+ $ miaoda db quota --json
771
+ {
772
+ "data": {
773
+ "storage_used_bytes": 15623782,
774
+ "storage_quota_bytes": 1073741824,
775
+ "usage_percent": 1.5,
776
+ "tables": 3,
777
+ "views": 10
778
+ }
779
+ }
208
780
  `);
209
781
  }