@lark-apaas/miaoda-cli 0.1.39-alpha.7cd293b → 0.1.39-alpha.8630d08
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 +1 -1
- package/dist/cli/commands/db/index.js +20 -1
- package/dist/cli/handlers/db/schema-check.js +104 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ miaoda file ls --output json
|
|
|
44
44
|
| 域 | 用途 |
|
|
45
45
|
| -------------------------- | ---------------------------------------------------- |
|
|
46
46
|
| `miaoda file ...` | 文件操作:上传、下载、元数据、签名下载、批量删除 |
|
|
47
|
-
| `miaoda db ...` | 数据操作:SQL
|
|
47
|
+
| `miaoda db ...` | 数据操作:SQL、表结构、导入导出。Base 结构检测 `miaoda db schema check --json` 返回 `data.results[]` |
|
|
48
48
|
| `miaoda observability ...` | 线上日志、链路、前端源码堆栈反查、监控指标、运营指标 |
|
|
49
49
|
| `miaoda comment ...` | 应用评论:查看评论列表、解决评论(仅 design-html 场景开放) |
|
|
50
50
|
| `miaoda registry ...` | 组件 registry:列出 / copy-in SDK 文件(本地执行) |
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.registerDbCommands = registerDbCommands;
|
|
4
|
+
const commander_1 = require("commander");
|
|
4
5
|
const error_1 = require("../../../utils/error");
|
|
5
6
|
const index_1 = require("../../../cli/handlers/db/index");
|
|
7
|
+
const schema_check_1 = require("../../../cli/handlers/db/schema-check");
|
|
8
|
+
const output_1 = require("../../../utils/output");
|
|
6
9
|
const shared_1 = require("../../../cli/commands/shared");
|
|
7
10
|
const _env_1 = require("../../../cli/handlers/db/_env");
|
|
8
11
|
function parsePositiveInt(raw) {
|
|
@@ -23,7 +26,9 @@ function registerDbCommands(program) {
|
|
|
23
26
|
// 任意 leaf action 执行前先做 --env vocab 校验:用户拼错('onlin'/'prod' 等)立即报
|
|
24
27
|
// UNKNOWN_ENV_VALUE 避免后端无意义被打到。preAction hook 拿父级 dbCmd 的 --env 值
|
|
25
28
|
// —— commander 把父级 option 自动 propagate 给子命令的 optsWithGlobals。
|
|
26
|
-
dbCmd.hook('preAction', () => {
|
|
29
|
+
dbCmd.hook('preAction', (_db, action) => {
|
|
30
|
+
if (action === checkCmd)
|
|
31
|
+
return;
|
|
27
32
|
const env = dbCmd.opts().env;
|
|
28
33
|
(0, _env_1.validateEnv)(typeof env === 'string' ? env : undefined);
|
|
29
34
|
});
|
|
@@ -83,6 +88,20 @@ Examples:
|
|
|
83
88
|
schemaCmd.action(() => {
|
|
84
89
|
schemaCmd.outputHelp();
|
|
85
90
|
});
|
|
91
|
+
const checkCmd = schemaCmd
|
|
92
|
+
.command('check')
|
|
93
|
+
.description('检查当前工程的 Base 与上次成功同步之间的结构变化。')
|
|
94
|
+
.option('--source [source]', '指定检测源,默认 base')
|
|
95
|
+
.addOption(new commander_1.Option('--managed').hideHelp())
|
|
96
|
+
.action(function () {
|
|
97
|
+
const { source, env, managed } = this.optsWithGlobals();
|
|
98
|
+
if ((source !== undefined && source !== 'base') || env !== undefined) {
|
|
99
|
+
(0, output_1.emitError)(new error_1.AppError('ARGS_INVALID', env !== undefined ? 'Base 结构检测不接受 --env' : '结构检测仅支持 --source base'));
|
|
100
|
+
process.exitCode = 2;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
(0, schema_check_1.handleDbSchemaCheck)(source, managed === true);
|
|
104
|
+
});
|
|
86
105
|
schemaCmd
|
|
87
106
|
.command('list')
|
|
88
107
|
.summary('列出所有表的概览(表名、行数、大小等)')
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleDbSchemaCheck = handleDbSchemaCheck;
|
|
4
|
+
const node_child_process_1 = require("node:child_process");
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
const error_1 = require("../../../utils/error");
|
|
8
|
+
const output_1 = require("../../../utils/output");
|
|
9
|
+
const run_npm_1 = require("../../../utils/run-npm");
|
|
10
|
+
const actions = { added: '新增', removed: '移除', renamed: '改名', updated: '修改' };
|
|
11
|
+
function isSourceResult(value) {
|
|
12
|
+
if (!value || typeof value !== 'object')
|
|
13
|
+
return false;
|
|
14
|
+
const result = value;
|
|
15
|
+
return (result.source === 'base' &&
|
|
16
|
+
typeof result.status === 'string' &&
|
|
17
|
+
['changed', 'unchanged', 'no_baseline', 'failed'].includes(result.status) &&
|
|
18
|
+
Array.isArray(result.changes) &&
|
|
19
|
+
(result.status === 'changed') === result.changes.length > 0 &&
|
|
20
|
+
(result.status === 'failed'
|
|
21
|
+
? typeof result.error?.code === 'string' && typeof result.error.message === 'string'
|
|
22
|
+
: result.error === undefined));
|
|
23
|
+
}
|
|
24
|
+
function renderSource(result) {
|
|
25
|
+
switch (result.status) {
|
|
26
|
+
case 'no_baseline':
|
|
27
|
+
return `尚未建立 Base 快照,可进行首次同步。`;
|
|
28
|
+
case 'unchanged':
|
|
29
|
+
return `Base 结构与上次同步一致。`;
|
|
30
|
+
case 'failed':
|
|
31
|
+
return `Base 检测失败:${result.error?.message ?? ''}`;
|
|
32
|
+
case 'changed': {
|
|
33
|
+
const rows = result.changes.map((c) => {
|
|
34
|
+
const path = [c.base_name, c.table_name, c.field_name].filter(Boolean).join(' / ');
|
|
35
|
+
return `${actions[c.action]} ${path} ${JSON.stringify(c.before)} → ${JSON.stringify(c.after)}`;
|
|
36
|
+
});
|
|
37
|
+
return [`Base 结构有变化,可能影响应用代码。`, ...rows].join('\n');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function handleDbSchemaCheck(source, managed = false) {
|
|
42
|
+
const cwd = process.cwd();
|
|
43
|
+
const args = ['--source', 'base'];
|
|
44
|
+
const options = { cwd, encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 };
|
|
45
|
+
// Managed sandbox images install the checker under the platform's /usr prefix.
|
|
46
|
+
const proc = managed
|
|
47
|
+
? (0, node_child_process_1.spawnSync)(process.execPath, ['/usr/bin/db-schema-check', '--managed', ...args], options)
|
|
48
|
+
: runProjectSchemaCheck(cwd, args, options);
|
|
49
|
+
const errorCode = 'SCHEMA_CHECK_FAILED';
|
|
50
|
+
if (proc.error)
|
|
51
|
+
throw new error_1.AppError(errorCode, proc.error.message);
|
|
52
|
+
if (proc.status !== 0) {
|
|
53
|
+
const lines = String(proc.stderr).trim().split('\n');
|
|
54
|
+
for (const line of lines) {
|
|
55
|
+
if (!line.startsWith('{'))
|
|
56
|
+
continue;
|
|
57
|
+
let result;
|
|
58
|
+
try {
|
|
59
|
+
result = JSON.parse(line);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (result.error?.code && result.error.message) {
|
|
65
|
+
throw new error_1.AppError(result.error.code, result.error.message);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
throw new error_1.AppError(errorCode, lines.slice(-4).join('\n') || 'Schema 检查器执行失败');
|
|
69
|
+
}
|
|
70
|
+
let data;
|
|
71
|
+
try {
|
|
72
|
+
const body = JSON.parse(String(proc.stdout));
|
|
73
|
+
if (!body?.data)
|
|
74
|
+
throw new Error();
|
|
75
|
+
data = body.data;
|
|
76
|
+
if (typeof data.app_id !== 'string' ||
|
|
77
|
+
!data.app_id.trim() ||
|
|
78
|
+
!Array.isArray(data.results) ||
|
|
79
|
+
!data.results.every(isSourceResult) ||
|
|
80
|
+
data.results.length !== 1) {
|
|
81
|
+
throw new Error();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
throw new error_1.AppError('INVALID_SCHEMA_CHECK_RESULT', 'Schema 检查器返回了无效结果,请确认已安装支持相应数据源的 db-schema-sync');
|
|
86
|
+
}
|
|
87
|
+
const result = data.results[0];
|
|
88
|
+
const skipUnconfigured = source === undefined &&
|
|
89
|
+
result.status === 'failed' &&
|
|
90
|
+
result.error?.code === 'SOURCE_NOT_CONFIGURED';
|
|
91
|
+
if ((0, output_1.isJsonMode)()) {
|
|
92
|
+
(0, output_1.emitOk)(skipUnconfigured ? { ...data, results: [] } : data);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
(0, output_1.emit)(skipUnconfigured ? '当前应用没有配置 Base 数据源。' : renderSource(result));
|
|
96
|
+
}
|
|
97
|
+
function runProjectSchemaCheck(cwd, args, options) {
|
|
98
|
+
const bin = process.platform === 'win32' ? 'db-schema-check.cmd' : 'db-schema-check';
|
|
99
|
+
const local = (0, node_fs_1.existsSync)((0, node_path_1.join)(cwd, 'node_modules', '.bin', bin));
|
|
100
|
+
const npm = local
|
|
101
|
+
? ['exec', '--offline', '--', 'db-schema-check']
|
|
102
|
+
: ['exec', '--yes', '--package=@lark-apaas/db-schema-sync@latest', '--', 'db-schema-check'];
|
|
103
|
+
return (0, run_npm_1.runNpmSync)([...npm, ...args], options);
|
|
104
|
+
}
|