@lark-apaas/miaoda-cli 0.1.1 → 0.1.2-alpha.746290f
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 +264 -12
- package/dist/api/db/client.js +76 -29
- package/dist/api/db/index.js +7 -1
- package/dist/api/db/parsers.js +33 -20
- package/dist/api/db/sql-keywords.js +123 -0
- package/dist/api/file/api.js +93 -24
- package/dist/api/file/client.js +1 -5
- package/dist/api/file/index.js +2 -1
- package/dist/api/file/parsers.js +1 -5
- package/dist/api/plugin/api.js +8 -3
- package/dist/cli/commands/db/index.js +138 -0
- package/dist/cli/commands/file/index.js +7 -0
- package/dist/cli/commands/plugin/index.js +18 -6
- package/dist/cli/commands/shared.js +1 -3
- package/dist/cli/handlers/db/audit.js +316 -0
- package/dist/cli/handlers/db/changelog.js +117 -0
- package/dist/cli/handlers/db/data.js +23 -3
- package/dist/cli/handlers/db/index.js +17 -1
- package/dist/cli/handlers/db/migration.js +145 -0
- package/dist/cli/handlers/db/quota.js +58 -0
- package/dist/cli/handlers/db/recovery.js +188 -0
- package/dist/cli/handlers/db/schema.js +22 -8
- package/dist/cli/handlers/db/sql.js +304 -16
- package/dist/cli/handlers/file/cp.js +39 -17
- package/dist/cli/handlers/file/index.js +3 -1
- package/dist/cli/handlers/file/ls.js +1 -3
- package/dist/cli/handlers/file/quota.js +57 -0
- package/dist/cli/handlers/file/rm.js +4 -3
- package/dist/cli/handlers/plugin/plugin-local.js +23 -9
- package/dist/cli/handlers/plugin/plugin.js +21 -7
- package/dist/cli/help.js +5 -2
- package/dist/utils/colors.js +98 -0
- package/dist/utils/error.js +11 -0
- package/dist/utils/fuzzy-match.js +91 -0
- package/dist/utils/output.js +81 -5
- package/dist/utils/render.js +61 -41
- package/package.json +10 -2
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.handleDbAuditStatus = handleDbAuditStatus;
|
|
37
|
+
exports.handleDbAuditEnable = handleDbAuditEnable;
|
|
38
|
+
exports.handleDbAuditDisable = handleDbAuditDisable;
|
|
39
|
+
exports.handleDbAuditList = handleDbAuditList;
|
|
40
|
+
const api = __importStar(require("../../../api/index"));
|
|
41
|
+
const index_1 = require("../../../api/file/index");
|
|
42
|
+
const shared_1 = require("../../../cli/commands/shared");
|
|
43
|
+
const error_1 = require("../../../utils/error");
|
|
44
|
+
const output_1 = require("../../../utils/output");
|
|
45
|
+
const render_1 = require("../../../utils/render");
|
|
46
|
+
const index_2 = require("../../../api/db/index");
|
|
47
|
+
const VALID_RETENTION = new Set(["7d", "30d", "180d", "360d", "forever"]);
|
|
48
|
+
async function handleDbAuditStatus(table, opts) {
|
|
49
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
50
|
+
const sql = table !== undefined && table !== ""
|
|
51
|
+
? `SELECT "table", enabled, enabled_at, retention FROM _dl_audit_config WHERE "table" = '${escapeSqlLiteral(table)}'`
|
|
52
|
+
: `SELECT "table", enabled, enabled_at, retention FROM _dl_audit_config ORDER BY "table"`;
|
|
53
|
+
const results = await api.db.execSql({ appId, sql, dbBranch: opts.env });
|
|
54
|
+
const rows = collectSelectRows(results);
|
|
55
|
+
// 单表查询且 _dl_audit_config 没记录 → enabled=false 占位
|
|
56
|
+
if (table !== undefined && table !== "" && rows.length === 0) {
|
|
57
|
+
rows.push({ table, enabled: false });
|
|
58
|
+
}
|
|
59
|
+
const items = rows.map(toAuditStatus);
|
|
60
|
+
// PRD JSON:单表返 object,多表返 array
|
|
61
|
+
if ((0, output_1.isJsonMode)()) {
|
|
62
|
+
if (table !== undefined && items.length === 1) {
|
|
63
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(items[0]));
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(items));
|
|
67
|
+
}
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (items.length === 0) {
|
|
71
|
+
(0, output_1.emit)("No audit configuration found.");
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
75
|
+
// 单表 → key:value 形态(PRD 示例)
|
|
76
|
+
if (table !== undefined && items.length === 1) {
|
|
77
|
+
const it = items[0];
|
|
78
|
+
(0, output_1.emit)((0, render_1.renderKeyValue)([
|
|
79
|
+
["Table", it.table],
|
|
80
|
+
["Enabled", boolToYesNo(it.enabled)],
|
|
81
|
+
["Enabled at", it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) : "—"],
|
|
82
|
+
["Retention", it.retention ?? "—"],
|
|
83
|
+
], tty));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
// 列表 → table 形态
|
|
87
|
+
const headers = ["table", "enabled", "enabled_at", "retention"];
|
|
88
|
+
const out = items.map((it) => [
|
|
89
|
+
it.table,
|
|
90
|
+
boolToYesNo(it.enabled),
|
|
91
|
+
it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) : "—",
|
|
92
|
+
it.retention ?? "—",
|
|
93
|
+
]);
|
|
94
|
+
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, out) : (0, render_1.renderTsv)(headers, out));
|
|
95
|
+
}
|
|
96
|
+
function toAuditStatus(row) {
|
|
97
|
+
// retention 列存的是后端 ValidDay (int64 天数),按 sentinel 反向映射成 PRD 字符串
|
|
98
|
+
const rawRetention = row.retention;
|
|
99
|
+
let retention = null;
|
|
100
|
+
if (rawRetention != null) {
|
|
101
|
+
if (typeof rawRetention === "number") {
|
|
102
|
+
retention = retentionDaysToString(rawRetention);
|
|
103
|
+
}
|
|
104
|
+
else if (typeof rawRetention === "string") {
|
|
105
|
+
// 兼容存的就是字符串形态("7d" / "forever")
|
|
106
|
+
retention = rawRetention;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
table: asString(row.table),
|
|
111
|
+
enabled: Boolean(row.enabled),
|
|
112
|
+
enabled_at: row.enabled_at == null ? null : asString(row.enabled_at),
|
|
113
|
+
retention,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
async function handleDbAuditEnable(table, opts) {
|
|
117
|
+
if (!table) {
|
|
118
|
+
throw new error_1.AppError("ARGS_INVALID", "table name is required", {
|
|
119
|
+
next_actions: [
|
|
120
|
+
"Usage: miaoda db audit enable <table> [--retention 7d|30d|180d|360d|forever]",
|
|
121
|
+
],
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
const retention = opts.retention ?? "7d";
|
|
125
|
+
if (!VALID_RETENTION.has(retention)) {
|
|
126
|
+
throw new error_1.AppError("INVALID_RETENTION", `Invalid retention '${retention}'`, {
|
|
127
|
+
next_actions: ["Allowed values: 7d, 30d, 180d, 360d, forever."],
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
131
|
+
const status = await api.db.setAuditConfig({
|
|
132
|
+
appId,
|
|
133
|
+
table,
|
|
134
|
+
enabled: true,
|
|
135
|
+
retention,
|
|
136
|
+
dbBranch: opts.env,
|
|
137
|
+
});
|
|
138
|
+
// PRD JSON:{"data": {"table": "...", "enabled": true, "retention": "..."}}
|
|
139
|
+
if ((0, output_1.isJsonMode)()) {
|
|
140
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
141
|
+
table: status.table,
|
|
142
|
+
enabled: status.enabled,
|
|
143
|
+
retention: status.retention ?? retention,
|
|
144
|
+
}));
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
148
|
+
const prefix = tty ? "✓" : "OK";
|
|
149
|
+
(0, output_1.emit)(`${prefix} Audit enabled for table '${status.table}' (retention: ${status.retention ?? retention})`);
|
|
150
|
+
}
|
|
151
|
+
async function handleDbAuditDisable(table, opts) {
|
|
152
|
+
if (!table) {
|
|
153
|
+
throw new error_1.AppError("ARGS_INVALID", "table name is required", {
|
|
154
|
+
next_actions: ["Usage: miaoda db audit disable <table>"],
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
158
|
+
const status = await api.db.setAuditConfig({
|
|
159
|
+
appId,
|
|
160
|
+
table,
|
|
161
|
+
enabled: false,
|
|
162
|
+
dbBranch: opts.env,
|
|
163
|
+
});
|
|
164
|
+
if ((0, output_1.isJsonMode)()) {
|
|
165
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
166
|
+
table: status.table,
|
|
167
|
+
enabled: status.enabled,
|
|
168
|
+
}));
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
172
|
+
const prefix = tty ? "✓" : "OK";
|
|
173
|
+
(0, output_1.emit)(`${prefix} Audit disabled for table '${status.table}'`);
|
|
174
|
+
}
|
|
175
|
+
async function handleDbAuditList(tables, opts) {
|
|
176
|
+
if (tables.length === 0) {
|
|
177
|
+
throw new error_1.AppError("ARGS_INVALID", "at least one table is required", {
|
|
178
|
+
next_actions: [
|
|
179
|
+
"Usage: miaoda db audit list <table> [<table>...] [--since ...] [--until ...]",
|
|
180
|
+
],
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
184
|
+
const limit = opts.limit ?? 20;
|
|
185
|
+
// since 优先级:opts.cursor(前一页末条 event_time)> opts.since
|
|
186
|
+
const sinceRaw = opts.cursor ?? opts.since;
|
|
187
|
+
const since = normalizeTime(sinceRaw, "--since/--cursor");
|
|
188
|
+
const until = normalizeTime(opts.until, "--until");
|
|
189
|
+
const inList = tables.map((t) => `'${escapeSqlLiteral(t)}'`).join(",");
|
|
190
|
+
const whereParts = [`target_table IN (${inList})`];
|
|
191
|
+
if (since !== undefined)
|
|
192
|
+
whereParts.push(`event_time >= '${since}'`);
|
|
193
|
+
if (until !== undefined)
|
|
194
|
+
whereParts.push(`event_time <= '${until}'`);
|
|
195
|
+
const sql = `SELECT event_id, event_time, target_table, type, operator, summary, details ` +
|
|
196
|
+
`FROM _dl_audit_log WHERE ${whereParts.join(" AND ")} ORDER BY event_time DESC LIMIT ${String(limit + 1)}`;
|
|
197
|
+
const results = await api.db.execSql({ appId, sql, dbBranch: opts.env });
|
|
198
|
+
const allRows = collectSelectRows(results);
|
|
199
|
+
const hasMore = allRows.length > limit;
|
|
200
|
+
const visible = hasMore ? allRows.slice(0, limit) : allRows;
|
|
201
|
+
const nextCursor = hasMore && visible.length > 0 ? asString(visible[visible.length - 1].event_time) : null;
|
|
202
|
+
// 多表混合时统计哪些表无记录
|
|
203
|
+
const seen = new Set();
|
|
204
|
+
for (const r of visible)
|
|
205
|
+
seen.add(asString(r.target_table));
|
|
206
|
+
const skipped = tables.filter((t) => !seen.has(t));
|
|
207
|
+
// PRD JSON:data 是数组,元素是事件对象(snake_case),分页信封 next_cursor / has_more
|
|
208
|
+
if ((0, output_1.isJsonMode)()) {
|
|
209
|
+
const items = visible.map((r) => ({
|
|
210
|
+
event_id: asString(r.event_id),
|
|
211
|
+
event_time: asString(r.event_time),
|
|
212
|
+
target_table: asString(r.target_table),
|
|
213
|
+
type: asString(r.type),
|
|
214
|
+
operator: asString(r.operator),
|
|
215
|
+
summary: asString(r.summary),
|
|
216
|
+
details: r.details, // 已经是结构化 JSON,透传
|
|
217
|
+
}));
|
|
218
|
+
(0, output_1.emitPaged)(items, nextCursor, hasMore);
|
|
219
|
+
if (skipped.length > 0) {
|
|
220
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(", ")})\n`);
|
|
221
|
+
}
|
|
222
|
+
if (visible.length === 0)
|
|
223
|
+
process.exitCode = 1;
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (visible.length === 0) {
|
|
227
|
+
(0, output_1.emit)("No audit log entries found.");
|
|
228
|
+
if (skipped.length > 0) {
|
|
229
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(", ")})\n`);
|
|
230
|
+
}
|
|
231
|
+
process.exitCode = 1;
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
235
|
+
// PRD:多表渲染时第一列是 target_table,单表时去掉这一列
|
|
236
|
+
const isMultiTable = tables.length > 1;
|
|
237
|
+
const headers = isMultiTable
|
|
238
|
+
? ["target_table", "event_time", "type", "event_id", "operator", "summary"]
|
|
239
|
+
: ["event_time", "type", "event_id", "operator", "summary"];
|
|
240
|
+
const rows = visible.map((it) => {
|
|
241
|
+
const eventTime = (0, render_1.formatTime)(asString(it.event_time), tty);
|
|
242
|
+
const eventId = truncateId(asString(it.event_id));
|
|
243
|
+
const cells = [
|
|
244
|
+
eventTime,
|
|
245
|
+
asString(it.type),
|
|
246
|
+
eventId,
|
|
247
|
+
asString(it.operator, "—"),
|
|
248
|
+
asString(it.summary),
|
|
249
|
+
];
|
|
250
|
+
return isMultiTable ? [asString(it.target_table), ...cells] : cells;
|
|
251
|
+
});
|
|
252
|
+
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, rows) : (0, render_1.renderTsv)(headers, rows));
|
|
253
|
+
if (skipped.length > 0) {
|
|
254
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(", ")})\n`);
|
|
255
|
+
}
|
|
256
|
+
if (hasMore && nextCursor) {
|
|
257
|
+
process.stderr.write(`(more results; use --cursor '${nextCursor}')\n`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
// ── helpers ──
|
|
261
|
+
function normalizeTime(input, flagName) {
|
|
262
|
+
if (input === undefined || input === "")
|
|
263
|
+
return undefined;
|
|
264
|
+
// ISO 8601 直接透传,避免 parseTimeFilterMs 在严格 ISO 上重做一次损精度
|
|
265
|
+
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})$/.test(input)) {
|
|
266
|
+
return new Date(input).toISOString();
|
|
267
|
+
}
|
|
268
|
+
const ms = (0, index_1.parseTimeFilterMs)(input, flagName);
|
|
269
|
+
return new Date(ms).toISOString();
|
|
270
|
+
}
|
|
271
|
+
function collectSelectRows(results) {
|
|
272
|
+
const out = [];
|
|
273
|
+
for (const r of results) {
|
|
274
|
+
const parsed = (0, index_2.parseSqlResult)(r);
|
|
275
|
+
if (parsed.kind === "select")
|
|
276
|
+
out.push(...parsed.rows);
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
// SQL 字面量转义。表名 / 时间戳由调用方控制范围(白名单或严格 ISO 8601 正则)。
|
|
281
|
+
function escapeSqlLiteral(s) {
|
|
282
|
+
return s.replace(/'/g, "''");
|
|
283
|
+
}
|
|
284
|
+
// PRD 输出 enabled 列用 yes/no 而非 true/false
|
|
285
|
+
function boolToYesNo(b) {
|
|
286
|
+
return b ? "yes" : "no";
|
|
287
|
+
}
|
|
288
|
+
// 后端 ValidDay (int64 天数) → PRD retention 字符串。-1 = 永久(前端 / 后端约定)
|
|
289
|
+
function retentionDaysToString(days) {
|
|
290
|
+
if (days === -1)
|
|
291
|
+
return "forever";
|
|
292
|
+
if (days === 7)
|
|
293
|
+
return "7d";
|
|
294
|
+
if (days === 30)
|
|
295
|
+
return "30d";
|
|
296
|
+
if (days === 180)
|
|
297
|
+
return "180d";
|
|
298
|
+
if (days === 360)
|
|
299
|
+
return "360d";
|
|
300
|
+
return `${String(days)}d`;
|
|
301
|
+
}
|
|
302
|
+
function asString(v, fallback = "") {
|
|
303
|
+
if (v == null)
|
|
304
|
+
return fallback;
|
|
305
|
+
if (typeof v === "string")
|
|
306
|
+
return v;
|
|
307
|
+
if (typeof v === "number" || typeof v === "boolean" || typeof v === "bigint")
|
|
308
|
+
return String(v);
|
|
309
|
+
return JSON.stringify(v);
|
|
310
|
+
}
|
|
311
|
+
// audit list 的 event_id 在 TTY 视图里截断展示(PRD 示例 "01525416B44F...")
|
|
312
|
+
function truncateId(id) {
|
|
313
|
+
if (id.length <= 12)
|
|
314
|
+
return id;
|
|
315
|
+
return id.slice(0, 12) + "...";
|
|
316
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.handleDbChangelog = handleDbChangelog;
|
|
37
|
+
const api = __importStar(require("../../../api/index"));
|
|
38
|
+
const index_1 = require("../../../api/file/index");
|
|
39
|
+
const shared_1 = require("../../../cli/commands/shared");
|
|
40
|
+
const output_1 = require("../../../utils/output");
|
|
41
|
+
const render_1 = require("../../../utils/render");
|
|
42
|
+
function toRow(it) {
|
|
43
|
+
return {
|
|
44
|
+
change_id: it.changeId,
|
|
45
|
+
changed_at: it.changedAt,
|
|
46
|
+
operator: it.operator,
|
|
47
|
+
target_table: it.targetTable,
|
|
48
|
+
change_type: it.changeType,
|
|
49
|
+
summary: it.summary,
|
|
50
|
+
statement: it.statement,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** 把用户传入的 since/until 归一成 ISO 8601 UTC 字符串;空串返 undefined。 */
|
|
54
|
+
function normalizeTime(input, flagName) {
|
|
55
|
+
if (input === undefined || input === "")
|
|
56
|
+
return undefined;
|
|
57
|
+
const ms = (0, index_1.parseTimeFilterMs)(input, flagName);
|
|
58
|
+
return new Date(ms).toISOString();
|
|
59
|
+
}
|
|
60
|
+
async function handleDbChangelog(opts) {
|
|
61
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
62
|
+
const since = normalizeTime(opts.since, "--since");
|
|
63
|
+
const until = normalizeTime(opts.until, "--until");
|
|
64
|
+
const allItems = [];
|
|
65
|
+
let cursor = opts.cursor;
|
|
66
|
+
let lastCursor = null;
|
|
67
|
+
let lastHasMore = false;
|
|
68
|
+
// --all:循环到 hasMore=false;否则只拉一页
|
|
69
|
+
// 没传 --all 时翻页由调用方自己用 --cursor 串
|
|
70
|
+
for (;;) {
|
|
71
|
+
const page = await api.db.listDDLChangelog({
|
|
72
|
+
appId,
|
|
73
|
+
table: opts.table,
|
|
74
|
+
since,
|
|
75
|
+
until,
|
|
76
|
+
limit: opts.limit,
|
|
77
|
+
cursor,
|
|
78
|
+
dbBranch: opts.env,
|
|
79
|
+
});
|
|
80
|
+
allItems.push(...page.items);
|
|
81
|
+
lastCursor = page.nextCursor;
|
|
82
|
+
lastHasMore = page.hasMore;
|
|
83
|
+
if (!opts.all || !page.hasMore || !page.nextCursor)
|
|
84
|
+
break;
|
|
85
|
+
cursor = page.nextCursor;
|
|
86
|
+
}
|
|
87
|
+
if ((0, output_1.isJsonMode)()) {
|
|
88
|
+
const rows = allItems.map(toRow);
|
|
89
|
+
// --all 时已经把所有页合一起返,has_more=false / next_cursor=null
|
|
90
|
+
if (opts.all) {
|
|
91
|
+
(0, output_1.emitPaged)((0, output_1.snakeCaseKeys)(rows), null, false);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
(0, output_1.emitPaged)((0, output_1.snakeCaseKeys)(rows), lastCursor, lastHasMore);
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (allItems.length === 0) {
|
|
99
|
+
(0, output_1.emit)("No DDL changes found.");
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
103
|
+
// PRD: change_id / changed_at / operator / target_table / change_type / summary
|
|
104
|
+
const headers = ["change_id", "changed_at", "operator", "target_table", "change_type", "summary"];
|
|
105
|
+
const rows = allItems.map((it) => [
|
|
106
|
+
it.changeId,
|
|
107
|
+
(0, render_1.formatTime)(it.changedAt, tty),
|
|
108
|
+
it.operator || "—",
|
|
109
|
+
it.targetTable || "—",
|
|
110
|
+
it.changeType,
|
|
111
|
+
it.summary || "—",
|
|
112
|
+
]);
|
|
113
|
+
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, rows) : (0, render_1.renderTsv)(headers, rows));
|
|
114
|
+
if (!opts.all && lastHasMore && lastCursor) {
|
|
115
|
+
process.stderr.write(`(more results; use --cursor ${lastCursor} or --all)\n`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -42,7 +42,7 @@ const error_1 = require("../../../utils/error");
|
|
|
42
42
|
const output_1 = require("../../../utils/output");
|
|
43
43
|
const shared_1 = require("../../../cli/commands/shared");
|
|
44
44
|
const render_1 = require("../../../utils/render");
|
|
45
|
-
//
|
|
45
|
+
// import / export 体积上限
|
|
46
46
|
const MAX_SIZE_BYTES = 1 * 1024 * 1024; // 1 MB
|
|
47
47
|
const MAX_ROWS = 5000;
|
|
48
48
|
async function handleDbDataImport(file, opts) {
|
|
@@ -56,7 +56,9 @@ async function handleDbDataImport(file, opts) {
|
|
|
56
56
|
catch (err) {
|
|
57
57
|
const code = err.code;
|
|
58
58
|
if (code === "ENOENT") {
|
|
59
|
-
throw new error_1.AppError("IMPORT_FILE_NOT_FOUND", `Local file '${file}' does not exist`, {
|
|
59
|
+
throw new error_1.AppError("IMPORT_FILE_NOT_FOUND", `Local file '${file}' does not exist`, {
|
|
60
|
+
next_actions: ["Check the file path."],
|
|
61
|
+
});
|
|
60
62
|
}
|
|
61
63
|
throw err;
|
|
62
64
|
}
|
|
@@ -100,6 +102,20 @@ async function handleDbDataExport(table, opts) {
|
|
|
100
102
|
if (!Number.isInteger(limit) || limit <= 0 || limit > MAX_ROWS) {
|
|
101
103
|
throw new error_1.AppError("ARGS_INVALID", `--limit must be a positive integer ≤ ${String(MAX_ROWS)}`);
|
|
102
104
|
}
|
|
105
|
+
if (!opts.force) {
|
|
106
|
+
try {
|
|
107
|
+
await fs.access(outputPath);
|
|
108
|
+
throw new error_1.AppError("FILE_ALREADY_EXISTS", `Output file '${outputPath}' already exists`, {
|
|
109
|
+
next_actions: ["Use -f to specify a different path, or --force to overwrite."],
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
if (err instanceof error_1.AppError)
|
|
114
|
+
throw err;
|
|
115
|
+
if (err.code !== "ENOENT")
|
|
116
|
+
throw err;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
103
119
|
const result = await api.db.exportData({
|
|
104
120
|
appId,
|
|
105
121
|
tableName: table,
|
|
@@ -107,7 +123,11 @@ async function handleDbDataExport(table, opts) {
|
|
|
107
123
|
limit,
|
|
108
124
|
});
|
|
109
125
|
if (result.body.length > MAX_SIZE_BYTES) {
|
|
110
|
-
throw new error_1.AppError("EXPORT_SIZE_EXCEEDED", `Export exceeds 1 MB limit (body is ${String(result.body.length)} bytes)`, {
|
|
126
|
+
throw new error_1.AppError("EXPORT_SIZE_EXCEEDED", `Export exceeds 1 MB limit (body is ${String(result.body.length)} bytes)`, {
|
|
127
|
+
next_actions: [
|
|
128
|
+
`Filter the table with "miaoda db sql" (e.g. WHERE/LIMIT) and export smaller subsets.`,
|
|
129
|
+
],
|
|
130
|
+
});
|
|
111
131
|
}
|
|
112
132
|
await fs.writeFile(outputPath, result.body);
|
|
113
133
|
// 优先信任后端 X-Miaoda-Record-Count header;header 缺失时再用 body 行数兜底
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handleDbDataExport = exports.handleDbDataImport = exports.handleDbSchemaGet = exports.handleDbSchemaList = exports.handleDbSql = void 0;
|
|
3
|
+
exports.handleDbQuota = exports.handleDbRecoveryApply = exports.handleDbRecoveryDiff = exports.handleDbMigrationApply = exports.handleDbMigrationDiff = exports.handleDbMigrationInit = exports.handleDbAuditList = exports.handleDbAuditDisable = exports.handleDbAuditEnable = exports.handleDbAuditStatus = exports.handleDbChangelog = exports.handleDbDataExport = exports.handleDbDataImport = exports.handleDbSchemaGet = exports.handleDbSchemaList = exports.handleDbSql = void 0;
|
|
4
4
|
var sql_1 = require("./sql");
|
|
5
5
|
Object.defineProperty(exports, "handleDbSql", { enumerable: true, get: function () { return sql_1.handleDbSql; } });
|
|
6
6
|
var schema_1 = require("./schema");
|
|
@@ -9,3 +9,19 @@ Object.defineProperty(exports, "handleDbSchemaGet", { enumerable: true, get: fun
|
|
|
9
9
|
var data_1 = require("./data");
|
|
10
10
|
Object.defineProperty(exports, "handleDbDataImport", { enumerable: true, get: function () { return data_1.handleDbDataImport; } });
|
|
11
11
|
Object.defineProperty(exports, "handleDbDataExport", { enumerable: true, get: function () { return data_1.handleDbDataExport; } });
|
|
12
|
+
var changelog_1 = require("./changelog");
|
|
13
|
+
Object.defineProperty(exports, "handleDbChangelog", { enumerable: true, get: function () { return changelog_1.handleDbChangelog; } });
|
|
14
|
+
var audit_1 = require("./audit");
|
|
15
|
+
Object.defineProperty(exports, "handleDbAuditStatus", { enumerable: true, get: function () { return audit_1.handleDbAuditStatus; } });
|
|
16
|
+
Object.defineProperty(exports, "handleDbAuditEnable", { enumerable: true, get: function () { return audit_1.handleDbAuditEnable; } });
|
|
17
|
+
Object.defineProperty(exports, "handleDbAuditDisable", { enumerable: true, get: function () { return audit_1.handleDbAuditDisable; } });
|
|
18
|
+
Object.defineProperty(exports, "handleDbAuditList", { enumerable: true, get: function () { return audit_1.handleDbAuditList; } });
|
|
19
|
+
var migration_1 = require("./migration");
|
|
20
|
+
Object.defineProperty(exports, "handleDbMigrationInit", { enumerable: true, get: function () { return migration_1.handleDbMigrationInit; } });
|
|
21
|
+
Object.defineProperty(exports, "handleDbMigrationDiff", { enumerable: true, get: function () { return migration_1.handleDbMigrationDiff; } });
|
|
22
|
+
Object.defineProperty(exports, "handleDbMigrationApply", { enumerable: true, get: function () { return migration_1.handleDbMigrationApply; } });
|
|
23
|
+
var recovery_1 = require("./recovery");
|
|
24
|
+
Object.defineProperty(exports, "handleDbRecoveryDiff", { enumerable: true, get: function () { return recovery_1.handleDbRecoveryDiff; } });
|
|
25
|
+
Object.defineProperty(exports, "handleDbRecoveryApply", { enumerable: true, get: function () { return recovery_1.handleDbRecoveryApply; } });
|
|
26
|
+
var quota_1 = require("./quota");
|
|
27
|
+
Object.defineProperty(exports, "handleDbQuota", { enumerable: true, get: function () { return quota_1.handleDbQuota; } });
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.handleDbMigrationInit = handleDbMigrationInit;
|
|
40
|
+
exports.handleDbMigrationDiff = handleDbMigrationDiff;
|
|
41
|
+
exports.handleDbMigrationApply = handleDbMigrationApply;
|
|
42
|
+
const node_readline_1 = __importDefault(require("node:readline"));
|
|
43
|
+
const api = __importStar(require("../../../api/index"));
|
|
44
|
+
const shared_1 = require("../../../cli/commands/shared");
|
|
45
|
+
const output_1 = require("../../../utils/output");
|
|
46
|
+
const render_1 = require("../../../utils/render");
|
|
47
|
+
async function handleDbMigrationInit(opts) {
|
|
48
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
49
|
+
// 不可逆操作,TTY 默认要求 y/N;--yes 跳过
|
|
50
|
+
if (!opts.yes && !(0, output_1.isJsonMode)()) {
|
|
51
|
+
const ok = await confirm(`? Initialize multi-env (single → dev/online), this is IRREVERSIBLE${opts.syncData ? " and will copy existing data to dev" : ""}? (y/N) `);
|
|
52
|
+
if (!ok) {
|
|
53
|
+
(0, output_1.emit)("Aborted.");
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const result = await api.db.migrationInit({ appId, syncData: opts.syncData });
|
|
58
|
+
if ((0, output_1.isJsonMode)()) {
|
|
59
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(result));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
63
|
+
(0, output_1.emit)((0, render_1.renderKeyValue)([
|
|
64
|
+
["Status", result.status],
|
|
65
|
+
["Environments", result.environments.join(", ")],
|
|
66
|
+
["Data synced", String(result.dataSynced)],
|
|
67
|
+
], tty));
|
|
68
|
+
}
|
|
69
|
+
async function handleDbMigrationDiff(opts) {
|
|
70
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
71
|
+
const result = await api.db.migrate({ appId, dryRun: true });
|
|
72
|
+
renderDiff(result);
|
|
73
|
+
}
|
|
74
|
+
async function handleDbMigrationApply(opts) {
|
|
75
|
+
const appId = (0, shared_1.resolveAppId)(opts);
|
|
76
|
+
// TTY 下先 diff 给用户审;--yes 直接打到 online
|
|
77
|
+
if (!opts.yes && !(0, output_1.isJsonMode)()) {
|
|
78
|
+
const preview = await api.db.migrate({ appId, dryRun: true });
|
|
79
|
+
if (preview.changes.length === 0) {
|
|
80
|
+
(0, output_1.emit)(`Error: No pending changes between ${preview.from} and ${preview.to}`);
|
|
81
|
+
(0, output_1.emit)(` hint: Make schema changes in dev first.`);
|
|
82
|
+
process.exitCode = 1;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
renderDiff(preview);
|
|
86
|
+
const ok = await confirm(`? Apply ${String(preview.changes.length)} change(s) to ${preview.to}? (y/N) `);
|
|
87
|
+
if (!ok) {
|
|
88
|
+
(0, output_1.emit)("Aborted.");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const result = await api.db.migrate({ appId, dryRun: false });
|
|
93
|
+
if ((0, output_1.isJsonMode)()) {
|
|
94
|
+
// PRD:{"status": "applied", "from": "dev", "to": "online", "changes_applied": 2}
|
|
95
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
96
|
+
status: result.status ?? "applied",
|
|
97
|
+
from: result.from,
|
|
98
|
+
to: result.to,
|
|
99
|
+
changesApplied: result.changesApplied ?? result.changes.length,
|
|
100
|
+
}));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
104
|
+
const prefix = tty ? "✓" : "OK";
|
|
105
|
+
const arrow = tty ? "→" : "->";
|
|
106
|
+
const applied = result.changesApplied ?? result.changes.length;
|
|
107
|
+
(0, output_1.emit)(`${prefix} Applied ${result.from} ${arrow} ${result.to} (${String(applied)} changes)`);
|
|
108
|
+
}
|
|
109
|
+
// ── helpers ──
|
|
110
|
+
// PRD diff 输出:
|
|
111
|
+
// dev → online (2 changes):
|
|
112
|
+
//
|
|
113
|
+
// ALTER TABLE users ADD COLUMN avatar_url text;
|
|
114
|
+
// CREATE INDEX idx_users_avatar ON users(avatar_url);
|
|
115
|
+
function renderDiff(result) {
|
|
116
|
+
if ((0, output_1.isJsonMode)()) {
|
|
117
|
+
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
118
|
+
from: result.from,
|
|
119
|
+
to: result.to,
|
|
120
|
+
changes: result.changes.map((c) => ({
|
|
121
|
+
type: c.type,
|
|
122
|
+
table: c.table,
|
|
123
|
+
statement: c.statement,
|
|
124
|
+
})),
|
|
125
|
+
}));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const tty = (0, render_1.isStdoutTty)();
|
|
129
|
+
if (result.changes.length === 0) {
|
|
130
|
+
(0, output_1.emit)(`No pending changes from ${result.from} to ${result.to}.`);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const arrow = tty ? "→" : "->";
|
|
134
|
+
(0, output_1.emit)(`${result.from} ${arrow} ${result.to} (${String(result.changes.length)} changes):\n\n` +
|
|
135
|
+
result.changes.map((c) => ` ${c.statement}`).join("\n"));
|
|
136
|
+
}
|
|
137
|
+
async function confirm(prompt) {
|
|
138
|
+
const rl = node_readline_1.default.createInterface({ input: process.stdin, output: process.stderr });
|
|
139
|
+
return new Promise((resolve) => {
|
|
140
|
+
rl.question(prompt, (answer) => {
|
|
141
|
+
rl.close();
|
|
142
|
+
resolve(answer.trim().toLowerCase() === "y");
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
}
|