@lark-apaas/miaoda-cli 0.1.2-alpha.4e370b6 → 0.1.2-alpha.55feccd
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.
|
@@ -42,85 +42,45 @@ exports.handleDbMigrationApply = handleDbMigrationApply;
|
|
|
42
42
|
const node_readline_1 = __importDefault(require("node:readline"));
|
|
43
43
|
const api = __importStar(require("../../../api/index"));
|
|
44
44
|
const shared_1 = require("../../../cli/commands/shared");
|
|
45
|
-
const error_1 = require("../../../utils/error");
|
|
46
45
|
const output_1 = require("../../../utils/output");
|
|
47
46
|
const render_1 = require("../../../utils/render");
|
|
48
47
|
async function handleDbMigrationInit(opts) {
|
|
49
48
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
50
49
|
// 不可逆操作,TTY 默认要求 y/N;--yes 跳过
|
|
51
50
|
if (!opts.yes && !(0, output_1.isJsonMode)()) {
|
|
52
|
-
|
|
53
|
-
const suffix = opts.syncData ? " (existing data will be copied to dev)" : "";
|
|
54
|
-
const ok = await confirm(`? This action is irreversible. Initialize multi-env (dev / online)${suffix}? (y/N) `);
|
|
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) `);
|
|
55
52
|
if (!ok) {
|
|
56
53
|
(0, output_1.emit)("Aborted.");
|
|
57
54
|
return;
|
|
58
55
|
}
|
|
59
56
|
}
|
|
60
|
-
|
|
61
|
-
try {
|
|
62
|
-
result = await api.db.migrationInit({ appId, syncData: opts.syncData });
|
|
63
|
-
}
|
|
64
|
-
catch (err) {
|
|
65
|
-
// PRD: 重复 init 报错带 hint,引导用户去 diff 看待发布变更
|
|
66
|
-
if (err instanceof error_1.AppError && err.code === "DB_API_k_dl_1300034") {
|
|
67
|
-
throw new error_1.AppError(err.code, err.message, {
|
|
68
|
-
next_actions: ["Run `miaoda db migration diff` to view pending changes."],
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
throw err;
|
|
72
|
-
}
|
|
57
|
+
const result = await api.db.migrationInit({ appId, syncData: opts.syncData });
|
|
73
58
|
if ((0, output_1.isJsonMode)()) {
|
|
74
59
|
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)(result));
|
|
75
60
|
return;
|
|
76
61
|
}
|
|
77
|
-
// PRD 单行格式(不用 key:value 表格):
|
|
78
|
-
// 默认 ✓ / OK: "Multi-env initialized (dev / online)"
|
|
79
|
-
// --sync-data: "Multi-env initialized, data synced to dev"
|
|
80
62
|
const tty = (0, render_1.isStdoutTty)();
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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));
|
|
86
68
|
}
|
|
87
69
|
async function handleDbMigrationDiff(opts) {
|
|
88
70
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
result = await api.db.migrate({ appId, dryRun: true });
|
|
92
|
-
}
|
|
93
|
-
catch (err) {
|
|
94
|
-
throw decorateMigrationError(err);
|
|
95
|
-
}
|
|
96
|
-
// PRD: diff 在无待发布变更时报错带 hint,而不是渲染空列表
|
|
97
|
-
if (result.changes.length === 0) {
|
|
98
|
-
throw new error_1.AppError("DB_API_k_dl_1300035", `No pending changes between ${result.from} and ${result.to}`, {
|
|
99
|
-
next_actions: [
|
|
100
|
-
'Make schema changes in dev first (e.g. `miaoda db sql "ALTER TABLE ..." --env dev`)',
|
|
101
|
-
],
|
|
102
|
-
});
|
|
103
|
-
}
|
|
71
|
+
const result = await api.db.migrate({ appId, dryRun: true });
|
|
104
72
|
renderDiff(result);
|
|
105
73
|
}
|
|
106
74
|
async function handleDbMigrationApply(opts) {
|
|
107
75
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
108
76
|
// TTY 下先 diff 给用户审;--yes 直接打到 online
|
|
109
77
|
if (!opts.yes && !(0, output_1.isJsonMode)()) {
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
preview = await api.db.migrate({ appId, dryRun: true });
|
|
113
|
-
}
|
|
114
|
-
catch (err) {
|
|
115
|
-
throw decorateMigrationError(err);
|
|
116
|
-
}
|
|
78
|
+
const preview = await api.db.migrate({ appId, dryRun: true });
|
|
117
79
|
if (preview.changes.length === 0) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
],
|
|
123
|
-
});
|
|
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;
|
|
124
84
|
}
|
|
125
85
|
renderDiff(preview);
|
|
126
86
|
const ok = await confirm(`? Apply ${String(preview.changes.length)} change(s) to ${preview.to}? (y/N) `);
|
|
@@ -129,13 +89,7 @@ async function handleDbMigrationApply(opts) {
|
|
|
129
89
|
return;
|
|
130
90
|
}
|
|
131
91
|
}
|
|
132
|
-
|
|
133
|
-
try {
|
|
134
|
-
result = await api.db.migrate({ appId, dryRun: false });
|
|
135
|
-
}
|
|
136
|
-
catch (err) {
|
|
137
|
-
throw decorateMigrationError(err);
|
|
138
|
-
}
|
|
92
|
+
const result = await api.db.migrate({ appId, dryRun: false });
|
|
139
93
|
if ((0, output_1.isJsonMode)()) {
|
|
140
94
|
// PRD:{"status": "applied", "from": "dev", "to": "online", "changes_applied": 2}
|
|
141
95
|
(0, output_1.emitOk)((0, output_1.snakeCaseKeys)({
|
|
@@ -180,28 +134,6 @@ function renderDiff(result) {
|
|
|
180
134
|
(0, output_1.emit)(`${result.from} ${arrow} ${result.to} (${String(result.changes.length)} changes):\n\n` +
|
|
181
135
|
result.changes.map((c) => ` ${c.statement}`).join("\n"));
|
|
182
136
|
}
|
|
183
|
-
// decorateMigrationError 给 migration / recovery 路径上的几个错误码补 PRD 规定的 hint。
|
|
184
|
-
// dataloom 后端只返 message + code,hint 由 CLI 端按错误码映射;其它错误码原样透传。
|
|
185
|
-
function decorateMigrationError(err) {
|
|
186
|
-
if (!(err instanceof error_1.AppError))
|
|
187
|
-
return err;
|
|
188
|
-
switch (err.code) {
|
|
189
|
-
case "DB_API_k_dl_1300039":
|
|
190
|
-
// 多环境未初始化:引导先 init
|
|
191
|
-
return new error_1.AppError(err.code, err.message, {
|
|
192
|
-
next_actions: ["Run `miaoda db migration init` to set up multi-env first."],
|
|
193
|
-
});
|
|
194
|
-
case "DB_API_k_dl_1300035":
|
|
195
|
-
// 无待发布变更:引导先在 dev 改 schema
|
|
196
|
-
return new error_1.AppError(err.code, err.message, {
|
|
197
|
-
next_actions: [
|
|
198
|
-
'Make schema changes in dev first (e.g. `miaoda db sql "ALTER TABLE ..." --env dev`)',
|
|
199
|
-
],
|
|
200
|
-
});
|
|
201
|
-
default:
|
|
202
|
-
return err;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
137
|
async function confirm(prompt) {
|
|
206
138
|
const rl = node_readline_1.default.createInterface({ input: process.stdin, output: process.stderr });
|
|
207
139
|
return new Promise((resolve) => {
|