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