@lark-apaas/miaoda-cli 0.1.2-beta.3416c6d → 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.
- package/dist/api/db/api.js +331 -24
- package/dist/api/db/client.js +36 -0
- package/dist/api/db/index.js +11 -1
- package/dist/api/file/api.js +25 -23
- package/dist/api/file/index.js +2 -1
- package/dist/api/file/parsers.js +14 -3
- package/dist/cli/commands/db/index.js +571 -0
- package/dist/cli/commands/file/index.js +16 -2
- package/dist/cli/handlers/db/audit.js +294 -0
- package/dist/cli/handlers/db/changelog.js +130 -0
- package/dist/cli/handlers/db/data.js +4 -3
- package/dist/cli/handlers/db/index.js +17 -1
- package/dist/cli/handlers/db/migration.js +235 -0
- package/dist/cli/handlers/db/quota.js +68 -0
- package/dist/cli/handlers/db/recovery.js +357 -0
- package/dist/cli/handlers/db/sql.js +1 -1
- package/dist/cli/handlers/file/index.js +3 -1
- package/dist/cli/handlers/file/quota.js +66 -0
- package/dist/cli/handlers/file/stat.js +2 -1
- package/dist/utils/index.js +3 -1
- package/dist/utils/output.js +22 -0
- package/dist/utils/poll.js +27 -0
- package/dist/utils/time.js +31 -26
- package/package.json +1 -1
package/dist/api/db/api.js
CHANGED
|
@@ -4,27 +4,39 @@ exports.execSql = execSql;
|
|
|
4
4
|
exports.getSchema = getSchema;
|
|
5
5
|
exports.importData = importData;
|
|
6
6
|
exports.exportData = exportData;
|
|
7
|
+
exports.listDDLChangelog = listDDLChangelog;
|
|
8
|
+
exports.getAuditStatus = getAuditStatus;
|
|
9
|
+
exports.setAuditConfig = setAuditConfig;
|
|
10
|
+
exports.listAuditLog = listAuditLog;
|
|
11
|
+
exports.migrationInit = migrationInit;
|
|
12
|
+
exports.migrate = migrate;
|
|
13
|
+
exports.getMigrationStatus = getMigrationStatus;
|
|
14
|
+
exports.recover = recover;
|
|
15
|
+
exports.getRecoveryPreview = getRecoveryPreview;
|
|
16
|
+
exports.getDbQuota = getDbQuota;
|
|
7
17
|
const http_1 = require("../../utils/http");
|
|
8
18
|
const error_1 = require("../../utils/error");
|
|
9
19
|
const http_client_1 = require("@lark-apaas/http-client");
|
|
10
20
|
const client_1 = require("./client");
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* 1. 先尝试从 response body 解 envelope,命中 dataloom 业务 code → AppError
|
|
14
|
-
* 2. 兜底返 HttpError,保留真实 status 码与上下文
|
|
15
|
-
*
|
|
16
|
-
* 配合调用点的 try/catch + traceHttp,让 --verbose 在错误路径上也能拿到
|
|
17
|
-
* x-tt-logid 与 status,方便定位线上问题。
|
|
18
|
-
*/
|
|
19
|
-
async function mapDbHttpError(err, url, ctx,
|
|
20
|
-
/**
|
|
21
|
-
* 可选 hook:解析到响应 body 后,如果 envelope 命中业务错误并抛出 AppError,
|
|
22
|
-
* 调用方可以借此把 body 里的额外字段(如 multi-statement 的 partial results)
|
|
23
|
-
* 挂到 AppError 上。
|
|
24
|
-
*/
|
|
25
|
-
onErrorBody) {
|
|
21
|
+
const DEFAULT_TIMEOUT_HINT = "The server may still be processing your request. Retry the command if needed.";
|
|
22
|
+
async function mapDbHttpError(err, url, ctx, opts) {
|
|
26
23
|
if (err instanceof error_1.AppError)
|
|
27
24
|
throw err;
|
|
25
|
+
// 客户端超时 / abort:SdkHttpError 时 response 通常缺失(status=0),落到 throw 时
|
|
26
|
+
// message 形如 'Failed to recover: 0' 让用户看不懂。识别 abort / timeout 关键字后
|
|
27
|
+
// 单独抛专用 AppError(带领域可定制的 message + hint),不再包成通用 "Failed to X"。
|
|
28
|
+
if (err instanceof Error) {
|
|
29
|
+
const lower = err.message.toLowerCase();
|
|
30
|
+
if (lower.includes("aborted") ||
|
|
31
|
+
lower.includes("timeout") ||
|
|
32
|
+
err.name === "AbortError" ||
|
|
33
|
+
err.name === "TimeoutError") {
|
|
34
|
+
const code = opts?.timeout?.code ?? "REQUEST_TIMEOUT";
|
|
35
|
+
const message = opts?.timeout?.message ?? "Request timed out after 30s";
|
|
36
|
+
const hint = opts?.timeout?.hint ?? DEFAULT_TIMEOUT_HINT;
|
|
37
|
+
throw new error_1.AppError(code, message, { next_actions: [hint] });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
28
40
|
if (err instanceof http_client_1.HttpError) {
|
|
29
41
|
const status = err.response?.status ?? 0;
|
|
30
42
|
const statusText = err.response?.statusText ?? "";
|
|
@@ -35,8 +47,8 @@ onErrorBody) {
|
|
|
35
47
|
(0, client_1.extractData)(body); // 业务 code 命中 → 抛 AppError;不命中走兜底
|
|
36
48
|
}
|
|
37
49
|
catch (appErr) {
|
|
38
|
-
if (appErr instanceof error_1.AppError && onErrorBody) {
|
|
39
|
-
onErrorBody(body, appErr);
|
|
50
|
+
if (appErr instanceof error_1.AppError && opts?.onErrorBody) {
|
|
51
|
+
opts.onErrorBody(body, appErr);
|
|
40
52
|
}
|
|
41
53
|
throw appErr;
|
|
42
54
|
}
|
|
@@ -90,7 +102,17 @@ async function execSql(opts) {
|
|
|
90
102
|
}
|
|
91
103
|
catch (err) {
|
|
92
104
|
(0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
93
|
-
await mapDbHttpError(err, url, "Failed to execute SQL",
|
|
105
|
+
await mapDbHttpError(err, url, "Failed to execute SQL", {
|
|
106
|
+
onErrorBody: attachSqlPartialResults,
|
|
107
|
+
// SQL 路径单独的超时文案:PG 的 statement_timeout 会回滚整条事务,所以这里
|
|
108
|
+
// 明示「事务已回滚、没有改动落地」,并把 hint 引导到「简化 SQL / 加 LIMIT / 拆条」。
|
|
109
|
+
timeout: {
|
|
110
|
+
code: "SQL_EXECUTION_TIMEOUT",
|
|
111
|
+
message: "SQL execution timed out after 30s",
|
|
112
|
+
hint: "The transaction was rolled back; no changes were applied. " +
|
|
113
|
+
"Simplify the SQL, add filters or LIMIT for queries, or split it into smaller statements.",
|
|
114
|
+
},
|
|
115
|
+
});
|
|
94
116
|
throw err; // 不可达
|
|
95
117
|
}
|
|
96
118
|
const body = (await response.json());
|
|
@@ -247,10 +269,14 @@ async function exportData(opts) {
|
|
|
247
269
|
await mapDbHttpError(err, url, "Failed to export data");
|
|
248
270
|
throw err; // 不可达
|
|
249
271
|
}
|
|
250
|
-
// 成功路径:响应 body 通常是原始 CSV/JSON 字节,但部分错误场景下网关会返
|
|
272
|
+
// 成功路径:响应 body 通常是原始 CSV/SQL/JSON 字节,但部分错误场景下网关会返
|
|
251
273
|
// HTTP 200 + JSON envelope(status_code != "0"),需要在这里嗅探兜底。
|
|
252
|
-
const
|
|
253
|
-
|
|
274
|
+
const defaultContentType = {
|
|
275
|
+
csv: "text/csv",
|
|
276
|
+
sql: "text/plain",
|
|
277
|
+
json: "application/json",
|
|
278
|
+
};
|
|
279
|
+
const contentType = response.headers.get("Content-Type") ?? defaultContentType[opts.format];
|
|
254
280
|
const ab = await response.arrayBuffer();
|
|
255
281
|
const buf = Buffer.from(new Uint8Array(ab));
|
|
256
282
|
if (buf.length === 0) {
|
|
@@ -258,8 +284,9 @@ async function exportData(opts) {
|
|
|
258
284
|
}
|
|
259
285
|
// Envelope sniff:HTTP 200 + Content-Type 为 application/json + body 解析得到
|
|
260
286
|
// InnerEnvelope 且 status_code 非 "0" 时,按业务错误抛出。
|
|
261
|
-
//
|
|
262
|
-
|
|
287
|
+
// CSV / SQL 都是非 JSON 输出,application/json 响应必是错误信封;JSON 格式
|
|
288
|
+
// 成功响应自身就是 application/json,跳过 sniff 避免误判。
|
|
289
|
+
if (opts.format !== "json" && /application\/json/i.test(contentType)) {
|
|
263
290
|
try {
|
|
264
291
|
const parsed = JSON.parse(buf.toString("utf8"));
|
|
265
292
|
if (parsed.status_code != null && parsed.status_code !== "0") {
|
|
@@ -269,7 +296,7 @@ async function exportData(opts) {
|
|
|
269
296
|
}
|
|
270
297
|
catch (err) {
|
|
271
298
|
// 已经被 extractData 抛成 AppError → 透传;否则 JSON.parse 失败说明 body
|
|
272
|
-
// 真的是 CSV 文本,继续按成功流程走
|
|
299
|
+
// 真的是 CSV / SQL 文本,继续按成功流程走
|
|
273
300
|
if (err instanceof error_1.AppError)
|
|
274
301
|
throw err;
|
|
275
302
|
}
|
|
@@ -286,3 +313,283 @@ async function exportData(opts) {
|
|
|
286
313
|
recordCount,
|
|
287
314
|
};
|
|
288
315
|
}
|
|
316
|
+
// ── db changelog → InnerAdminListDDLChangelog ──
|
|
317
|
+
/**
|
|
318
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/changelog?table=&since=&until=&limit=&cursor=&dbBranch=
|
|
319
|
+
*
|
|
320
|
+
* 时间字段 since/until 由 CLI 端归一化为 ISO 8601 UTC 后透传;后端按 created_at 比较。
|
|
321
|
+
*/
|
|
322
|
+
async function listDDLChangelog(opts) {
|
|
323
|
+
const client = (0, http_1.getHttpClient)();
|
|
324
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/changelog", {
|
|
325
|
+
table: opts.table,
|
|
326
|
+
since: opts.since,
|
|
327
|
+
until: opts.until,
|
|
328
|
+
limit: opts.limit !== undefined ? String(opts.limit) : undefined,
|
|
329
|
+
cursor: opts.cursor,
|
|
330
|
+
dbBranch: opts.dbBranch,
|
|
331
|
+
});
|
|
332
|
+
const start = Date.now();
|
|
333
|
+
let response;
|
|
334
|
+
try {
|
|
335
|
+
response = await client.get(url);
|
|
336
|
+
(0, client_1.traceHttp)("GET", url, start, response);
|
|
337
|
+
}
|
|
338
|
+
catch (err) {
|
|
339
|
+
(0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
340
|
+
await mapDbHttpError(err, url, "Failed to list DDL changelog");
|
|
341
|
+
throw err; // 不可达
|
|
342
|
+
}
|
|
343
|
+
const body = (await response.json());
|
|
344
|
+
const data = (0, client_1.extractData)(body);
|
|
345
|
+
return {
|
|
346
|
+
items: data.items ?? [],
|
|
347
|
+
nextCursor: data.nextCursor && data.nextCursor !== "" ? data.nextCursor : null,
|
|
348
|
+
hasMore: Boolean(data.hasMore),
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
// ── db audit → InnerAdminGetAuditStatus / InnerAdminSetAuditConfig ──
|
|
352
|
+
/**
|
|
353
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/audit/status?table=&dbBranch=
|
|
354
|
+
* 查表审计开关状态。table 非空 → 单表过滤;空 → 返当前 workspace 全部已配置表。
|
|
355
|
+
*/
|
|
356
|
+
async function getAuditStatus(opts) {
|
|
357
|
+
const client = (0, http_1.getHttpClient)();
|
|
358
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/audit/status", {
|
|
359
|
+
table: opts.table,
|
|
360
|
+
dbBranch: opts.dbBranch,
|
|
361
|
+
});
|
|
362
|
+
const start = Date.now();
|
|
363
|
+
let response;
|
|
364
|
+
try {
|
|
365
|
+
response = await client.get(url);
|
|
366
|
+
(0, client_1.traceHttp)("GET", url, start, response);
|
|
367
|
+
}
|
|
368
|
+
catch (err) {
|
|
369
|
+
(0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
370
|
+
await mapDbHttpError(err, url, "Failed to get audit status");
|
|
371
|
+
throw err; // 不可达
|
|
372
|
+
}
|
|
373
|
+
const respBody = (await response.json());
|
|
374
|
+
const data = (0, client_1.extractData)(respBody);
|
|
375
|
+
return data.items ?? [];
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* 后端:POST /v1/dataloom/app/{appId}/db/audit/config
|
|
379
|
+
* 写:enabled=true 开启 / false 关闭。retention 仅 enabled=true 生效。
|
|
380
|
+
*/
|
|
381
|
+
async function setAuditConfig(opts) {
|
|
382
|
+
const client = (0, http_1.getHttpClient)();
|
|
383
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/audit/config");
|
|
384
|
+
const body = {
|
|
385
|
+
table: opts.table,
|
|
386
|
+
enabled: opts.enabled,
|
|
387
|
+
};
|
|
388
|
+
if (opts.retention !== undefined && opts.retention !== "")
|
|
389
|
+
body.retention = opts.retention;
|
|
390
|
+
if (opts.dbBranch !== undefined && opts.dbBranch !== "")
|
|
391
|
+
body.dbBranch = opts.dbBranch;
|
|
392
|
+
const start = Date.now();
|
|
393
|
+
let response;
|
|
394
|
+
try {
|
|
395
|
+
response = await client.post(url, body);
|
|
396
|
+
(0, client_1.traceHttp)("POST", url, start, response);
|
|
397
|
+
}
|
|
398
|
+
catch (err) {
|
|
399
|
+
(0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
400
|
+
await mapDbHttpError(err, url, "Failed to set audit config");
|
|
401
|
+
throw err; // 不可达
|
|
402
|
+
}
|
|
403
|
+
const respBody = (await response.json());
|
|
404
|
+
const data = (0, client_1.extractData)(respBody);
|
|
405
|
+
if (!data.status) {
|
|
406
|
+
throw new error_1.AppError("INTERNAL_DB_ERROR", "audit config response missing status field");
|
|
407
|
+
}
|
|
408
|
+
return data.status;
|
|
409
|
+
}
|
|
410
|
+
// ── db audit log → InnerAdminListAuditLog ──
|
|
411
|
+
/**
|
|
412
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/audit/log?tables=&since=&until=&limit=&cursor=&dbBranch=
|
|
413
|
+
*
|
|
414
|
+
* 走 admin-inner 接口而不是 InnerAdminExecuteSQL 直接 SELECT pg_audit:
|
|
415
|
+
* - operator 在 details JSONB 内是 user_id,服务端解析成 username
|
|
416
|
+
* - summary 后端按 type + before/after diff 合成(pg_audit 表无此列)
|
|
417
|
+
* - before/after JSONB 后端 JSON.stringify 后透传字符串,CLI 按需 parse
|
|
418
|
+
*
|
|
419
|
+
* 多表用逗号拼接走 query;后端按 target_table IN (...) 一次查。skipped 字段返
|
|
420
|
+
* 多表中无记录的表名,便于 CLI 展示 hint。
|
|
421
|
+
*/
|
|
422
|
+
async function listAuditLog(opts) {
|
|
423
|
+
if (opts.tables.length === 0) {
|
|
424
|
+
throw new error_1.AppError("ARGS_INVALID", "at least one table is required");
|
|
425
|
+
}
|
|
426
|
+
const client = (0, http_1.getHttpClient)();
|
|
427
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/audit/log", {
|
|
428
|
+
tables: opts.tables.join(","),
|
|
429
|
+
since: opts.since,
|
|
430
|
+
until: opts.until,
|
|
431
|
+
limit: opts.limit !== undefined ? String(opts.limit) : undefined,
|
|
432
|
+
cursor: opts.cursor,
|
|
433
|
+
dbBranch: opts.dbBranch,
|
|
434
|
+
});
|
|
435
|
+
const start = Date.now();
|
|
436
|
+
let response;
|
|
437
|
+
try {
|
|
438
|
+
response = await client.get(url);
|
|
439
|
+
(0, client_1.traceHttp)("GET", url, start, response);
|
|
440
|
+
}
|
|
441
|
+
catch (err) {
|
|
442
|
+
(0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
443
|
+
await mapDbHttpError(err, url, "Failed to list audit log");
|
|
444
|
+
throw err; // 不可达
|
|
445
|
+
}
|
|
446
|
+
const body = (await response.json());
|
|
447
|
+
const data = (0, client_1.extractData)(body);
|
|
448
|
+
return {
|
|
449
|
+
items: data.items ?? [],
|
|
450
|
+
nextCursor: data.nextCursor && data.nextCursor !== "" ? data.nextCursor : null,
|
|
451
|
+
hasMore: Boolean(data.hasMore),
|
|
452
|
+
skipped: data.skipped ?? [],
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
// ── db migration → InnerAdminMigrationInit / InnerAdminMigrate ──
|
|
456
|
+
/**
|
|
457
|
+
* 后端:POST /v1/dataloom/app/{appId}/db/enableMultiEnv
|
|
458
|
+
* 单库 → dev/online 双库初始化,不可逆。对应公开 API EnableMultiEnvDB 的 admin-inner 通道。
|
|
459
|
+
*/
|
|
460
|
+
async function migrationInit(opts) {
|
|
461
|
+
const client = (0, http_1.getHttpClient)();
|
|
462
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/enableMultiEnv");
|
|
463
|
+
const body = {};
|
|
464
|
+
if (opts.syncData !== undefined)
|
|
465
|
+
body.syncData = opts.syncData;
|
|
466
|
+
const start = Date.now();
|
|
467
|
+
let response;
|
|
468
|
+
try {
|
|
469
|
+
response = await client.post(url, body);
|
|
470
|
+
(0, client_1.traceHttp)("POST", url, start, response);
|
|
471
|
+
}
|
|
472
|
+
catch (err) {
|
|
473
|
+
(0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
474
|
+
await mapDbHttpError(err, url, "Failed to init migration");
|
|
475
|
+
throw err; // 不可达
|
|
476
|
+
}
|
|
477
|
+
const respBody = (await response.json());
|
|
478
|
+
return (0, client_1.extractData)(respBody);
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* 后端:POST /v1/dataloom/app/{appId}/db/migration
|
|
482
|
+
* 合并 diff + apply:dryRun=true 只返 changes 不下发;dryRun=false 才执行。
|
|
483
|
+
*/
|
|
484
|
+
async function migrate(opts) {
|
|
485
|
+
const client = (0, http_1.getHttpClient)();
|
|
486
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/migration");
|
|
487
|
+
const start = Date.now();
|
|
488
|
+
let response;
|
|
489
|
+
try {
|
|
490
|
+
response = await client.post(url, { dryRun: opts.dryRun });
|
|
491
|
+
(0, client_1.traceHttp)("POST", url, start, response);
|
|
492
|
+
}
|
|
493
|
+
catch (err) {
|
|
494
|
+
(0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
495
|
+
await mapDbHttpError(err, url, "Failed to migrate");
|
|
496
|
+
throw err; // 不可达
|
|
497
|
+
}
|
|
498
|
+
const respBody = (await response.json());
|
|
499
|
+
return (0, client_1.extractData)(respBody);
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/migration/status?taskId=...
|
|
503
|
+
* CLI 拿到 migration apply 的 taskId 后定时调本接口,直到 status=success/failed。
|
|
504
|
+
* 网络层超时仍走 mapDbHttpError → 单次 30s;轮询节奏由 CLI handler 自行控制。
|
|
505
|
+
*/
|
|
506
|
+
async function getMigrationStatus(opts) {
|
|
507
|
+
const client = (0, http_1.getHttpClient)();
|
|
508
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/migration/status", {
|
|
509
|
+
taskId: opts.taskId,
|
|
510
|
+
dbBranch: opts.dbBranch,
|
|
511
|
+
});
|
|
512
|
+
const start = Date.now();
|
|
513
|
+
let response;
|
|
514
|
+
try {
|
|
515
|
+
response = await client.get(url);
|
|
516
|
+
(0, client_1.traceHttp)("GET", url, start, response);
|
|
517
|
+
}
|
|
518
|
+
catch (err) {
|
|
519
|
+
(0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
520
|
+
await mapDbHttpError(err, url, "Failed to get migration status");
|
|
521
|
+
throw err; // 不可达
|
|
522
|
+
}
|
|
523
|
+
const body = (await response.json());
|
|
524
|
+
return (0, client_1.extractData)(body);
|
|
525
|
+
}
|
|
526
|
+
// ── db recovery → InnerAdminRecover ──
|
|
527
|
+
/**
|
|
528
|
+
* 后端:POST /v1/dataloom/app/{appId}/db/recovery
|
|
529
|
+
* 合并 PITR diff + apply:dryRun=true 预览影响;dryRun=false 触发恢复。
|
|
530
|
+
*/
|
|
531
|
+
async function recover(opts) {
|
|
532
|
+
const client = (0, http_1.getHttpClient)();
|
|
533
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/recovery");
|
|
534
|
+
const start = Date.now();
|
|
535
|
+
let response;
|
|
536
|
+
try {
|
|
537
|
+
response = await client.post(url, {
|
|
538
|
+
target: opts.target,
|
|
539
|
+
dryRun: opts.dryRun,
|
|
540
|
+
});
|
|
541
|
+
(0, client_1.traceHttp)("POST", url, start, response);
|
|
542
|
+
}
|
|
543
|
+
catch (err) {
|
|
544
|
+
(0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
545
|
+
await mapDbHttpError(err, url, "Failed to recover");
|
|
546
|
+
throw err; // 不可达
|
|
547
|
+
}
|
|
548
|
+
const respBody = (await response.json());
|
|
549
|
+
return (0, client_1.extractData)(respBody);
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/recovery/preview?previewRequestId=...
|
|
553
|
+
* CLI 拿到 recovery diff 的 previewRequestId 后定时调本接口直到 previewStatus=success/failed。
|
|
554
|
+
*/
|
|
555
|
+
async function getRecoveryPreview(opts) {
|
|
556
|
+
const client = (0, http_1.getHttpClient)();
|
|
557
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/recovery/preview", {
|
|
558
|
+
previewRequestId: opts.previewRequestId,
|
|
559
|
+
dbBranch: opts.dbBranch,
|
|
560
|
+
});
|
|
561
|
+
const start = Date.now();
|
|
562
|
+
let response;
|
|
563
|
+
try {
|
|
564
|
+
response = await client.get(url);
|
|
565
|
+
(0, client_1.traceHttp)("GET", url, start, response);
|
|
566
|
+
}
|
|
567
|
+
catch (err) {
|
|
568
|
+
(0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
569
|
+
await mapDbHttpError(err, url, "Failed to get recovery preview");
|
|
570
|
+
throw err; // 不可达
|
|
571
|
+
}
|
|
572
|
+
const body = (await response.json());
|
|
573
|
+
return (0, client_1.extractData)(body);
|
|
574
|
+
}
|
|
575
|
+
// ── db quota → InnerAdminGetDbQuota ──
|
|
576
|
+
/**
|
|
577
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/quota?dbBranch=
|
|
578
|
+
*/
|
|
579
|
+
async function getDbQuota(opts) {
|
|
580
|
+
const client = (0, http_1.getHttpClient)();
|
|
581
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/quota", { dbBranch: opts.dbBranch });
|
|
582
|
+
const start = Date.now();
|
|
583
|
+
let response;
|
|
584
|
+
try {
|
|
585
|
+
response = await client.get(url);
|
|
586
|
+
(0, client_1.traceHttp)("GET", url, start, response);
|
|
587
|
+
}
|
|
588
|
+
catch (err) {
|
|
589
|
+
(0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
590
|
+
await mapDbHttpError(err, url, "Failed to get db quota");
|
|
591
|
+
throw err; // 不可达
|
|
592
|
+
}
|
|
593
|
+
const respBody = (await response.json());
|
|
594
|
+
return (0, client_1.extractData)(respBody);
|
|
595
|
+
}
|
package/dist/api/db/client.js
CHANGED
|
@@ -129,6 +129,42 @@ const BIZ_ERR_MAP = new Map(Object.entries({
|
|
|
129
129
|
// k_dl_1300015:SELECT 结果超过 1000 行硬拦;多行 hint 由 output.ts 的
|
|
130
130
|
// SERVER_ERROR_HINTS 按语义 code 兜底,这里只做 code 改名
|
|
131
131
|
k_dl_1300015: { code: "RESULT_SET_TOO_LARGE" },
|
|
132
|
+
// audit
|
|
133
|
+
k_dl_1310001: {
|
|
134
|
+
code: "AUDIT_ALREADY_ENABLED",
|
|
135
|
+
hint: "Use `miaoda db audit status <table>` to confirm current state.",
|
|
136
|
+
},
|
|
137
|
+
k_dl_1310002: {
|
|
138
|
+
code: "AUDIT_NOT_ENABLED",
|
|
139
|
+
hint: "Run `miaoda db audit enable <table>` first.",
|
|
140
|
+
},
|
|
141
|
+
k_dl_1310003: {
|
|
142
|
+
code: "INVALID_RETENTION",
|
|
143
|
+
hint: "Allowed values: 7d, 30d, 180d, 360d, forever.",
|
|
144
|
+
},
|
|
145
|
+
// migration
|
|
146
|
+
k_dl_1320001: {
|
|
147
|
+
code: "MIGRATION_NOT_AVAILABLE",
|
|
148
|
+
hint: "Migration commands require an expert-mode application.",
|
|
149
|
+
},
|
|
150
|
+
k_dl_1320002: { code: "MULTI_ENV_ALREADY_INITIALIZED" },
|
|
151
|
+
k_dl_1320003: {
|
|
152
|
+
code: "NO_PENDING_CHANGES",
|
|
153
|
+
hint: "dev and online schemas are already in sync.",
|
|
154
|
+
},
|
|
155
|
+
// recovery
|
|
156
|
+
k_dl_1330001: {
|
|
157
|
+
code: "RECOVERY_WINDOW_EXCEEDED",
|
|
158
|
+
hint: "Pick a timestamp inside the supported recovery window.",
|
|
159
|
+
},
|
|
160
|
+
k_dl_1330002: {
|
|
161
|
+
code: "RECOVERY_IN_PROGRESS",
|
|
162
|
+
hint: "Wait for the running recovery to finish, or check its status.",
|
|
163
|
+
},
|
|
164
|
+
k_dl_1330003: {
|
|
165
|
+
code: "INVALID_TIMESTAMP",
|
|
166
|
+
hint: "Use ISO 8601 / yyyy-mm-dd / yyyy-mm-dd HH:MM:SS.",
|
|
167
|
+
},
|
|
132
168
|
}));
|
|
133
169
|
/** PG SQLSTATE → CLI code(当前 dataloom 不一定透传,预留未来使用) */
|
|
134
170
|
exports.SQLSTATE_MAP = {
|
package/dist/api/db/index.js
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
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.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.getRecoveryPreview = exports.recover = exports.getMigrationStatus = 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
|
+
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; } });
|
|
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; } });
|
|
13
|
+
Object.defineProperty(exports, "migrationInit", { enumerable: true, get: function () { return api_1.migrationInit; } });
|
|
14
|
+
Object.defineProperty(exports, "migrate", { enumerable: true, get: function () { return api_1.migrate; } });
|
|
15
|
+
Object.defineProperty(exports, "getMigrationStatus", { enumerable: true, get: function () { return api_1.getMigrationStatus; } });
|
|
16
|
+
Object.defineProperty(exports, "recover", { enumerable: true, get: function () { return api_1.recover; } });
|
|
17
|
+
Object.defineProperty(exports, "getRecoveryPreview", { enumerable: true, get: function () { return api_1.getRecoveryPreview; } });
|
|
18
|
+
Object.defineProperty(exports, "getDbQuota", { enumerable: true, get: function () { return api_1.getDbQuota; } });
|
|
9
19
|
var client_1 = require("./client");
|
|
10
20
|
Object.defineProperty(exports, "buildInnerUrl", { enumerable: true, get: function () { return client_1.buildInnerUrl; } });
|
|
11
21
|
Object.defineProperty(exports, "ensureInnerSuccess", { enumerable: true, get: function () { return client_1.ensureInnerSuccess; } });
|
package/dist/api/file/api.js
CHANGED
|
@@ -8,8 +8,10 @@ exports.uploadFile = uploadFile;
|
|
|
8
8
|
exports.signDownload = signDownload;
|
|
9
9
|
exports.downloadFile = downloadFile;
|
|
10
10
|
exports.deleteFiles = deleteFiles;
|
|
11
|
+
exports.getStorageQuota = getStorageQuota;
|
|
11
12
|
const error_1 = require("../../utils/error");
|
|
12
13
|
const logger_1 = require("../../utils/logger");
|
|
14
|
+
const time_1 = require("../../utils/time");
|
|
13
15
|
const client_1 = require("./client");
|
|
14
16
|
const detect_1 = require("./detect");
|
|
15
17
|
const parsers_1 = require("./parsers");
|
|
@@ -107,31 +109,17 @@ function buildFilterExpr(opts) {
|
|
|
107
109
|
* flagName 用于错误信息,调用方传 "--uploaded-since" 或 "--uploaded-until"。
|
|
108
110
|
*/
|
|
109
111
|
function parseTimeFilterMs(input, flagName) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const rel = RELATIVE.exec(input);
|
|
113
|
-
if (rel) {
|
|
114
|
-
const n = parseInt(rel[1], 10);
|
|
115
|
-
if (n <= 0) {
|
|
116
|
-
throw new error_1.AppError("ARGS_INVALID", `${flagName} "${input}" 必须是正整数 + 单位(s / m / h / d / w)`);
|
|
117
|
-
}
|
|
118
|
-
const UNIT_MS = {
|
|
119
|
-
s: 1_000,
|
|
120
|
-
m: 60_000,
|
|
121
|
-
h: 3_600_000,
|
|
122
|
-
d: 86_400_000,
|
|
123
|
-
w: 604_800_000,
|
|
124
|
-
};
|
|
125
|
-
return Date.now() - n * UNIT_MS[rel[2]];
|
|
112
|
+
try {
|
|
113
|
+
return (0, time_1.parseTimeToMs)(input);
|
|
126
114
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
115
|
+
catch (err) {
|
|
116
|
+
if (err instanceof error_1.AppError) {
|
|
117
|
+
throw new error_1.AppError(err.code, `${flagName}: ${err.message}`, {
|
|
118
|
+
next_actions: err.next_actions,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
throw err;
|
|
133
122
|
}
|
|
134
|
-
return ms;
|
|
135
123
|
}
|
|
136
124
|
/**
|
|
137
125
|
* 列出文件:所有过滤下推到后端 FilterExpression,纯精确匹配,无 glob。
|
|
@@ -519,3 +507,17 @@ async function deleteFiles(opts) {
|
|
|
519
507
|
}
|
|
520
508
|
return { deleted, failed };
|
|
521
509
|
}
|
|
510
|
+
// ── storage quota ──
|
|
511
|
+
/**
|
|
512
|
+
* 后端:GET /v1/storage/app/{appId}/bucket/{bucketId}/quota
|
|
513
|
+
* 单 bucket 用量;StorageQuotaBytes 暂未对接,CLI 拿到 0 时按 "—" 渲染。
|
|
514
|
+
* bucketId 缺省时走默认 bucket(与 ls / stat / cp 等一致)。
|
|
515
|
+
*/
|
|
516
|
+
async function getStorageQuota(opts) {
|
|
517
|
+
const bucketId = opts.bucketId ?? (await (0, client_1.getDefaultBucketId)(opts.appId));
|
|
518
|
+
const url = `/v1/storage/app/${encodeURIComponent(opts.appId)}/bucket/${encodeURIComponent(bucketId)}/quota`;
|
|
519
|
+
const body = await (0, client_1.doGet)(url, {
|
|
520
|
+
errorContext: `fetch storage quota for app '${opts.appId}' bucket '${bucketId}'`,
|
|
521
|
+
});
|
|
522
|
+
return extractEnvelope(body);
|
|
523
|
+
}
|
package/dist/api/file/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toAbsolutePath = exports.looksLikePath = exports.resetBucketCache = exports.getDefaultBucketId = exports.parseTimeFilterMs = exports.resolveInputs = exports.deleteFiles = exports.downloadFile = exports.signDownload = exports.uploadFile = exports.statFile = exports.listFiles = void 0;
|
|
3
|
+
exports.toAbsolutePath = exports.looksLikePath = exports.resetBucketCache = exports.getDefaultBucketId = exports.getStorageQuota = exports.parseTimeFilterMs = exports.resolveInputs = exports.deleteFiles = exports.downloadFile = exports.signDownload = exports.uploadFile = exports.statFile = exports.listFiles = void 0;
|
|
4
4
|
var api_1 = require("./api");
|
|
5
5
|
Object.defineProperty(exports, "listFiles", { enumerable: true, get: function () { return api_1.listFiles; } });
|
|
6
6
|
Object.defineProperty(exports, "statFile", { enumerable: true, get: function () { return api_1.statFile; } });
|
|
@@ -10,6 +10,7 @@ Object.defineProperty(exports, "downloadFile", { enumerable: true, get: function
|
|
|
10
10
|
Object.defineProperty(exports, "deleteFiles", { enumerable: true, get: function () { return api_1.deleteFiles; } });
|
|
11
11
|
Object.defineProperty(exports, "resolveInputs", { enumerable: true, get: function () { return api_1.resolveInputs; } });
|
|
12
12
|
Object.defineProperty(exports, "parseTimeFilterMs", { enumerable: true, get: function () { return api_1.parseTimeFilterMs; } });
|
|
13
|
+
Object.defineProperty(exports, "getStorageQuota", { enumerable: true, get: function () { return api_1.getStorageQuota; } });
|
|
13
14
|
var client_1 = require("./client");
|
|
14
15
|
Object.defineProperty(exports, "getDefaultBucketId", { enumerable: true, get: function () { return client_1.getDefaultBucketId; } });
|
|
15
16
|
Object.defineProperty(exports, "resetBucketCache", { enumerable: true, get: function () { return client_1.resetBucketCache; } });
|
package/dist/api/file/parsers.js
CHANGED
|
@@ -41,8 +41,9 @@ function parseAttachment(att) {
|
|
|
41
41
|
const downloadUrl = str(att.downloadURL);
|
|
42
42
|
if (downloadUrl)
|
|
43
43
|
info.download_url = downloadUrl;
|
|
44
|
-
// uploaded_by
|
|
45
|
-
|
|
44
|
+
// uploaded_by 输出 {id, name} 对象:后端 createdBy: {userID, name, email, ...},
|
|
45
|
+
// 任一非空都建对象;JSON 用对象、pretty 渲染只取 name。
|
|
46
|
+
const uploader = toUploaderRef(att.createdBy);
|
|
46
47
|
if (uploader)
|
|
47
48
|
info.uploaded_by = uploader;
|
|
48
49
|
return info;
|
|
@@ -61,8 +62,18 @@ function parseHead(data, filePath) {
|
|
|
61
62
|
const downloadUrl = str(outer.downloadURL);
|
|
62
63
|
if (downloadUrl)
|
|
63
64
|
info.download_url = downloadUrl;
|
|
64
|
-
const uploader =
|
|
65
|
+
const uploader = toUploaderRef(outer.createdBy);
|
|
65
66
|
if (uploader)
|
|
66
67
|
info.uploaded_by = uploader;
|
|
67
68
|
return info;
|
|
68
69
|
}
|
|
70
|
+
/** 把后端 UserRef 归一成 CLI 暴露的 {id, name};id / name 全空时返 undefined 略过字段。 */
|
|
71
|
+
function toUploaderRef(user) {
|
|
72
|
+
if (!user)
|
|
73
|
+
return undefined;
|
|
74
|
+
const id = str(user.userID);
|
|
75
|
+
const name = str(user.name);
|
|
76
|
+
if (id === "" && name === "")
|
|
77
|
+
return undefined;
|
|
78
|
+
return { id, name };
|
|
79
|
+
}
|