@lark-apaas/miaoda-cli 0.1.3 → 0.1.4
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/app/api.js +3 -3
- package/dist/api/app/schemas.js +43 -43
- package/dist/api/db/api.js +398 -55
- package/dist/api/db/client.js +155 -28
- package/dist/api/db/index.js +12 -1
- package/dist/api/db/parsers.js +20 -20
- package/dist/api/db/sql-keywords.js +87 -87
- package/dist/api/deploy/api.js +5 -5
- package/dist/api/deploy/schemas.js +32 -32
- package/dist/api/file/api.js +89 -87
- package/dist/api/file/client.js +62 -22
- package/dist/api/file/detect.js +3 -3
- package/dist/api/file/index.js +2 -1
- package/dist/api/file/parsers.js +18 -7
- package/dist/api/observability/api.js +6 -6
- package/dist/api/observability/schemas.js +14 -14
- package/dist/api/plugin/api.js +31 -31
- package/dist/cli/commands/app/index.js +12 -12
- package/dist/cli/commands/db/index.js +602 -54
- package/dist/cli/commands/deploy/index.js +28 -28
- package/dist/cli/commands/file/index.js +85 -58
- package/dist/cli/commands/observability/index.js +69 -69
- package/dist/cli/commands/plugin/index.js +27 -27
- package/dist/cli/commands/shared.js +10 -10
- package/dist/cli/handlers/app/update.js +2 -2
- package/dist/cli/handlers/db/_destructive.js +67 -0
- package/dist/cli/handlers/db/_env.js +26 -0
- package/dist/cli/handlers/db/_operator.js +35 -0
- package/dist/cli/handlers/db/audit.js +383 -0
- package/dist/cli/handlers/db/changelog.js +160 -0
- package/dist/cli/handlers/db/data.js +32 -31
- package/dist/cli/handlers/db/index.js +17 -1
- package/dist/cli/handlers/db/migration.js +234 -0
- package/dist/cli/handlers/db/quota.js +68 -0
- package/dist/cli/handlers/db/recovery.js +413 -0
- package/dist/cli/handlers/db/schema.js +33 -33
- package/dist/cli/handlers/db/sql.js +69 -69
- package/dist/cli/handlers/deploy/deploy.js +4 -4
- package/dist/cli/handlers/deploy/error-log.js +1 -1
- package/dist/cli/handlers/deploy/get.js +3 -3
- package/dist/cli/handlers/deploy/polling.js +11 -11
- package/dist/cli/handlers/file/cp.js +30 -30
- package/dist/cli/handlers/file/index.js +3 -1
- package/dist/cli/handlers/file/ls.js +5 -5
- package/dist/cli/handlers/file/quota.js +66 -0
- package/dist/cli/handlers/file/rm.js +32 -30
- package/dist/cli/handlers/file/sign.js +3 -3
- package/dist/cli/handlers/file/stat.js +10 -9
- package/dist/cli/handlers/observability/analytics.js +47 -47
- package/dist/cli/handlers/observability/helpers.js +2 -2
- package/dist/cli/handlers/observability/log.js +9 -9
- package/dist/cli/handlers/observability/metric.js +26 -26
- package/dist/cli/handlers/observability/trace.js +5 -5
- package/dist/cli/handlers/plugin/plugin-local.js +53 -53
- package/dist/cli/handlers/plugin/plugin.js +15 -15
- package/dist/cli/help.js +16 -16
- package/dist/main.js +12 -12
- package/dist/utils/args.js +1 -1
- package/dist/utils/colors.js +2 -2
- package/dist/utils/config.js +2 -2
- package/dist/utils/devops-error.js +9 -9
- package/dist/utils/error.js +2 -2
- package/dist/utils/git.js +4 -4
- package/dist/utils/http.js +19 -19
- package/dist/utils/index.js +3 -1
- package/dist/utils/output.js +67 -45
- package/dist/utils/poll.js +35 -0
- package/dist/utils/render.js +27 -27
- package/dist/utils/spinner.js +46 -0
- package/dist/utils/time.js +47 -42
- package/package.json +1 -1
package/dist/api/db/api.js
CHANGED
|
@@ -4,30 +4,43 @@ 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.getRecoveryStatus = getRecoveryStatus;
|
|
17
|
+
exports.getDbQuota = getDbQuota;
|
|
7
18
|
const http_1 = require("../../utils/http");
|
|
8
19
|
const error_1 = require("../../utils/error");
|
|
9
20
|
const http_client_1 = require("@lark-apaas/http-client");
|
|
10
21
|
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) {
|
|
22
|
+
const DEFAULT_TIMEOUT_HINT = 'The server may still be processing your request. Retry the command if needed.';
|
|
23
|
+
async function mapDbHttpError(err, url, ctx, opts) {
|
|
26
24
|
if (err instanceof error_1.AppError)
|
|
27
25
|
throw err;
|
|
26
|
+
// 客户端超时 / abort:SdkHttpError 时 response 通常缺失(status=0),落到 throw 时
|
|
27
|
+
// message 形如 'Failed to recover: 0' 让用户看不懂。识别 abort / timeout 关键字后
|
|
28
|
+
// 单独抛专用 AppError(带领域可定制的 message + hint),不再包成通用 "Failed to X"。
|
|
29
|
+
if (err instanceof Error) {
|
|
30
|
+
const lower = err.message.toLowerCase();
|
|
31
|
+
if (lower.includes('aborted') ||
|
|
32
|
+
lower.includes('timeout') ||
|
|
33
|
+
err.name === 'AbortError' ||
|
|
34
|
+
err.name === 'TimeoutError') {
|
|
35
|
+
const code = opts?.timeout?.code ?? 'REQUEST_TIMEOUT';
|
|
36
|
+
const message = opts?.timeout?.message ?? 'Request timed out after 30s';
|
|
37
|
+
const hint = opts?.timeout?.hint ?? DEFAULT_TIMEOUT_HINT;
|
|
38
|
+
throw new error_1.AppError(code, message, { next_actions: [hint] });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
28
41
|
if (err instanceof http_client_1.HttpError) {
|
|
29
42
|
const status = err.response?.status ?? 0;
|
|
30
|
-
const statusText = err.response?.statusText ??
|
|
43
|
+
const statusText = err.response?.statusText ?? '';
|
|
31
44
|
try {
|
|
32
45
|
const body = (await err.response?.json());
|
|
33
46
|
if (body) {
|
|
@@ -35,8 +48,13 @@ onErrorBody) {
|
|
|
35
48
|
(0, client_1.extractData)(body); // 业务 code 命中 → 抛 AppError;不命中走兜底
|
|
36
49
|
}
|
|
37
50
|
catch (appErr) {
|
|
38
|
-
if (appErr instanceof error_1.AppError
|
|
39
|
-
|
|
51
|
+
if (appErr instanceof error_1.AppError) {
|
|
52
|
+
// env 上下文存在时把"env not exist"系列错误统一重写成 UNKNOWN_ENV_VALUE
|
|
53
|
+
// —— 用户视角 vocab 错和 state 错都是「这个 env 不能用」,文案统一更好理解。
|
|
54
|
+
const decorated = (0, client_1.decorateEnvError)(appErr, opts?.env);
|
|
55
|
+
if (opts?.onErrorBody)
|
|
56
|
+
opts.onErrorBody(body, decorated);
|
|
57
|
+
throw decorated;
|
|
40
58
|
}
|
|
41
59
|
throw appErr;
|
|
42
60
|
}
|
|
@@ -79,18 +97,29 @@ function attachSqlPartialResults(body, appErr) {
|
|
|
79
97
|
*/
|
|
80
98
|
async function execSql(opts) {
|
|
81
99
|
const client = (0, http_1.getHttpClient)();
|
|
82
|
-
const url = (0, client_1.buildInnerUrl)(opts.appId,
|
|
100
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/sql', {
|
|
83
101
|
dbBranch: opts.dbBranch,
|
|
84
102
|
});
|
|
85
103
|
const start = Date.now();
|
|
86
104
|
let response;
|
|
87
105
|
try {
|
|
88
106
|
response = await client.post(url, { sql: opts.sql });
|
|
89
|
-
(0, client_1.traceHttp)(
|
|
107
|
+
(0, client_1.traceHttp)('POST', url, start, response);
|
|
90
108
|
}
|
|
91
109
|
catch (err) {
|
|
92
|
-
(0, client_1.traceHttp)(
|
|
93
|
-
await mapDbHttpError(err, url,
|
|
110
|
+
(0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
111
|
+
await mapDbHttpError(err, url, 'Failed to execute SQL', {
|
|
112
|
+
env: opts.dbBranch,
|
|
113
|
+
onErrorBody: attachSqlPartialResults,
|
|
114
|
+
// SQL 路径单独的超时文案:PG 的 statement_timeout 会回滚整条事务,所以这里
|
|
115
|
+
// 明示「事务已回滚、没有改动落地」,并把 hint 引导到「简化 SQL / 加 LIMIT / 拆条」。
|
|
116
|
+
timeout: {
|
|
117
|
+
code: 'SQL_EXECUTION_TIMEOUT',
|
|
118
|
+
message: 'SQL execution timed out after 30s',
|
|
119
|
+
hint: 'The transaction was rolled back; no changes were applied. ' +
|
|
120
|
+
'Simplify the SQL, add filters or LIMIT for queries, or split it into smaller statements.',
|
|
121
|
+
},
|
|
122
|
+
});
|
|
94
123
|
throw err; // 不可达
|
|
95
124
|
}
|
|
96
125
|
const body = (await response.json());
|
|
@@ -101,11 +130,11 @@ async function execSql(opts) {
|
|
|
101
130
|
// results 末尾追加一条 SqlType="ERROR" 的哨兵,data 字段是 {code,message} JSON。
|
|
102
131
|
// 这样网关在错误路径不会吞业务字段,CLI 拿到完整 partial_results + statement_index。
|
|
103
132
|
const last = results.at(-1);
|
|
104
|
-
if (last?.sqlType ===
|
|
133
|
+
if (last?.sqlType === 'ERROR') {
|
|
105
134
|
const appErr = parseSqlErrorSentinel(last.data);
|
|
106
135
|
appErr.partial_results = results.slice(0, -1);
|
|
107
136
|
const stmtIdx = data.errorStatementIndex;
|
|
108
|
-
if (typeof stmtIdx ===
|
|
137
|
+
if (typeof stmtIdx === 'number')
|
|
109
138
|
appErr.statement_index = stmtIdx;
|
|
110
139
|
throw appErr;
|
|
111
140
|
}
|
|
@@ -137,12 +166,12 @@ async function execSql(opts) {
|
|
|
137
166
|
function parseSqlErrorSentinel(payload) {
|
|
138
167
|
try {
|
|
139
168
|
const parsed = JSON.parse(payload);
|
|
140
|
-
const code = typeof parsed.code ===
|
|
141
|
-
const message = typeof parsed.message ===
|
|
169
|
+
const code = typeof parsed.code === 'string' && parsed.code !== '' ? parsed.code : 'INTERNAL_DB_ERROR';
|
|
170
|
+
const message = typeof parsed.message === 'string' ? parsed.message : payload;
|
|
142
171
|
return (0, client_1.mapDataloomBizError)(code, message);
|
|
143
172
|
}
|
|
144
173
|
catch {
|
|
145
|
-
return new error_1.AppError(
|
|
174
|
+
return new error_1.AppError('INTERNAL_DB_ERROR', payload);
|
|
146
175
|
}
|
|
147
176
|
}
|
|
148
177
|
// ── db schema → InnerGetSchema ──
|
|
@@ -154,21 +183,21 @@ function parseSqlErrorSentinel(payload) {
|
|
|
154
183
|
*/
|
|
155
184
|
async function getSchema(opts) {
|
|
156
185
|
const client = (0, http_1.getHttpClient)();
|
|
157
|
-
const url = (0, client_1.buildInnerUrl)(opts.appId,
|
|
158
|
-
format: opts.format ??
|
|
186
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/schema', {
|
|
187
|
+
format: opts.format ?? 'schema',
|
|
159
188
|
tableNames: opts.tableNames,
|
|
160
|
-
includeStats: opts.includeStats ?
|
|
189
|
+
includeStats: opts.includeStats ? 'true' : undefined,
|
|
161
190
|
dbBranch: opts.dbBranch,
|
|
162
191
|
});
|
|
163
192
|
const start = Date.now();
|
|
164
193
|
let response;
|
|
165
194
|
try {
|
|
166
195
|
response = await client.get(url);
|
|
167
|
-
(0, client_1.traceHttp)(
|
|
196
|
+
(0, client_1.traceHttp)('GET', url, start, response);
|
|
168
197
|
}
|
|
169
198
|
catch (err) {
|
|
170
|
-
(0, client_1.traceHttp)(
|
|
171
|
-
await mapDbHttpError(err, url,
|
|
199
|
+
(0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
200
|
+
await mapDbHttpError(err, url, 'Failed to get schema', { env: opts.dbBranch });
|
|
172
201
|
throw err; // 不可达
|
|
173
202
|
}
|
|
174
203
|
const body = (await response.json());
|
|
@@ -188,24 +217,25 @@ async function getSchema(opts) {
|
|
|
188
217
|
*/
|
|
189
218
|
async function importData(opts) {
|
|
190
219
|
const client = (0, http_1.getHttpClient)();
|
|
191
|
-
const url = (0, client_1.buildInnerUrl)(opts.appId,
|
|
220
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/data/import');
|
|
192
221
|
const reqBody = {
|
|
193
222
|
tableName: opts.tableName,
|
|
194
223
|
format: opts.format,
|
|
195
|
-
records: opts.body.toString(
|
|
224
|
+
records: opts.body.toString('utf8'),
|
|
196
225
|
};
|
|
197
|
-
if (opts.dbBranch !== undefined && opts.dbBranch !==
|
|
198
|
-
|
|
226
|
+
if (opts.dbBranch !== undefined && opts.dbBranch !== '') {
|
|
227
|
+
// 兼容 `online` 别名 → 后端实际 dbBranch 名为 `main`
|
|
228
|
+
reqBody.dbBranch = opts.dbBranch === 'online' ? 'main' : opts.dbBranch;
|
|
199
229
|
}
|
|
200
230
|
const start = Date.now();
|
|
201
231
|
let response;
|
|
202
232
|
try {
|
|
203
233
|
response = await client.post(url, reqBody);
|
|
204
|
-
(0, client_1.traceHttp)(
|
|
234
|
+
(0, client_1.traceHttp)('POST', url, start, response);
|
|
205
235
|
}
|
|
206
236
|
catch (err) {
|
|
207
|
-
(0, client_1.traceHttp)(
|
|
208
|
-
await mapDbHttpError(err, url,
|
|
237
|
+
(0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
238
|
+
await mapDbHttpError(err, url, 'Failed to import data', { env: opts.dbBranch });
|
|
209
239
|
throw err; // 不可达
|
|
210
240
|
}
|
|
211
241
|
// 后端 InnerAdminImportData 响应里 data 直接返 {tableName, recordCount, durationMs}
|
|
@@ -229,7 +259,7 @@ async function importData(opts) {
|
|
|
229
259
|
*/
|
|
230
260
|
async function exportData(opts) {
|
|
231
261
|
const client = (0, http_1.getHttpClient)();
|
|
232
|
-
const url = (0, client_1.buildInnerUrl)(opts.appId,
|
|
262
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/data/export', {
|
|
233
263
|
tableName: opts.tableName,
|
|
234
264
|
format: opts.format,
|
|
235
265
|
limit: String(opts.limit ?? 5000),
|
|
@@ -239,43 +269,48 @@ async function exportData(opts) {
|
|
|
239
269
|
const start = Date.now();
|
|
240
270
|
let response;
|
|
241
271
|
try {
|
|
242
|
-
response = await client.request({ method:
|
|
243
|
-
(0, client_1.traceHttp)(
|
|
272
|
+
response = await client.request({ method: 'POST', url });
|
|
273
|
+
(0, client_1.traceHttp)('POST', url, start, response);
|
|
244
274
|
}
|
|
245
275
|
catch (err) {
|
|
246
|
-
(0, client_1.traceHttp)(
|
|
247
|
-
await mapDbHttpError(err, url,
|
|
276
|
+
(0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
277
|
+
await mapDbHttpError(err, url, 'Failed to export data', { env: opts.dbBranch });
|
|
248
278
|
throw err; // 不可达
|
|
249
279
|
}
|
|
250
|
-
// 成功路径:响应 body 通常是原始 CSV/JSON 字节,但部分错误场景下网关会返
|
|
280
|
+
// 成功路径:响应 body 通常是原始 CSV/SQL/JSON 字节,但部分错误场景下网关会返
|
|
251
281
|
// HTTP 200 + JSON envelope(status_code != "0"),需要在这里嗅探兜底。
|
|
252
|
-
const
|
|
253
|
-
|
|
282
|
+
const defaultContentType = {
|
|
283
|
+
csv: 'text/csv',
|
|
284
|
+
sql: 'text/plain',
|
|
285
|
+
json: 'application/json',
|
|
286
|
+
};
|
|
287
|
+
const contentType = response.headers.get('Content-Type') ?? defaultContentType[opts.format];
|
|
254
288
|
const ab = await response.arrayBuffer();
|
|
255
289
|
const buf = Buffer.from(new Uint8Array(ab));
|
|
256
290
|
if (buf.length === 0) {
|
|
257
|
-
throw new error_1.AppError(
|
|
291
|
+
throw new error_1.AppError('INTERNAL_DB_ERROR', 'Empty export response body');
|
|
258
292
|
}
|
|
259
293
|
// Envelope sniff:HTTP 200 + Content-Type 为 application/json + body 解析得到
|
|
260
294
|
// InnerEnvelope 且 status_code 非 "0" 时,按业务错误抛出。
|
|
261
|
-
//
|
|
262
|
-
|
|
295
|
+
// CSV / SQL 都是非 JSON 输出,application/json 响应必是错误信封;JSON 格式
|
|
296
|
+
// 成功响应自身就是 application/json,跳过 sniff 避免误判。
|
|
297
|
+
if (opts.format !== 'json' && /application\/json/i.test(contentType)) {
|
|
263
298
|
try {
|
|
264
|
-
const parsed = JSON.parse(buf.toString(
|
|
265
|
-
if (parsed.status_code != null && parsed.status_code !==
|
|
299
|
+
const parsed = JSON.parse(buf.toString('utf8'));
|
|
300
|
+
if (parsed.status_code != null && parsed.status_code !== '0') {
|
|
266
301
|
// 复用 extractData 的错误映射逻辑(throw AppError)
|
|
267
302
|
(0, client_1.extractData)(parsed);
|
|
268
303
|
}
|
|
269
304
|
}
|
|
270
305
|
catch (err) {
|
|
271
306
|
// 已经被 extractData 抛成 AppError → 透传;否则 JSON.parse 失败说明 body
|
|
272
|
-
// 真的是 CSV 文本,继续按成功流程走
|
|
307
|
+
// 真的是 CSV / SQL 文本,继续按成功流程走
|
|
273
308
|
if (err instanceof error_1.AppError)
|
|
274
309
|
throw err;
|
|
275
310
|
}
|
|
276
311
|
}
|
|
277
312
|
// 后端通过响应头回传记录数(避免污染 body);header 缺失或解析失败 → undefined
|
|
278
|
-
const recordCountHeader = response.headers.get(
|
|
313
|
+
const recordCountHeader = response.headers.get('X-Miaoda-Record-Count');
|
|
279
314
|
const parsedCount = recordCountHeader != null ? Number(recordCountHeader) : NaN;
|
|
280
315
|
const recordCount = Number.isFinite(parsedCount) && parsedCount >= 0 ? parsedCount : undefined;
|
|
281
316
|
return {
|
|
@@ -286,3 +321,311 @@ async function exportData(opts) {
|
|
|
286
321
|
recordCount,
|
|
287
322
|
};
|
|
288
323
|
}
|
|
324
|
+
// ── db changelog → InnerAdminListDDLChangelog ──
|
|
325
|
+
/**
|
|
326
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/changelog?table=&since=&until=&limit=&cursor=&dbBranch=
|
|
327
|
+
*
|
|
328
|
+
* 时间字段 since/until 由 CLI 端归一化为 ISO 8601 UTC 后透传;后端按 created_at 比较。
|
|
329
|
+
*/
|
|
330
|
+
async function listDDLChangelog(opts) {
|
|
331
|
+
const client = (0, http_1.getHttpClient)();
|
|
332
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/changelog', {
|
|
333
|
+
table: opts.table,
|
|
334
|
+
since: opts.since,
|
|
335
|
+
until: opts.until,
|
|
336
|
+
changeId: opts.changeId,
|
|
337
|
+
limit: opts.limit !== undefined ? String(opts.limit) : undefined,
|
|
338
|
+
cursor: opts.cursor,
|
|
339
|
+
dbBranch: opts.dbBranch,
|
|
340
|
+
});
|
|
341
|
+
const start = Date.now();
|
|
342
|
+
let response;
|
|
343
|
+
try {
|
|
344
|
+
response = await client.get(url);
|
|
345
|
+
(0, client_1.traceHttp)('GET', url, start, response);
|
|
346
|
+
}
|
|
347
|
+
catch (err) {
|
|
348
|
+
(0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
349
|
+
await mapDbHttpError(err, url, 'Failed to list DDL changelog', { env: opts.dbBranch });
|
|
350
|
+
throw err; // 不可达
|
|
351
|
+
}
|
|
352
|
+
const body = (await response.json());
|
|
353
|
+
const data = (0, client_1.extractData)(body);
|
|
354
|
+
return {
|
|
355
|
+
items: data.items ?? [],
|
|
356
|
+
nextCursor: data.nextCursor && data.nextCursor !== '' ? data.nextCursor : null,
|
|
357
|
+
hasMore: Boolean(data.hasMore),
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
// ── db audit → InnerAdminGetAuditStatus / InnerAdminSetAuditConfig ──
|
|
361
|
+
/**
|
|
362
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/audit/status?table=&dbBranch=
|
|
363
|
+
* 查表审计开关状态。table 非空 → 单表过滤;空 → 返当前 workspace 全部已配置表。
|
|
364
|
+
*/
|
|
365
|
+
async function getAuditStatus(opts) {
|
|
366
|
+
const client = (0, http_1.getHttpClient)();
|
|
367
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/audit/status', {
|
|
368
|
+
table: opts.table,
|
|
369
|
+
dbBranch: opts.dbBranch,
|
|
370
|
+
});
|
|
371
|
+
const start = Date.now();
|
|
372
|
+
let response;
|
|
373
|
+
try {
|
|
374
|
+
response = await client.get(url);
|
|
375
|
+
(0, client_1.traceHttp)('GET', url, start, response);
|
|
376
|
+
}
|
|
377
|
+
catch (err) {
|
|
378
|
+
(0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
379
|
+
await mapDbHttpError(err, url, 'Failed to get audit status', { env: opts.dbBranch });
|
|
380
|
+
throw err; // 不可达
|
|
381
|
+
}
|
|
382
|
+
const respBody = (await response.json());
|
|
383
|
+
const data = (0, client_1.extractData)(respBody);
|
|
384
|
+
return data.items ?? [];
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* 后端:POST /v1/dataloom/app/{appId}/db/audit/config
|
|
388
|
+
* 写:enabled=true 开启 / false 关闭。retention 仅 enabled=true 生效。
|
|
389
|
+
*/
|
|
390
|
+
async function setAuditConfig(opts) {
|
|
391
|
+
const client = (0, http_1.getHttpClient)();
|
|
392
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/audit/config');
|
|
393
|
+
const body = {
|
|
394
|
+
table: opts.table,
|
|
395
|
+
enabled: opts.enabled,
|
|
396
|
+
};
|
|
397
|
+
if (opts.retention !== undefined && opts.retention !== '')
|
|
398
|
+
body.retention = opts.retention;
|
|
399
|
+
if (opts.dbBranch !== undefined && opts.dbBranch !== '') {
|
|
400
|
+
// 兼容 `online` 别名 → 后端实际 dbBranch 名为 `main`
|
|
401
|
+
body.dbBranch = opts.dbBranch === 'online' ? 'main' : opts.dbBranch;
|
|
402
|
+
}
|
|
403
|
+
const start = Date.now();
|
|
404
|
+
let response;
|
|
405
|
+
try {
|
|
406
|
+
response = await client.post(url, body);
|
|
407
|
+
(0, client_1.traceHttp)('POST', url, start, response);
|
|
408
|
+
}
|
|
409
|
+
catch (err) {
|
|
410
|
+
(0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
411
|
+
await mapDbHttpError(err, url, 'Failed to set audit config', { env: opts.dbBranch });
|
|
412
|
+
throw err; // 不可达
|
|
413
|
+
}
|
|
414
|
+
const respBody = (await response.json());
|
|
415
|
+
const data = (0, client_1.extractData)(respBody);
|
|
416
|
+
if (!data.status) {
|
|
417
|
+
throw new error_1.AppError('INTERNAL_DB_ERROR', 'audit config response missing status field');
|
|
418
|
+
}
|
|
419
|
+
return data.status;
|
|
420
|
+
}
|
|
421
|
+
// ── db audit log → InnerAdminListAuditLog ──
|
|
422
|
+
/**
|
|
423
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/audit/log?tables=&since=&until=&limit=&cursor=&dbBranch=
|
|
424
|
+
*
|
|
425
|
+
* 走 admin-inner 接口而不是 InnerAdminExecuteSQL 直接 SELECT pg_audit:
|
|
426
|
+
* - operator 在 details JSONB 内是 user_id,服务端解析成 username
|
|
427
|
+
* - summary 后端按 type + before/after diff 合成(pg_audit 表无此列)
|
|
428
|
+
* - before/after JSONB 后端 JSON.stringify 后透传字符串,CLI 按需 parse
|
|
429
|
+
*
|
|
430
|
+
* 多表用逗号拼接走 query;后端按 target_table IN (...) 一次查。skipped 字段返
|
|
431
|
+
* 多表中无记录的表名,便于 CLI 展示 hint。
|
|
432
|
+
*/
|
|
433
|
+
async function listAuditLog(opts) {
|
|
434
|
+
if (opts.tables.length === 0) {
|
|
435
|
+
throw new error_1.AppError('ARGS_INVALID', 'at least one table is required');
|
|
436
|
+
}
|
|
437
|
+
const client = (0, http_1.getHttpClient)();
|
|
438
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/audit/log', {
|
|
439
|
+
// IDL `Tables list<string>` 走重复 query key(?tables=A&tables=B)—— join(',')
|
|
440
|
+
// 会让 Hertz/Kitex 网关绑成 ["A,B"] 单元素切片,命中单表错误分支报错文案错乱。
|
|
441
|
+
tables: opts.tables,
|
|
442
|
+
since: opts.since,
|
|
443
|
+
until: opts.until,
|
|
444
|
+
limit: opts.limit !== undefined ? String(opts.limit) : undefined,
|
|
445
|
+
cursor: opts.cursor,
|
|
446
|
+
dbBranch: opts.dbBranch,
|
|
447
|
+
});
|
|
448
|
+
const start = Date.now();
|
|
449
|
+
let response;
|
|
450
|
+
try {
|
|
451
|
+
response = await client.get(url);
|
|
452
|
+
(0, client_1.traceHttp)('GET', url, start, response);
|
|
453
|
+
}
|
|
454
|
+
catch (err) {
|
|
455
|
+
(0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
456
|
+
await mapDbHttpError(err, url, 'Failed to list audit log', { env: opts.dbBranch });
|
|
457
|
+
throw err; // 不可达
|
|
458
|
+
}
|
|
459
|
+
const body = (await response.json());
|
|
460
|
+
const data = (0, client_1.extractData)(body);
|
|
461
|
+
return {
|
|
462
|
+
items: data.items ?? [],
|
|
463
|
+
nextCursor: data.nextCursor && data.nextCursor !== '' ? data.nextCursor : null,
|
|
464
|
+
hasMore: Boolean(data.hasMore),
|
|
465
|
+
skipped: data.skipped ?? [],
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
// ── db migration → InnerAdminMigrationInit / InnerAdminMigrate ──
|
|
469
|
+
/**
|
|
470
|
+
* 后端:POST /v1/dataloom/app/{appId}/db/enableMultiEnv
|
|
471
|
+
* 单库 → dev/online 双库初始化,不可逆。对应公开 API EnableMultiEnvDB 的 admin-inner 通道。
|
|
472
|
+
*/
|
|
473
|
+
async function migrationInit(opts) {
|
|
474
|
+
const client = (0, http_1.getHttpClient)();
|
|
475
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/enableMultiEnv');
|
|
476
|
+
const body = {};
|
|
477
|
+
if (opts.syncData !== undefined)
|
|
478
|
+
body.syncData = opts.syncData;
|
|
479
|
+
const start = Date.now();
|
|
480
|
+
let response;
|
|
481
|
+
try {
|
|
482
|
+
response = await client.post(url, body);
|
|
483
|
+
(0, client_1.traceHttp)('POST', url, start, response);
|
|
484
|
+
}
|
|
485
|
+
catch (err) {
|
|
486
|
+
(0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
487
|
+
await mapDbHttpError(err, url, 'Failed to init migration');
|
|
488
|
+
throw err; // 不可达
|
|
489
|
+
}
|
|
490
|
+
const respBody = (await response.json());
|
|
491
|
+
return (0, client_1.extractData)(respBody);
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* 后端:POST /v1/dataloom/app/{appId}/db/migration
|
|
495
|
+
* 合并 diff + apply:dryRun=true 只返 changes 不下发;dryRun=false 才执行。
|
|
496
|
+
*/
|
|
497
|
+
async function migrate(opts) {
|
|
498
|
+
const client = (0, http_1.getHttpClient)();
|
|
499
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/migration');
|
|
500
|
+
const start = Date.now();
|
|
501
|
+
let response;
|
|
502
|
+
try {
|
|
503
|
+
response = await client.post(url, { dryRun: opts.dryRun });
|
|
504
|
+
(0, client_1.traceHttp)('POST', url, start, response);
|
|
505
|
+
}
|
|
506
|
+
catch (err) {
|
|
507
|
+
(0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
508
|
+
await mapDbHttpError(err, url, 'Failed to migrate');
|
|
509
|
+
throw err; // 不可达
|
|
510
|
+
}
|
|
511
|
+
const respBody = (await response.json());
|
|
512
|
+
return (0, client_1.extractData)(respBody);
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/migration/status?taskId=...
|
|
516
|
+
* CLI 拿到 migration apply 的 taskId 后定时调本接口,直到 status=success/failed。
|
|
517
|
+
* 网络层超时仍走 mapDbHttpError → 单次 30s;轮询节奏由 CLI handler 自行控制。
|
|
518
|
+
*/
|
|
519
|
+
async function getMigrationStatus(opts) {
|
|
520
|
+
const client = (0, http_1.getHttpClient)();
|
|
521
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/migration/status', {
|
|
522
|
+
taskId: opts.taskId,
|
|
523
|
+
dbBranch: opts.dbBranch,
|
|
524
|
+
});
|
|
525
|
+
const start = Date.now();
|
|
526
|
+
let response;
|
|
527
|
+
try {
|
|
528
|
+
response = await client.get(url);
|
|
529
|
+
(0, client_1.traceHttp)('GET', url, start, response);
|
|
530
|
+
}
|
|
531
|
+
catch (err) {
|
|
532
|
+
(0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
533
|
+
await mapDbHttpError(err, url, 'Failed to get migration status', { env: opts.dbBranch });
|
|
534
|
+
throw err; // 不可达
|
|
535
|
+
}
|
|
536
|
+
const body = (await response.json());
|
|
537
|
+
return (0, client_1.extractData)(body);
|
|
538
|
+
}
|
|
539
|
+
// ── db recovery → InnerAdminRecover ──
|
|
540
|
+
/**
|
|
541
|
+
* 后端:POST /v1/dataloom/app/{appId}/db/recovery
|
|
542
|
+
* 合并 PITR diff + apply:dryRun=true 预览影响;dryRun=false 触发恢复。
|
|
543
|
+
*/
|
|
544
|
+
async function recover(opts) {
|
|
545
|
+
const client = (0, http_1.getHttpClient)();
|
|
546
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/recovery');
|
|
547
|
+
const start = Date.now();
|
|
548
|
+
let response;
|
|
549
|
+
try {
|
|
550
|
+
response = await client.post(url, {
|
|
551
|
+
target: opts.target,
|
|
552
|
+
dryRun: opts.dryRun,
|
|
553
|
+
});
|
|
554
|
+
(0, client_1.traceHttp)('POST', url, start, response);
|
|
555
|
+
}
|
|
556
|
+
catch (err) {
|
|
557
|
+
(0, client_1.traceHttp)('POST', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
558
|
+
await mapDbHttpError(err, url, 'Failed to recover');
|
|
559
|
+
throw err; // 不可达
|
|
560
|
+
}
|
|
561
|
+
const respBody = (await response.json());
|
|
562
|
+
return (0, client_1.extractData)(respBody);
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/recovery/preview?previewRequestId=...
|
|
566
|
+
* CLI 拿到 recovery diff 的 previewRequestId 后定时调本接口直到 previewStatus=success/failed。
|
|
567
|
+
*/
|
|
568
|
+
async function getRecoveryPreview(opts) {
|
|
569
|
+
const client = (0, http_1.getHttpClient)();
|
|
570
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/recovery/preview', {
|
|
571
|
+
previewRequestId: opts.previewRequestId,
|
|
572
|
+
dbBranch: opts.dbBranch,
|
|
573
|
+
});
|
|
574
|
+
const start = Date.now();
|
|
575
|
+
let response;
|
|
576
|
+
try {
|
|
577
|
+
response = await client.get(url);
|
|
578
|
+
(0, client_1.traceHttp)('GET', url, start, response);
|
|
579
|
+
}
|
|
580
|
+
catch (err) {
|
|
581
|
+
(0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
582
|
+
await mapDbHttpError(err, url, 'Failed to get recovery preview', { env: opts.dbBranch });
|
|
583
|
+
throw err; // 不可达
|
|
584
|
+
}
|
|
585
|
+
const body = (await response.json());
|
|
586
|
+
return (0, client_1.extractData)(body);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/recovery/status
|
|
590
|
+
* CLI apply 触发后定时调本接口直到 status=success/failed。dataloom 内部 Redis
|
|
591
|
+
* 维护 workspace 级 restore 状态,无需传 task id;workspace+dbBranch 维度同时
|
|
592
|
+
* 只允许一个 restore 进行中。
|
|
593
|
+
*/
|
|
594
|
+
async function getRecoveryStatus(opts) {
|
|
595
|
+
const client = (0, http_1.getHttpClient)();
|
|
596
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/recovery/status', { dbBranch: opts.dbBranch });
|
|
597
|
+
const start = Date.now();
|
|
598
|
+
let response;
|
|
599
|
+
try {
|
|
600
|
+
response = await client.get(url);
|
|
601
|
+
(0, client_1.traceHttp)('GET', url, start, response);
|
|
602
|
+
}
|
|
603
|
+
catch (err) {
|
|
604
|
+
(0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
605
|
+
await mapDbHttpError(err, url, 'Failed to get recovery status', { env: opts.dbBranch });
|
|
606
|
+
throw err; // 不可达
|
|
607
|
+
}
|
|
608
|
+
const body = (await response.json());
|
|
609
|
+
return (0, client_1.extractData)(body);
|
|
610
|
+
}
|
|
611
|
+
// ── db quota → InnerAdminGetDbQuota ──
|
|
612
|
+
/**
|
|
613
|
+
* 后端:GET /v1/dataloom/app/{appId}/db/quota?dbBranch=
|
|
614
|
+
*/
|
|
615
|
+
async function getDbQuota(opts) {
|
|
616
|
+
const client = (0, http_1.getHttpClient)();
|
|
617
|
+
const url = (0, client_1.buildInnerUrl)(opts.appId, '/db/quota', { dbBranch: opts.dbBranch });
|
|
618
|
+
const start = Date.now();
|
|
619
|
+
let response;
|
|
620
|
+
try {
|
|
621
|
+
response = await client.get(url);
|
|
622
|
+
(0, client_1.traceHttp)('GET', url, start, response);
|
|
623
|
+
}
|
|
624
|
+
catch (err) {
|
|
625
|
+
(0, client_1.traceHttp)('GET', url, start, err instanceof http_client_1.HttpError ? err.response : undefined, err);
|
|
626
|
+
await mapDbHttpError(err, url, 'Failed to get db quota', { env: opts.dbBranch });
|
|
627
|
+
throw err; // 不可达
|
|
628
|
+
}
|
|
629
|
+
const respBody = (await response.json());
|
|
630
|
+
return (0, client_1.extractData)(respBody);
|
|
631
|
+
}
|