@lark-apaas/miaoda-cli 0.1.2-alpha.4d0ff57 → 0.1.2-alpha.55feccd
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/dist/api/db/api.js +73 -4
- package/dist/api/db/client.js +1 -1
- package/dist/api/db/index.js +3 -1
- package/dist/cli/commands/db/index.js +1 -1
- package/dist/cli/handlers/db/audit.js +145 -110
- package/dist/cli/handlers/db/changelog.js +16 -3
- package/dist/cli/handlers/db/migration.js +50 -32
- package/dist/cli/handlers/db/quota.js +18 -10
- package/dist/cli/handlers/db/recovery.js +76 -29
- package/dist/cli/handlers/file/quota.js +16 -8
- package/dist/utils/output.js +22 -0
- package/package.json +1 -1
package/dist/api/db/api.js
CHANGED
|
@@ -5,7 +5,9 @@ exports.getSchema = getSchema;
|
|
|
5
5
|
exports.importData = importData;
|
|
6
6
|
exports.exportData = exportData;
|
|
7
7
|
exports.listDDLChangelog = listDDLChangelog;
|
|
8
|
+
exports.getAuditStatus = getAuditStatus;
|
|
8
9
|
exports.setAuditConfig = setAuditConfig;
|
|
10
|
+
exports.listAuditLog = listAuditLog;
|
|
9
11
|
exports.migrationInit = migrationInit;
|
|
10
12
|
exports.migrate = migrate;
|
|
11
13
|
exports.recover = recover;
|
|
@@ -332,13 +334,35 @@ async function listDDLChangelog(opts) {
|
|
|
332
334
|
hasMore: Boolean(data.hasMore),
|
|
333
335
|
};
|
|
334
336
|
}
|
|
335
|
-
// ── db audit → InnerAdminSetAuditConfig ──
|
|
337
|
+
// ── db audit → InnerAdminGetAuditStatus / InnerAdminSetAuditConfig ──
|
|
338
|
+
/**
|
|
339
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/audit/status?table=&dbBranch=
|
|
340
|
+
* 查表审计开关状态。table 非空 → 单表过滤;空 → 返当前 workspace 全部已配置表。
|
|
341
|
+
*/
|
|
342
|
+
async function getAuditStatus(opts) {
|
|
343
|
+
const client = (0, http_1.getHttpClient)();
|
|
344
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/audit/status", {
|
|
345
|
+
table: opts.table,
|
|
346
|
+
dbBranch: opts.dbBranch,
|
|
347
|
+
});
|
|
348
|
+
const start = Date.now();
|
|
349
|
+
let response;
|
|
350
|
+
try {
|
|
351
|
+
response = await client.get(url);
|
|
352
|
+
(0, client_1.traceHttp)("GET", url, start, response);
|
|
353
|
+
}
|
|
354
|
+
catch (err) {
|
|
355
|
+
(0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
356
|
+
await mapDbHttpError(err, url, "Failed to get audit status");
|
|
357
|
+
throw err; // 不可达
|
|
358
|
+
}
|
|
359
|
+
const respBody = (await response.json());
|
|
360
|
+
const data = (0, client_1.extractData)(respBody);
|
|
361
|
+
return data.items ?? [];
|
|
362
|
+
}
|
|
336
363
|
/**
|
|
337
364
|
* 后端:POST /v1/dataloom/app/{appId}/db/audit/config
|
|
338
365
|
* 写:enabled=true 开启 / false 关闭。retention 仅 enabled=true 生效。
|
|
339
|
-
*
|
|
340
|
-
* 读路径(status / log)不在此模块——CLI handler 走 execSql 直接 SELECT
|
|
341
|
-
* `_dl_audit_config` / `_dl_audit_log` 元数据表。
|
|
342
366
|
*/
|
|
343
367
|
async function setAuditConfig(opts) {
|
|
344
368
|
const client = (0, http_1.getHttpClient)();
|
|
@@ -369,6 +393,51 @@ async function setAuditConfig(opts) {
|
|
|
369
393
|
}
|
|
370
394
|
return data.status;
|
|
371
395
|
}
|
|
396
|
+
// ── db audit log → InnerAdminListAuditLog ──
|
|
397
|
+
/**
|
|
398
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/audit/log?tables=&since=&until=&limit=&cursor=&dbBranch=
|
|
399
|
+
*
|
|
400
|
+
* 走 admin-inner 接口而不是 InnerAdminExecuteSQL 直接 SELECT pg_audit:
|
|
401
|
+
* - operator 在 details JSONB 内是 user_id,服务端解析成 username
|
|
402
|
+
* - summary 后端按 type + before/after diff 合成(pg_audit 表无此列)
|
|
403
|
+
* - before/after JSONB 后端 JSON.stringify 后透传字符串,CLI 按需 parse
|
|
404
|
+
*
|
|
405
|
+
* 多表用逗号拼接走 query;后端按 target_table IN (...) 一次查。skipped 字段返
|
|
406
|
+
* 多表中无记录的表名,便于 CLI 展示 hint。
|
|
407
|
+
*/
|
|
408
|
+
async function listAuditLog(opts) {
|
|
409
|
+
if (opts.tables.length === 0) {
|
|
410
|
+
throw new error_1.AppError("ARGS_INVALID", "at least one table is required");
|
|
411
|
+
}
|
|
412
|
+
const client = (0, http_1.getHttpClient)();
|
|
413
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/audit/log", {
|
|
414
|
+
tables: opts.tables.join(","),
|
|
415
|
+
since: opts.since,
|
|
416
|
+
until: opts.until,
|
|
417
|
+
limit: opts.limit !== undefined ? String(opts.limit) : undefined,
|
|
418
|
+
cursor: opts.cursor,
|
|
419
|
+
dbBranch: opts.dbBranch,
|
|
420
|
+
});
|
|
421
|
+
const start = Date.now();
|
|
422
|
+
let response;
|
|
423
|
+
try {
|
|
424
|
+
response = await client.get(url);
|
|
425
|
+
(0, client_1.traceHttp)("GET", url, start, response);
|
|
426
|
+
}
|
|
427
|
+
catch (err) {
|
|
428
|
+
(0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
429
|
+
await mapDbHttpError(err, url, "Failed to list audit log");
|
|
430
|
+
throw err; // 不可达
|
|
431
|
+
}
|
|
432
|
+
const body = (await response.json());
|
|
433
|
+
const data = (0, client_1.extractData)(body);
|
|
434
|
+
return {
|
|
435
|
+
items: data.items ?? [],
|
|
436
|
+
nextCursor: data.nextCursor && data.nextCursor !== "" ? data.nextCursor : null,
|
|
437
|
+
hasMore: Boolean(data.hasMore),
|
|
438
|
+
skipped: data.skipped ?? [],
|
|
439
|
+
};
|
|
440
|
+
}
|
|
372
441
|
// ── db migration → InnerAdminMigrationInit / InnerAdminMigrate ──
|
|
373
442
|
/**
|
|
374
443
|
* 后端:POST /v1/dataloom/app/{appId}/db/enableMultiEnv
|
package/dist/api/db/client.js
CHANGED
|
@@ -140,7 +140,7 @@ const BIZ_ERR_MAP = new Map(Object.entries({
|
|
|
140
140
|
},
|
|
141
141
|
k_dl_1310003: {
|
|
142
142
|
code: "INVALID_RETENTION",
|
|
143
|
-
hint: "Allowed values: 7d
|
|
143
|
+
hint: "Allowed values: 7d, 30d, 180d, 360d, forever.",
|
|
144
144
|
},
|
|
145
145
|
// migration
|
|
146
146
|
k_dl_1320001: {
|
package/dist/api/db/index.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.pickTableDetail = exports.flattenSchemaList = exports.parseSqlResult = exports.SQLSTATE_MAP = exports.ensureInnerSuccess = exports.buildInnerUrl = exports.getDbQuota = exports.recover = exports.migrate = exports.migrationInit = exports.setAuditConfig = exports.listDDLChangelog = exports.exportData = exports.importData = exports.getSchema = exports.execSql = void 0;
|
|
3
|
+
exports.pickTableDetail = exports.flattenSchemaList = exports.parseSqlResult = exports.SQLSTATE_MAP = exports.ensureInnerSuccess = exports.buildInnerUrl = exports.getDbQuota = exports.recover = exports.migrate = exports.migrationInit = exports.listAuditLog = exports.setAuditConfig = exports.getAuditStatus = exports.listDDLChangelog = exports.exportData = exports.importData = exports.getSchema = exports.execSql = void 0;
|
|
4
4
|
var api_1 = require("./api");
|
|
5
5
|
Object.defineProperty(exports, "execSql", { enumerable: true, get: function () { return api_1.execSql; } });
|
|
6
6
|
Object.defineProperty(exports, "getSchema", { enumerable: true, get: function () { return api_1.getSchema; } });
|
|
7
7
|
Object.defineProperty(exports, "importData", { enumerable: true, get: function () { return api_1.importData; } });
|
|
8
8
|
Object.defineProperty(exports, "exportData", { enumerable: true, get: function () { return api_1.exportData; } });
|
|
9
9
|
Object.defineProperty(exports, "listDDLChangelog", { enumerable: true, get: function () { return api_1.listDDLChangelog; } });
|
|
10
|
+
Object.defineProperty(exports, "getAuditStatus", { enumerable: true, get: function () { return api_1.getAuditStatus; } });
|
|
10
11
|
Object.defineProperty(exports, "setAuditConfig", { enumerable: true, get: function () { return api_1.setAuditConfig; } });
|
|
12
|
+
Object.defineProperty(exports, "listAuditLog", { enumerable: true, get: function () { return api_1.listAuditLog; } });
|
|
11
13
|
Object.defineProperty(exports, "migrationInit", { enumerable: true, get: function () { return api_1.migrationInit; } });
|
|
12
14
|
Object.defineProperty(exports, "migrate", { enumerable: true, get: function () { return api_1.migrate; } });
|
|
13
15
|
Object.defineProperty(exports, "recover", { enumerable: true, get: function () { return api_1.recover; } });
|
|
@@ -251,7 +251,7 @@ Examples:
|
|
|
251
251
|
.summary("启用表审计")
|
|
252
252
|
.usage("<table> [flags]")
|
|
253
253
|
.argument("<table>", "目标表名")
|
|
254
|
-
.option("--retention <ttl>", "保留时长 7d / 30d / 180d / 360d", "7d")
|
|
254
|
+
.option("--retention <ttl>", "保留时长 7d / 30d / 180d / 360d / forever", "7d")
|
|
255
255
|
.action(async function (table) {
|
|
256
256
|
await (0, index_1.handleDbAuditEnable)(table, this.optsWithGlobals());
|
|
257
257
|
});
|
|
@@ -40,98 +40,113 @@ exports.handleDbAuditList = handleDbAuditList;
|
|
|
40
40
|
const api = __importStar(require("../../../api/index"));
|
|
41
41
|
const index_1 = require("../../../api/file/index");
|
|
42
42
|
const shared_1 = require("../../../cli/commands/shared");
|
|
43
|
+
const colors_1 = require("../../../utils/colors");
|
|
43
44
|
const error_1 = require("../../../utils/error");
|
|
44
45
|
const output_1 = require("../../../utils/output");
|
|
45
46
|
const render_1 = require("../../../utils/render");
|
|
46
|
-
const
|
|
47
|
-
// "forever" 不在后端支持范围(pg_audit ValidDay 只接 int64 天数),CLI 提前拦截
|
|
48
|
-
const VALID_RETENTION = new Set(["7d", "30d", "180d", "360d"]);
|
|
47
|
+
const VALID_RETENTION = new Set(["7d", "30d", "180d", "360d", "forever"]);
|
|
49
48
|
async function handleDbAuditStatus(table, opts) {
|
|
50
49
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const results = await api.db.execSql({ appId, sql, dbBranch: opts.env });
|
|
55
|
-
const rows = collectSelectRows(results);
|
|
56
|
-
// 单表查询:表未配置时给个 enabled=false 占位项,让 CLI 输出语义统一
|
|
50
|
+
const items = await api.db.getAuditStatus({ appId, table, dbBranch: opts.env });
|
|
51
|
+
const rows = items.map(toAuditStatus);
|
|
52
|
+
// 单表查询但后端没记录 → 占位 enabled=false
|
|
57
53
|
if (table !== undefined && table !== "" && rows.length === 0) {
|
|
58
|
-
rows.push({ table, enabled: false });
|
|
54
|
+
rows.push({ table, enabled: false, enabled_at: null, retention: null });
|
|
59
55
|
}
|
|
60
|
-
|
|
56
|
+
// PRD JSON:单表返 object,多表返 array
|
|
61
57
|
if ((0, output_1.isJsonMode)()) {
|
|
62
|
-
(
|
|
58
|
+
if (table !== undefined && rows.length === 1) {
|
|
59
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(rows[0]));
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(rows));
|
|
63
|
+
}
|
|
63
64
|
return;
|
|
64
65
|
}
|
|
65
|
-
if (
|
|
66
|
+
if (rows.length === 0) {
|
|
66
67
|
(0, output_1.emit)("No audit configuration found.");
|
|
67
68
|
return;
|
|
68
69
|
}
|
|
69
70
|
const tty = (0, render_1.isStdoutTty)();
|
|
70
|
-
//
|
|
71
|
-
if (table !== undefined &&
|
|
72
|
-
const it =
|
|
71
|
+
// 单表 → key:value 形态
|
|
72
|
+
if (table !== undefined && rows.length === 1) {
|
|
73
|
+
const it = rows[0];
|
|
73
74
|
(0, output_1.emit)((0, render_1.renderKeyValue)([
|
|
74
|
-
["
|
|
75
|
-
["
|
|
76
|
-
["
|
|
77
|
-
["
|
|
75
|
+
["Table", it.table],
|
|
76
|
+
["Enabled", boolToYesNo(it.enabled)],
|
|
77
|
+
["Enabled at", it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) : "—"],
|
|
78
|
+
["Retention", it.retention ?? "—"],
|
|
78
79
|
], tty));
|
|
79
80
|
return;
|
|
80
81
|
}
|
|
82
|
+
// 列表 → table 形态
|
|
81
83
|
const headers = ["table", "enabled", "enabled_at", "retention"];
|
|
82
|
-
const out =
|
|
84
|
+
const out = rows.map((it) => [
|
|
83
85
|
it.table,
|
|
84
|
-
|
|
86
|
+
boolToYesNo(it.enabled),
|
|
85
87
|
it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) : "—",
|
|
86
88
|
it.retention ?? "—",
|
|
87
89
|
]);
|
|
88
90
|
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, out) : (0, render_1.renderTsv)(headers, out));
|
|
89
91
|
}
|
|
90
|
-
function toAuditStatus(
|
|
92
|
+
function toAuditStatus(s) {
|
|
91
93
|
return {
|
|
92
|
-
table:
|
|
93
|
-
enabled:
|
|
94
|
-
enabled_at:
|
|
95
|
-
retention:
|
|
94
|
+
table: s.table,
|
|
95
|
+
enabled: s.enabled,
|
|
96
|
+
enabled_at: s.enabledAt ?? null,
|
|
97
|
+
retention: s.retention ?? null,
|
|
96
98
|
};
|
|
97
99
|
}
|
|
98
|
-
// asString 把 SELECT 结果里的列值(unknown)安全地转成字符串。SQL 返回值通常是
|
|
99
|
-
// string / number / boolean / null,但极端情况下也可能是 JSON 反序列化出的对象。
|
|
100
|
-
// 直接 String(obj) 会得到 "[object Object]",这里改用 typeof 分类避免。
|
|
101
|
-
function asString(v, fallback = "") {
|
|
102
|
-
if (v == null)
|
|
103
|
-
return fallback;
|
|
104
|
-
if (typeof v === "string")
|
|
105
|
-
return v;
|
|
106
|
-
if (typeof v === "number" || typeof v === "boolean" || typeof v === "bigint")
|
|
107
|
-
return String(v);
|
|
108
|
-
return JSON.stringify(v);
|
|
109
|
-
}
|
|
110
100
|
async function handleDbAuditEnable(table, opts) {
|
|
111
101
|
if (!table) {
|
|
112
102
|
throw new error_1.AppError("ARGS_INVALID", "table name is required", {
|
|
113
|
-
next_actions: [
|
|
103
|
+
next_actions: [
|
|
104
|
+
"Usage: miaoda db audit enable <table> [--retention 7d|30d|180d|360d|forever]",
|
|
105
|
+
],
|
|
114
106
|
});
|
|
115
107
|
}
|
|
116
108
|
const retention = opts.retention ?? "7d";
|
|
117
109
|
if (!VALID_RETENTION.has(retention)) {
|
|
118
|
-
throw new error_1.AppError("INVALID_RETENTION", `
|
|
119
|
-
next_actions: ["Allowed values: 7d
|
|
110
|
+
throw new error_1.AppError("INVALID_RETENTION", `Invalid retention '${retention}'`, {
|
|
111
|
+
next_actions: ["Allowed values: 7d, 30d, 180d, 360d, forever."],
|
|
120
112
|
});
|
|
121
113
|
}
|
|
122
114
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
115
|
+
let status;
|
|
116
|
+
try {
|
|
117
|
+
status = await api.db.setAuditConfig({
|
|
118
|
+
appId,
|
|
119
|
+
table,
|
|
120
|
+
enabled: true,
|
|
121
|
+
retention,
|
|
122
|
+
dbBranch: opts.env,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch (err) {
|
|
126
|
+
// PRD: 重复 enable 报错时附带 hint,引导用户去 status 看 retention 或换值更新
|
|
127
|
+
if (err instanceof error_1.AppError && err.code === "DB_API_k_dl_1300030") {
|
|
128
|
+
throw new error_1.AppError(err.code, err.message, {
|
|
129
|
+
next_actions: [
|
|
130
|
+
`Use \`miaoda db audit status ${table}\` to check current retention, or run this command with a different --retention to update.`,
|
|
131
|
+
],
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
136
|
+
// PRD JSON:{"data": {"table": "...", "enabled": true, "retention": "..."}}
|
|
130
137
|
if ((0, output_1.isJsonMode)()) {
|
|
131
|
-
(0, output_1.emitOk)(
|
|
138
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
139
|
+
table: status.table,
|
|
140
|
+
enabled: status.enabled,
|
|
141
|
+
retention: status.retention ?? retention,
|
|
142
|
+
}));
|
|
132
143
|
return;
|
|
133
144
|
}
|
|
134
|
-
(0,
|
|
145
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
146
|
+
const prefix = tty ? "✓" : "OK";
|
|
147
|
+
// c.highlight 内部按 TTY/NO_COLOR/FORCE_COLOR 自动决定是否染色,无需再判 tty
|
|
148
|
+
const tableLabel = colors_1.c.highlight(`'${status.table}'`);
|
|
149
|
+
(0, output_1.emit)(`${prefix} Audit enabled for table ${tableLabel} (retention: ${status.retention ?? retention})`);
|
|
135
150
|
}
|
|
136
151
|
async function handleDbAuditDisable(table, opts) {
|
|
137
152
|
if (!table) {
|
|
@@ -140,17 +155,34 @@ async function handleDbAuditDisable(table, opts) {
|
|
|
140
155
|
});
|
|
141
156
|
}
|
|
142
157
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
158
|
+
let status;
|
|
159
|
+
try {
|
|
160
|
+
status = await api.db.setAuditConfig({
|
|
161
|
+
appId,
|
|
162
|
+
table,
|
|
163
|
+
enabled: false,
|
|
164
|
+
dbBranch: opts.env,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
// PRD: 重复 disable / 未启用就 disable 报错时附带 hint,引导用户去看哪些表已开启
|
|
169
|
+
if (err instanceof error_1.AppError && err.code === "DB_API_k_dl_1300031") {
|
|
170
|
+
throw new error_1.AppError(err.code, err.message, {
|
|
171
|
+
next_actions: ["Use `miaoda db audit status` to see which tables have audit enabled."],
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
throw err;
|
|
175
|
+
}
|
|
149
176
|
if ((0, output_1.isJsonMode)()) {
|
|
150
|
-
(0, output_1.emitOk)(
|
|
177
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
178
|
+
table: status.table,
|
|
179
|
+
enabled: status.enabled,
|
|
180
|
+
}));
|
|
151
181
|
return;
|
|
152
182
|
}
|
|
153
|
-
(0,
|
|
183
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
184
|
+
const prefix = tty ? "✓" : "OK";
|
|
185
|
+
(0, output_1.emit)(`${prefix} Audit disabled for table ${colors_1.c.highlight(`'${status.table}'`)}`);
|
|
154
186
|
}
|
|
155
187
|
async function handleDbAuditList(tables, opts) {
|
|
156
188
|
if (tables.length === 0) {
|
|
@@ -161,36 +193,39 @@ async function handleDbAuditList(tables, opts) {
|
|
|
161
193
|
});
|
|
162
194
|
}
|
|
163
195
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
164
|
-
const limit = opts.limit ??
|
|
165
|
-
//
|
|
166
|
-
|
|
167
|
-
const since = normalizeTime(
|
|
196
|
+
const limit = opts.limit ?? 20;
|
|
197
|
+
// 时间字段归一化为 ISO 8601 UTC(服务端按 event_time 字段比较)。cursor 由
|
|
198
|
+
// 后端管理(本质也是 ISO 时间),CLI 不再混用 cursor/since。
|
|
199
|
+
const since = normalizeTime(opts.since, "--since");
|
|
168
200
|
const until = normalizeTime(opts.until, "--until");
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const visible = hasMore ? allRows.slice(0, limit) : allRows;
|
|
182
|
-
const nextCursor = hasMore && visible.length > 0 ? asString(visible[visible.length - 1].event_time) : null;
|
|
183
|
-
// 多表混合时统计哪些表无记录,PRD 要求末尾汇总
|
|
184
|
-
const seen = new Set();
|
|
185
|
-
for (const r of visible)
|
|
186
|
-
seen.add(asString(r.target_table));
|
|
187
|
-
const skipped = tables.filter((t) => !seen.has(t));
|
|
201
|
+
const result = await api.db.listAuditLog({
|
|
202
|
+
appId,
|
|
203
|
+
tables,
|
|
204
|
+
since,
|
|
205
|
+
until,
|
|
206
|
+
limit,
|
|
207
|
+
cursor: opts.cursor,
|
|
208
|
+
dbBranch: opts.env,
|
|
209
|
+
});
|
|
210
|
+
const visible = result.items;
|
|
211
|
+
const skipped = result.skipped;
|
|
212
|
+
// PRD JSON:data 是数组,元素是事件对象(snake_case),分页信封 next_cursor / has_more
|
|
188
213
|
if ((0, output_1.isJsonMode)()) {
|
|
189
|
-
|
|
214
|
+
const items = visible.map((it) => ({
|
|
215
|
+
event_id: it.eventId,
|
|
216
|
+
event_time: it.eventTime,
|
|
217
|
+
target_table: it.targetTable,
|
|
218
|
+
type: it.type,
|
|
219
|
+
operator: it.operator,
|
|
220
|
+
summary: it.summary,
|
|
221
|
+
// before/after 服务端是 JSON 字符串,反序列化回结构化对象供下游消费
|
|
222
|
+
...(it.before !== undefined ? { before: safeParseJson(it.before) } : {}),
|
|
223
|
+
...(it.after !== undefined ? { after: safeParseJson(it.after) } : {}),
|
|
224
|
+
}));
|
|
225
|
+
(0, output_1.emitPaged)(items, result.nextCursor, result.hasMore);
|
|
190
226
|
if (skipped.length > 0) {
|
|
191
227
|
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(", ")})\n`);
|
|
192
228
|
}
|
|
193
|
-
// 退出码:任一表有数据 → 0;全空 → 1(PRD 约定)
|
|
194
229
|
if (visible.length === 0)
|
|
195
230
|
process.exitCode = 1;
|
|
196
231
|
return;
|
|
@@ -204,47 +239,47 @@ async function handleDbAuditList(tables, opts) {
|
|
|
204
239
|
return;
|
|
205
240
|
}
|
|
206
241
|
const tty = (0, render_1.isStdoutTty)();
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
242
|
+
// PRD:多表渲染时第一列是 target_table,单表时去掉这一列
|
|
243
|
+
const isMultiTable = tables.length > 1;
|
|
244
|
+
const headers = isMultiTable
|
|
245
|
+
? ["target_table", "event_time", "type", "event_id", "operator", "summary"]
|
|
246
|
+
: ["event_time", "type", "event_id", "operator", "summary"];
|
|
247
|
+
const rows = visible.map((it) => {
|
|
248
|
+
const eventTime = (0, render_1.formatTime)(it.eventTime, tty);
|
|
249
|
+
// event_id 完整透传——PRD 截图里的 "..." 只是文档省略写法,不是 CLI 行为
|
|
250
|
+
const cells = [eventTime, it.type, it.eventId, it.operator || "—", it.summary];
|
|
251
|
+
return isMultiTable ? [it.targetTable, ...cells] : cells;
|
|
252
|
+
});
|
|
215
253
|
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, rows) : (0, render_1.renderTsv)(headers, rows));
|
|
216
254
|
if (skipped.length > 0) {
|
|
217
255
|
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(", ")})\n`);
|
|
218
256
|
}
|
|
219
|
-
if (hasMore && nextCursor) {
|
|
220
|
-
process.stderr.write(`(more results; use --cursor '${nextCursor}')\n`);
|
|
257
|
+
if (result.hasMore && result.nextCursor) {
|
|
258
|
+
process.stderr.write(`(more results; use --cursor '${result.nextCursor}')\n`);
|
|
221
259
|
}
|
|
222
260
|
}
|
|
223
261
|
// ── helpers ──
|
|
224
262
|
function normalizeTime(input, flagName) {
|
|
225
263
|
if (input === undefined || input === "")
|
|
226
264
|
return undefined;
|
|
227
|
-
// ISO 8601
|
|
228
|
-
// 严格 ISO 上重做一次会损精度
|
|
265
|
+
// ISO 8601 直接透传,避免 parseTimeFilterMs 在严格 ISO 上重做一次损精度
|
|
229
266
|
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})$/.test(input)) {
|
|
230
267
|
return new Date(input).toISOString();
|
|
231
268
|
}
|
|
232
269
|
const ms = (0, index_1.parseTimeFilterMs)(input, flagName);
|
|
233
270
|
return new Date(ms).toISOString();
|
|
234
271
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
272
|
+
// 服务端 before/after 是 JSON 字符串透传,CLI JSON 输出再反序列化回对象,
|
|
273
|
+
// 给下游 jq 等工具消费。失败时透传原始字符串避免数据丢失。
|
|
274
|
+
function safeParseJson(s) {
|
|
275
|
+
try {
|
|
276
|
+
return JSON.parse(s);
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
return s;
|
|
280
|
+
}
|
|
243
281
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
*/
|
|
248
|
-
function escapeSqlLiteral(s) {
|
|
249
|
-
return s.replace(/'/g, "''");
|
|
282
|
+
// PRD 输出 enabled 列用 yes/no 而非 true/false
|
|
283
|
+
function boolToYesNo(b) {
|
|
284
|
+
return b ? "yes" : "no";
|
|
250
285
|
}
|
|
@@ -39,6 +39,17 @@ const index_1 = require("../../../api/file/index");
|
|
|
39
39
|
const shared_1 = require("../../../cli/commands/shared");
|
|
40
40
|
const output_1 = require("../../../utils/output");
|
|
41
41
|
const render_1 = require("../../../utils/render");
|
|
42
|
+
function toRow(it) {
|
|
43
|
+
return {
|
|
44
|
+
change_id: it.changeId,
|
|
45
|
+
changed_at: it.changedAt,
|
|
46
|
+
operator: it.operator,
|
|
47
|
+
target_table: it.targetTable,
|
|
48
|
+
change_type: it.changeType,
|
|
49
|
+
summary: it.summary,
|
|
50
|
+
statement: it.statement,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
42
53
|
/** 把用户传入的 since/until 归一成 ISO 8601 UTC 字符串;空串返 undefined。 */
|
|
43
54
|
function normalizeTime(input, flagName) {
|
|
44
55
|
if (input === undefined || input === "")
|
|
@@ -74,12 +85,13 @@ async function handleDbChangelog(opts) {
|
|
|
74
85
|
cursor = page.nextCursor;
|
|
75
86
|
}
|
|
76
87
|
if ((0, output_1.isJsonMode)()) {
|
|
88
|
+
const rows = allItems.map(toRow);
|
|
77
89
|
// --all 时已经把所有页合一起返,has_more=false / next_cursor=null
|
|
78
90
|
if (opts.all) {
|
|
79
|
-
(0, output_1.emitPaged)(
|
|
91
|
+
(0, output_1.emitPaged)((0, output_1.snakeCaseKeys)(rows), null, false);
|
|
80
92
|
}
|
|
81
93
|
else {
|
|
82
|
-
(0, output_1.emitPaged)(
|
|
94
|
+
(0, output_1.emitPaged)((0, output_1.snakeCaseKeys)(rows), lastCursor, lastHasMore);
|
|
83
95
|
}
|
|
84
96
|
return;
|
|
85
97
|
}
|
|
@@ -88,7 +100,8 @@ async function handleDbChangelog(opts) {
|
|
|
88
100
|
return;
|
|
89
101
|
}
|
|
90
102
|
const tty = (0, render_1.isStdoutTty)();
|
|
91
|
-
|
|
103
|
+
// PRD: change_id / changed_at / operator / target_table / change_type / summary
|
|
104
|
+
const headers = ["change_id", "changed_at", "operator", "target_table", "change_type", "summary"];
|
|
92
105
|
const rows = allItems.map((it) => [
|
|
93
106
|
it.changeId,
|
|
94
107
|
(0, render_1.formatTime)(it.changedAt, tty),
|
|
@@ -46,7 +46,7 @@ const output_1 = require("../../../utils/output");
|
|
|
46
46
|
const render_1 = require("../../../utils/render");
|
|
47
47
|
async function handleDbMigrationInit(opts) {
|
|
48
48
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
49
|
-
//
|
|
49
|
+
// 不可逆操作,TTY 默认要求 y/N;--yes 跳过
|
|
50
50
|
if (!opts.yes && !(0, output_1.isJsonMode)()) {
|
|
51
51
|
const ok = await confirm(`? Initialize multi-env (single → dev/online), this is IRREVERSIBLE${opts.syncData ? " and will copy existing data to dev" : ""}? (y/N) `);
|
|
52
52
|
if (!ok) {
|
|
@@ -56,48 +56,73 @@ async function handleDbMigrationInit(opts) {
|
|
|
56
56
|
}
|
|
57
57
|
const result = await api.db.migrationInit({ appId, syncData: opts.syncData });
|
|
58
58
|
if ((0, output_1.isJsonMode)()) {
|
|
59
|
-
(0, output_1.emitOk)(result);
|
|
59
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(result));
|
|
60
60
|
return;
|
|
61
61
|
}
|
|
62
62
|
const tty = (0, render_1.isStdoutTty)();
|
|
63
63
|
(0, output_1.emit)((0, render_1.renderKeyValue)([
|
|
64
|
-
["
|
|
65
|
-
["
|
|
66
|
-
["
|
|
64
|
+
["Status", result.status],
|
|
65
|
+
["Environments", result.environments.join(", ")],
|
|
66
|
+
["Data synced", String(result.dataSynced)],
|
|
67
67
|
], tty));
|
|
68
68
|
}
|
|
69
69
|
async function handleDbMigrationDiff(opts) {
|
|
70
70
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
71
71
|
const result = await api.db.migrate({ appId, dryRun: true });
|
|
72
|
-
|
|
72
|
+
renderDiff(result);
|
|
73
73
|
}
|
|
74
74
|
async function handleDbMigrationApply(opts) {
|
|
75
75
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
76
|
-
//
|
|
77
|
-
const preview = await api.db.migrate({ appId, dryRun: true });
|
|
78
|
-
if (preview.changes.length === 0) {
|
|
79
|
-
if ((0, output_1.isJsonMode)()) {
|
|
80
|
-
(0, output_1.emitOk)({ ...preview, status: "no_pending_changes" });
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
(0, output_1.emit)("No pending changes from dev to online.");
|
|
84
|
-
}
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
76
|
+
// TTY 下先 diff 给用户审;--yes 直接打到 online
|
|
87
77
|
if (!opts.yes && !(0, output_1.isJsonMode)()) {
|
|
88
|
-
|
|
89
|
-
|
|
78
|
+
const preview = await api.db.migrate({ appId, dryRun: true });
|
|
79
|
+
if (preview.changes.length === 0) {
|
|
80
|
+
(0, output_1.emit)(`Error: No pending changes between ${preview.from} and ${preview.to}`);
|
|
81
|
+
(0, output_1.emit)(` hint: Make schema changes in dev first.`);
|
|
82
|
+
process.exitCode = 1;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
renderDiff(preview);
|
|
86
|
+
const ok = await confirm(`? Apply ${String(preview.changes.length)} change(s) to ${preview.to}? (y/N) `);
|
|
90
87
|
if (!ok) {
|
|
91
88
|
(0, output_1.emit)("Aborted.");
|
|
92
89
|
return;
|
|
93
90
|
}
|
|
94
91
|
}
|
|
95
92
|
const result = await api.db.migrate({ appId, dryRun: false });
|
|
96
|
-
|
|
93
|
+
if ((0, output_1.isJsonMode)()) {
|
|
94
|
+
// PRD:{"status": "applied", "from": "dev", "to": "online", "changes_applied": 2}
|
|
95
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
96
|
+
status: result.status ?? "applied",
|
|
97
|
+
from: result.from,
|
|
98
|
+
to: result.to,
|
|
99
|
+
changesApplied: result.changesApplied ?? result.changes.length,
|
|
100
|
+
}));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
104
|
+
const prefix = tty ? "✓" : "OK";
|
|
105
|
+
const arrow = tty ? "→" : "->";
|
|
106
|
+
const applied = result.changesApplied ?? result.changes.length;
|
|
107
|
+
(0, output_1.emit)(`${prefix} Applied ${result.from} ${arrow} ${result.to} (${String(applied)} changes)`);
|
|
97
108
|
}
|
|
98
|
-
|
|
109
|
+
// ── helpers ──
|
|
110
|
+
// PRD diff 输出:
|
|
111
|
+
// dev → online (2 changes):
|
|
112
|
+
//
|
|
113
|
+
// ALTER TABLE users ADD COLUMN avatar_url text;
|
|
114
|
+
// CREATE INDEX idx_users_avatar ON users(avatar_url);
|
|
115
|
+
function renderDiff(result) {
|
|
99
116
|
if ((0, output_1.isJsonMode)()) {
|
|
100
|
-
(0, output_1.emitOk)(
|
|
117
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
118
|
+
from: result.from,
|
|
119
|
+
to: result.to,
|
|
120
|
+
changes: result.changes.map((c) => ({
|
|
121
|
+
type: c.type,
|
|
122
|
+
table: c.table,
|
|
123
|
+
statement: c.statement,
|
|
124
|
+
})),
|
|
125
|
+
}));
|
|
101
126
|
return;
|
|
102
127
|
}
|
|
103
128
|
const tty = (0, render_1.isStdoutTty)();
|
|
@@ -105,17 +130,10 @@ function renderMigrate(result) {
|
|
|
105
130
|
(0, output_1.emit)(`No pending changes from ${result.from} to ${result.to}.`);
|
|
106
131
|
return;
|
|
107
132
|
}
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (result.dryRun) {
|
|
112
|
-
(0, output_1.emit)(`(dry-run: ${String(result.changes.length)} change(s) preview, run \`db migration apply\` to commit)`);
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
(0, output_1.emit)(`✓ Applied ${String(result.changesApplied ?? result.changes.length)} change(s) from ${result.from} to ${result.to}`);
|
|
116
|
-
}
|
|
133
|
+
const arrow = tty ? "→" : "->";
|
|
134
|
+
(0, output_1.emit)(`${result.from} ${arrow} ${result.to} (${String(result.changes.length)} changes):\n\n` +
|
|
135
|
+
result.changes.map((c) => ` ${c.statement}`).join("\n"));
|
|
117
136
|
}
|
|
118
|
-
// ── confirm ──
|
|
119
137
|
async function confirm(prompt) {
|
|
120
138
|
const rl = node_readline_1.default.createInterface({ input: process.stdin, output: process.stderr });
|
|
121
139
|
return new Promise((resolve) => {
|
|
@@ -42,19 +42,27 @@ async function handleDbQuota(opts) {
|
|
|
42
42
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
43
43
|
const data = await api.db.getDbQuota({ appId, dbBranch: opts.env });
|
|
44
44
|
if ((0, output_1.isJsonMode)()) {
|
|
45
|
-
|
|
45
|
+
// 配额未对接(storageQuotaBytes=0)时,quota / usage_percent 字段都不输出
|
|
46
|
+
const out = {
|
|
47
|
+
storageUsedBytes: data.storageUsedBytes,
|
|
48
|
+
tables: data.tables,
|
|
49
|
+
views: data.views,
|
|
50
|
+
};
|
|
51
|
+
if (data.storageQuotaBytes > 0) {
|
|
52
|
+
out.storageQuotaBytes = data.storageQuotaBytes;
|
|
53
|
+
out.usagePercent = data.usagePercent;
|
|
54
|
+
}
|
|
55
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(out));
|
|
46
56
|
return;
|
|
47
57
|
}
|
|
58
|
+
// PRD:单行 "Storage: 14.9 MB / 1 GB (1.5%)";配额未对接时只显示 used
|
|
48
59
|
const tty = (0, render_1.isStdoutTty)();
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const pct = data.storageQuotaBytes > 0 ? `${data.usagePercent.toFixed(1)}%` : "—";
|
|
60
|
+
const storageLine = data.storageQuotaBytes > 0
|
|
61
|
+
? `${(0, render_1.formatSize)(data.storageUsedBytes)} / ${(0, render_1.formatSize)(data.storageQuotaBytes)} (${data.usagePercent.toFixed(1)}%)`
|
|
62
|
+
: (0, render_1.formatSize)(data.storageUsedBytes);
|
|
53
63
|
(0, output_1.emit)((0, render_1.renderKeyValue)([
|
|
54
|
-
["
|
|
55
|
-
["
|
|
56
|
-
["
|
|
57
|
-
["tables", String(data.tables)],
|
|
58
|
-
["views", String(data.views)],
|
|
64
|
+
["Storage", storageLine],
|
|
65
|
+
["Tables", String(data.tables)],
|
|
66
|
+
["Views", String(data.views)],
|
|
59
67
|
], tty));
|
|
60
68
|
}
|
|
@@ -49,23 +49,36 @@ async function handleDbRecoveryDiff(target, opts) {
|
|
|
49
49
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
50
50
|
const ts = normalizeTimestamp(target);
|
|
51
51
|
const result = await api.db.recover({ appId, target: ts, dryRun: true });
|
|
52
|
-
|
|
52
|
+
renderDiff(result);
|
|
53
53
|
}
|
|
54
54
|
async function handleDbRecoveryApply(target, opts) {
|
|
55
55
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
56
56
|
const ts = normalizeTimestamp(target);
|
|
57
|
-
// PITR 高危:
|
|
58
|
-
const preview = await api.db.recover({ appId, target: ts, dryRun: true });
|
|
57
|
+
// PITR 高危:TTY 下先 diff 给用户审;--yes 直接执行
|
|
59
58
|
if (!opts.yes && !(0, output_1.isJsonMode)()) {
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
const preview = await api.db.recover({ appId, target: ts, dryRun: true });
|
|
60
|
+
renderDiff(preview);
|
|
61
|
+
const ok = await confirm(`? Restore database to ${preview.target}? This will overwrite current data. (y/N) `);
|
|
62
62
|
if (!ok) {
|
|
63
63
|
(0, output_1.emit)("Aborted.");
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
const result = await api.db.recover({ appId, target: ts, dryRun: false });
|
|
68
|
-
|
|
68
|
+
if ((0, output_1.isJsonMode)()) {
|
|
69
|
+
// PRD:{"status": "restored", "target": "...", "tables_affected": 2, "elapsed_seconds": 30}
|
|
70
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
71
|
+
status: result.status ?? "restored",
|
|
72
|
+
target: result.target,
|
|
73
|
+
tablesAffected: result.tablesAffected,
|
|
74
|
+
elapsedSeconds: result.elapsedSeconds ?? 0,
|
|
75
|
+
}));
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
79
|
+
const prefix = tty ? "✓" : "OK";
|
|
80
|
+
(0, output_1.emit)(`${prefix} Database restored to ${result.target} ` +
|
|
81
|
+
`(${String(result.tablesAffected)} tables affected, ${String(result.elapsedSeconds ?? 0)}s elapsed)`);
|
|
69
82
|
}
|
|
70
83
|
// ── helpers ──
|
|
71
84
|
/**
|
|
@@ -79,56 +92,90 @@ function normalizeTimestamp(input) {
|
|
|
79
92
|
next_actions: ["Usage: miaoda db recovery diff|apply <timestamp>"],
|
|
80
93
|
});
|
|
81
94
|
}
|
|
82
|
-
// YYYY-MM-DD:按 UTC 00:00:00
|
|
83
95
|
if (/^\d{4}-\d{2}-\d{2}$/.test(input)) {
|
|
84
96
|
return new Date(`${input}T00:00:00Z`).toISOString();
|
|
85
97
|
}
|
|
86
|
-
// YYYY-MM-DD HH:MM[:SS]:本地时间,让 Date 自己解析
|
|
87
98
|
if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2})?$/.test(input)) {
|
|
88
99
|
const d = new Date(input.replace(" ", "T"));
|
|
89
100
|
if (Number.isNaN(d.getTime())) {
|
|
90
|
-
throw new error_1.AppError("INVALID_TIMESTAMP", `
|
|
101
|
+
throw new error_1.AppError("INVALID_TIMESTAMP", `Invalid timestamp format '${input}'`, {
|
|
91
102
|
next_actions: [FORMAT_HINT],
|
|
92
103
|
});
|
|
93
104
|
}
|
|
94
105
|
return d.toISOString();
|
|
95
106
|
}
|
|
96
|
-
// 完整 ISO 8601
|
|
97
107
|
const d = new Date(input);
|
|
98
108
|
if (Number.isNaN(d.getTime())) {
|
|
99
|
-
throw new error_1.AppError("INVALID_TIMESTAMP", `
|
|
109
|
+
throw new error_1.AppError("INVALID_TIMESTAMP", `Invalid timestamp format '${input}'`, {
|
|
100
110
|
next_actions: [FORMAT_HINT],
|
|
101
111
|
});
|
|
102
112
|
}
|
|
103
113
|
return d.toISOString();
|
|
104
114
|
}
|
|
105
|
-
|
|
115
|
+
// PRD diff 输出(结构化 prose):
|
|
116
|
+
// Recovery preview (→ 2026-04-15T10:00:00Z):
|
|
117
|
+
//
|
|
118
|
+
// tables affected: 2
|
|
119
|
+
// users: +3 rows, -1 row, ~5 rows modified
|
|
120
|
+
// orders: table will be restored (was dropped at 10:25:00)
|
|
121
|
+
//
|
|
122
|
+
// estimated time: ~30s
|
|
123
|
+
function renderDiff(result) {
|
|
106
124
|
if ((0, output_1.isJsonMode)()) {
|
|
107
|
-
(0, output_1.emitOk)(
|
|
125
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
126
|
+
target: result.target,
|
|
127
|
+
tablesAffected: result.tablesAffected,
|
|
128
|
+
changes: result.changes.map((c) => ({
|
|
129
|
+
table: c.table,
|
|
130
|
+
inserted: c.inserted,
|
|
131
|
+
deleted: c.deleted,
|
|
132
|
+
modified: c.modified,
|
|
133
|
+
action: c.action,
|
|
134
|
+
droppedAt: c.droppedAt,
|
|
135
|
+
})),
|
|
136
|
+
estimatedSeconds: result.estimatedSeconds ?? 0,
|
|
137
|
+
}));
|
|
108
138
|
return;
|
|
109
139
|
}
|
|
110
140
|
const tty = (0, render_1.isStdoutTty)();
|
|
141
|
+
const arrow = tty ? "→" : "->";
|
|
111
142
|
if (result.changes.length === 0) {
|
|
112
|
-
(0, output_1.emit)(`
|
|
143
|
+
(0, output_1.emit)(`Recovery preview (${arrow} ${result.target}):\n\n` +
|
|
144
|
+
` No changes — database is already at this state.`);
|
|
113
145
|
return;
|
|
114
146
|
}
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
c.
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
147
|
+
const lines = [
|
|
148
|
+
`Recovery preview (${arrow} ${result.target}):`,
|
|
149
|
+
"",
|
|
150
|
+
` tables affected: ${String(result.tablesAffected)}`,
|
|
151
|
+
];
|
|
152
|
+
for (const c of result.changes) {
|
|
153
|
+
lines.push(` ${c.table}: ${describeChange(c)}`);
|
|
154
|
+
}
|
|
155
|
+
if (result.estimatedSeconds !== undefined) {
|
|
156
|
+
lines.push("");
|
|
157
|
+
lines.push(` estimated time: ~${String(result.estimatedSeconds)}s`);
|
|
158
|
+
}
|
|
159
|
+
(0, output_1.emit)(lines.join("\n"));
|
|
160
|
+
}
|
|
161
|
+
function describeChange(c) {
|
|
162
|
+
if (c.action === "restore_table" || c.action === "drop" || c.action === "create") {
|
|
163
|
+
const ts = c.droppedAt ? ` (was dropped at ${c.droppedAt})` : "";
|
|
164
|
+
if (c.action === "restore_table")
|
|
165
|
+
return `table will be restored${ts}`;
|
|
166
|
+
if (c.action === "drop")
|
|
167
|
+
return `table will be dropped${ts}`;
|
|
168
|
+
return `table will be created${ts}`;
|
|
128
169
|
}
|
|
129
|
-
|
|
130
|
-
|
|
170
|
+
const parts = [];
|
|
171
|
+
if (c.inserted !== undefined && c.inserted !== 0)
|
|
172
|
+
parts.push(`+${String(c.inserted)} rows`);
|
|
173
|
+
if (c.deleted !== undefined && c.deleted !== 0) {
|
|
174
|
+
parts.push(`-${String(c.deleted)} ${c.deleted === 1 ? "row" : "rows"}`);
|
|
131
175
|
}
|
|
176
|
+
if (c.modified !== undefined && c.modified !== 0)
|
|
177
|
+
parts.push(`~${String(c.modified)} rows modified`);
|
|
178
|
+
return parts.length === 0 ? "no changes" : parts.join(", ");
|
|
132
179
|
}
|
|
133
180
|
async function confirm(prompt) {
|
|
134
181
|
const rl = node_readline_1.default.createInterface({ input: process.stdin, output: process.stderr });
|
|
@@ -42,17 +42,25 @@ async function handleFileQuota(opts) {
|
|
|
42
42
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
43
43
|
const data = await api.file.getStorageQuota({ appId });
|
|
44
44
|
if ((0, output_1.isJsonMode)()) {
|
|
45
|
-
|
|
45
|
+
// 配额未对接(storageQuotaBytes=0)时,quota / usage_percent 字段都不输出
|
|
46
|
+
const out = {
|
|
47
|
+
storageUsedBytes: data.storageUsedBytes,
|
|
48
|
+
files: data.files,
|
|
49
|
+
};
|
|
50
|
+
if (data.storageQuotaBytes > 0) {
|
|
51
|
+
out.storageQuotaBytes = data.storageQuotaBytes;
|
|
52
|
+
out.usagePercent = data.usagePercent;
|
|
53
|
+
}
|
|
54
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(out));
|
|
46
55
|
return;
|
|
47
56
|
}
|
|
57
|
+
// PRD:单行 "Storage: 150 MB / 1 GB (15%)";配额未对接时只显示 used
|
|
48
58
|
const tty = (0, render_1.isStdoutTty)();
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
59
|
+
const storageLine = data.storageQuotaBytes > 0
|
|
60
|
+
? `${(0, render_1.formatSize)(data.storageUsedBytes)} / ${(0, render_1.formatSize)(data.storageQuotaBytes)} (${data.usagePercent.toFixed(1)}%)`
|
|
61
|
+
: (0, render_1.formatSize)(data.storageUsedBytes);
|
|
52
62
|
(0, output_1.emit)((0, render_1.renderKeyValue)([
|
|
53
|
-
["
|
|
54
|
-
["
|
|
55
|
-
["usage", pct],
|
|
56
|
-
["files", String(data.files)],
|
|
63
|
+
["Storage", storageLine],
|
|
64
|
+
["Files", String(data.files)],
|
|
57
65
|
], tty));
|
|
58
66
|
}
|
package/dist/utils/output.js
CHANGED
|
@@ -5,6 +5,7 @@ exports.emit = emit;
|
|
|
5
5
|
exports.emitError = emitError;
|
|
6
6
|
exports.emitOk = emitOk;
|
|
7
7
|
exports.emitPaged = emitPaged;
|
|
8
|
+
exports.snakeCaseKeys = snakeCaseKeys;
|
|
8
9
|
const config_1 = require("./config");
|
|
9
10
|
const error_1 = require("./error");
|
|
10
11
|
const colors_1 = require("./colors");
|
|
@@ -118,6 +119,27 @@ function emitOk(data) {
|
|
|
118
119
|
function emitPaged(items, nextCursor, hasMore) {
|
|
119
120
|
emit({ data: items, next_cursor: nextCursor, has_more: hasMore });
|
|
120
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* 把对象 / 数组里所有字符串 key 从 camelCase 转成 snake_case,递归处理嵌套对象。
|
|
124
|
+
* 后端 IDL 返回的 JSON 字段是 camelCase(`storageUsedBytes`),CLI 对外(PRD)
|
|
125
|
+
* 统一 snake_case(`storage_used_bytes`);emit JSON 前过一遍这个函数。
|
|
126
|
+
*/
|
|
127
|
+
function snakeCaseKeys(input) {
|
|
128
|
+
if (Array.isArray(input)) {
|
|
129
|
+
return input.map((item) => snakeCaseKeys(item));
|
|
130
|
+
}
|
|
131
|
+
if (input !== null && typeof input === "object") {
|
|
132
|
+
const out = {};
|
|
133
|
+
for (const [k, v] of Object.entries(input)) {
|
|
134
|
+
out[camelToSnake(k)] = snakeCaseKeys(v);
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
return input;
|
|
139
|
+
}
|
|
140
|
+
function camelToSnake(s) {
|
|
141
|
+
return s.replace(/[A-Z]/g, (m) => "_" + m.toLowerCase());
|
|
142
|
+
}
|
|
121
143
|
function toErrorInfo(err) {
|
|
122
144
|
if (err instanceof error_1.AppError)
|
|
123
145
|
return err.toJSON();
|