@lark-apaas/miaoda-cli 0.1.2 → 0.1.3-alpha.2a09432

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 (84) 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 +361 -54
  7. package/dist/api/db/client.js +60 -24
  8. package/dist/api/db/index.js +11 -1
  9. package/dist/api/db/parsers.js +20 -20
  10. package/dist/api/db/sql-keywords.js +87 -87
  11. package/dist/api/deploy/api.js +60 -0
  12. package/dist/api/deploy/index.js +16 -0
  13. package/dist/api/deploy/schemas.js +105 -0
  14. package/dist/api/deploy/types.js +22 -0
  15. package/dist/api/file/api.js +89 -87
  16. package/dist/api/file/client.js +62 -22
  17. package/dist/api/file/detect.js +3 -3
  18. package/dist/api/file/index.js +2 -1
  19. package/dist/api/file/parsers.js +18 -7
  20. package/dist/api/index.js +7 -1
  21. package/dist/api/observability/api.js +52 -0
  22. package/dist/api/observability/index.js +16 -0
  23. package/dist/api/observability/schemas.js +60 -0
  24. package/dist/api/observability/types.js +27 -0
  25. package/dist/api/plugin/api.js +31 -31
  26. package/dist/cli/commands/app/index.js +62 -0
  27. package/dist/cli/commands/db/index.js +599 -59
  28. package/dist/cli/commands/deploy/index.js +155 -0
  29. package/dist/cli/commands/file/index.js +90 -62
  30. package/dist/cli/commands/index.js +10 -6
  31. package/dist/cli/commands/observability/index.js +240 -0
  32. package/dist/cli/commands/plugin/index.js +27 -27
  33. package/dist/cli/commands/shared.js +86 -10
  34. package/dist/cli/handlers/app/get.js +47 -0
  35. package/dist/cli/handlers/app/index.js +7 -0
  36. package/dist/cli/handlers/app/update.js +59 -0
  37. package/dist/cli/handlers/db/_operator.js +35 -0
  38. package/dist/cli/handlers/db/audit.js +303 -0
  39. package/dist/cli/handlers/db/changelog.js +132 -0
  40. package/dist/cli/handlers/db/data.js +34 -34
  41. package/dist/cli/handlers/db/index.js +17 -1
  42. package/dist/cli/handlers/db/migration.js +235 -0
  43. package/dist/cli/handlers/db/quota.js +68 -0
  44. package/dist/cli/handlers/db/recovery.js +357 -0
  45. package/dist/cli/handlers/db/schema.js +35 -36
  46. package/dist/cli/handlers/db/sql.js +70 -71
  47. package/dist/cli/handlers/deploy/deploy.js +84 -0
  48. package/dist/cli/handlers/deploy/error-log.js +60 -0
  49. package/dist/cli/handlers/deploy/format.js +39 -0
  50. package/dist/cli/handlers/deploy/get.js +71 -0
  51. package/dist/cli/handlers/deploy/helpers.js +41 -0
  52. package/dist/cli/handlers/deploy/history.js +70 -0
  53. package/dist/cli/handlers/deploy/index.js +14 -0
  54. package/dist/cli/handlers/deploy/polling.js +162 -0
  55. package/dist/cli/handlers/file/cp.js +31 -32
  56. package/dist/cli/handlers/file/index.js +3 -1
  57. package/dist/cli/handlers/file/ls.js +6 -7
  58. package/dist/cli/handlers/file/quota.js +66 -0
  59. package/dist/cli/handlers/file/rm.js +25 -26
  60. package/dist/cli/handlers/file/sign.js +4 -5
  61. package/dist/cli/handlers/file/stat.js +11 -11
  62. package/dist/cli/handlers/observability/analytics.js +212 -0
  63. package/dist/cli/handlers/observability/helpers.js +66 -0
  64. package/dist/cli/handlers/observability/index.js +12 -0
  65. package/dist/cli/handlers/observability/log.js +94 -0
  66. package/dist/cli/handlers/observability/metric.js +208 -0
  67. package/dist/cli/handlers/observability/trace.js +102 -0
  68. package/dist/cli/handlers/plugin/plugin-local.js +53 -53
  69. package/dist/cli/handlers/plugin/plugin.js +15 -15
  70. package/dist/cli/help.js +16 -16
  71. package/dist/main.js +13 -9
  72. package/dist/utils/args.js +8 -0
  73. package/dist/utils/colors.js +2 -2
  74. package/dist/utils/config.js +2 -2
  75. package/dist/utils/devops-error.js +28 -0
  76. package/dist/utils/error.js +2 -2
  77. package/dist/utils/git.js +29 -0
  78. package/dist/utils/http.js +119 -1
  79. package/dist/utils/index.js +15 -1
  80. package/dist/utils/output.js +373 -20
  81. package/dist/utils/poll.js +27 -0
  82. package/dist/utils/render.js +27 -27
  83. package/dist/utils/time.js +208 -0
  84. package/package.json +7 -5
@@ -4,30 +4,42 @@ 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
- * SDK 抛出的 HttpError 统一映射成 CLI 层错误:
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
- const statusText = err.response?.statusText ?? "";
42
+ const statusText = err.response?.statusText ?? '';
31
43
  try {
32
44
  const body = (await err.response?.json());
33
45
  if (body) {
@@ -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
  }
@@ -79,18 +91,28 @@ function attachSqlPartialResults(body, appErr) {
79
91
  */
80
92
  async function execSql(opts) {
81
93
  const client = (0, http_1.getHttpClient)();
82
- const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/sql", {
94
+ const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/sql', {
83
95
  dbBranch: opts.dbBranch,
84
96
  });
85
97
  const start = Date.now();
86
98
  let response;
87
99
  try {
88
100
  response = await client.post(url, { sql: opts.sql });
89
- (0, client_1.traceHttp)("POST", url, start, response);
101
+ (0, client_1.traceHttp)('POST', url, start, response);
90
102
  }
91
103
  catch (err) {
92
- (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", attachSqlPartialResults);
104
+ (0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
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());
@@ -101,11 +123,11 @@ async function execSql(opts) {
101
123
  // results 末尾追加一条 SqlType="ERROR" 的哨兵,data 字段是 {code,message} JSON。
102
124
  // 这样网关在错误路径不会吞业务字段,CLI 拿到完整 partial_results + statement_index。
103
125
  const last = results.at(-1);
104
- if (last?.sqlType === "ERROR") {
126
+ if (last?.sqlType === 'ERROR') {
105
127
  const appErr = parseSqlErrorSentinel(last.data);
106
128
  appErr.partial_results = results.slice(0, -1);
107
129
  const stmtIdx = data.errorStatementIndex;
108
- if (typeof stmtIdx === "number")
130
+ if (typeof stmtIdx === 'number')
109
131
  appErr.statement_index = stmtIdx;
110
132
  throw appErr;
111
133
  }
@@ -137,12 +159,12 @@ async function execSql(opts) {
137
159
  function parseSqlErrorSentinel(payload) {
138
160
  try {
139
161
  const parsed = JSON.parse(payload);
140
- const code = typeof parsed.code === "string" && parsed.code !== "" ? parsed.code : "INTERNAL_DB_ERROR";
141
- const message = typeof parsed.message === "string" ? parsed.message : payload;
162
+ const code = typeof parsed.code === 'string' && parsed.code !== '' ? parsed.code : 'INTERNAL_DB_ERROR';
163
+ const message = typeof parsed.message === 'string' ? parsed.message : payload;
142
164
  return (0, client_1.mapDataloomBizError)(code, message);
143
165
  }
144
166
  catch {
145
- return new error_1.AppError("INTERNAL_DB_ERROR", payload);
167
+ return new error_1.AppError('INTERNAL_DB_ERROR', payload);
146
168
  }
147
169
  }
148
170
  // ── db schema → InnerGetSchema ──
@@ -154,21 +176,21 @@ function parseSqlErrorSentinel(payload) {
154
176
  */
155
177
  async function getSchema(opts) {
156
178
  const client = (0, http_1.getHttpClient)();
157
- const url = (0, client_1.buildInnerUrl)(opts.appId, "/db/schema", {
158
- format: opts.format ?? "schema",
179
+ const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/schema', {
180
+ format: opts.format ?? 'schema',
159
181
  tableNames: opts.tableNames,
160
- includeStats: opts.includeStats ? "true" : undefined,
182
+ includeStats: opts.includeStats ? 'true' : undefined,
161
183
  dbBranch: opts.dbBranch,
162
184
  });
163
185
  const start = Date.now();
164
186
  let response;
165
187
  try {
166
188
  response = await client.get(url);
167
- (0, client_1.traceHttp)("GET", url, start, response);
189
+ (0, client_1.traceHttp)('GET', url, start, response);
168
190
  }
169
191
  catch (err) {
170
- (0, client_1.traceHttp)("GET", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
171
- await mapDbHttpError(err, url, "Failed to get schema");
192
+ (0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
193
+ await mapDbHttpError(err, url, 'Failed to get schema');
172
194
  throw err; // 不可达
173
195
  }
174
196
  const body = (await response.json());
@@ -188,24 +210,24 @@ async function getSchema(opts) {
188
210
  */
189
211
  async function importData(opts) {
190
212
  const client = (0, http_1.getHttpClient)();
191
- const url = (0, client_1.buildInnerUrl)(opts.appId, "/data/import");
213
+ const url = (0, client_1.buildInnerUrl)(opts.appId, '/data/import');
192
214
  const reqBody = {
193
215
  tableName: opts.tableName,
194
216
  format: opts.format,
195
- records: opts.body.toString("utf8"),
217
+ records: opts.body.toString('utf8'),
196
218
  };
197
- if (opts.dbBranch !== undefined && opts.dbBranch !== "") {
219
+ if (opts.dbBranch !== undefined && opts.dbBranch !== '') {
198
220
  reqBody.dbBranch = opts.dbBranch;
199
221
  }
200
222
  const start = Date.now();
201
223
  let response;
202
224
  try {
203
225
  response = await client.post(url, reqBody);
204
- (0, client_1.traceHttp)("POST", url, start, response);
226
+ (0, client_1.traceHttp)('POST', url, start, response);
205
227
  }
206
228
  catch (err) {
207
- (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
208
- await mapDbHttpError(err, url, "Failed to import data");
229
+ (0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
230
+ await mapDbHttpError(err, url, 'Failed to import data');
209
231
  throw err; // 不可达
210
232
  }
211
233
  // 后端 InnerAdminImportData 响应里 data 直接返 {tableName, recordCount, durationMs}
@@ -229,7 +251,7 @@ async function importData(opts) {
229
251
  */
230
252
  async function exportData(opts) {
231
253
  const client = (0, http_1.getHttpClient)();
232
- const url = (0, client_1.buildInnerUrl)(opts.appId, "/data/export", {
254
+ const url = (0, client_1.buildInnerUrl)(opts.appId, '/data/export', {
233
255
  tableName: opts.tableName,
234
256
  format: opts.format,
235
257
  limit: String(opts.limit ?? 5000),
@@ -239,43 +261,48 @@ async function exportData(opts) {
239
261
  const start = Date.now();
240
262
  let response;
241
263
  try {
242
- response = await client.request({ method: "POST", url });
243
- (0, client_1.traceHttp)("POST", url, start, response);
264
+ response = await client.request({ method: 'POST', url });
265
+ (0, client_1.traceHttp)('POST', url, start, response);
244
266
  }
245
267
  catch (err) {
246
- (0, client_1.traceHttp)("POST", url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
247
- await mapDbHttpError(err, url, "Failed to export data");
268
+ (0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
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 contentType = response.headers.get("Content-Type") ??
253
- (opts.format === "csv" ? "text/csv" : "application/json");
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) {
257
- throw new error_1.AppError("INTERNAL_DB_ERROR", "Empty export response body");
283
+ throw new error_1.AppError('INTERNAL_DB_ERROR', 'Empty export response body');
258
284
  }
259
285
  // Envelope sniff:HTTP 200 + Content-Type 为 application/json + body 解析得到
260
286
  // InnerEnvelope 且 status_code 非 "0" 时,按业务错误抛出。
261
- // CSV 格式做 sniff —— JSON 格式正常成功响应也是 application/json,会误判。
262
- if (opts.format === "csv" && /application\/json/i.test(contentType)) {
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
- const parsed = JSON.parse(buf.toString("utf8"));
265
- if (parsed.status_code != null && parsed.status_code !== "0") {
291
+ const parsed = JSON.parse(buf.toString('utf8'));
292
+ if (parsed.status_code != null && parsed.status_code !== '0') {
266
293
  // 复用 extractData 的错误映射逻辑(throw AppError)
267
294
  (0, client_1.extractData)(parsed);
268
295
  }
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
  }
276
303
  }
277
304
  // 后端通过响应头回传记录数(避免污染 body);header 缺失或解析失败 → undefined
278
- const recordCountHeader = response.headers.get("X-Miaoda-Record-Count");
305
+ const recordCountHeader = response.headers.get('X-Miaoda-Record-Count');
279
306
  const parsedCount = recordCountHeader != null ? Number(recordCountHeader) : NaN;
280
307
  const recordCount = Number.isFinite(parsedCount) && parsedCount >= 0 ? parsedCount : undefined;
281
308
  return {
@@ -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
+ }