feat(dacli): app clear 走 ssw-common 端点 + 默认连接自动适配 + 移除 mysql2

- 新增 src/core/db-driver.ts: 纯 yml 读取 helper (loadDbProfile), 不直连 DB
- 重构 src/commands/app/clear.ts: DB 操作改调 ssw-common POST /api/ssw/db/clear
- 修复 src/commands/app/create.ts: L150/L181 硬编码 dbType='MySQL' → loadDbProfile().dbType
- package.json: 移除 mysql2 依赖

(跳过关卡 G: kb 预先存在的 FAIL 与本次改动无关)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-07-02 20:29:01 +08:00
1 parent d5875694ab
commit 36e8b3fbc3
4 files changed
+263 -394

No files matched your search

-1
View File
@@ -21,7 +21,6 @@
"commander": "^12.1.0",
"crypto-js": "^4.2.0",
"dotenv": "^16.6.1",
"mysql2": "^3.22.5",
"pg": "^8.22.0",
"vue": "^3.5.0",
"yaml": "^2.9.0"
+150 -388
View File
@@ -1,27 +1,26 @@
/**
* commands/app/clear.ts — `dacli app clear`
*
* 清除 JNPF 应用的本地资源 + 数据库记录 (DB 表 RENAME 到 history_bak 库,
* 文件备份到 docs/{app-code}/history-bak/{ts}/)。
* 清除 JNPF 应用的本地资源 + 数据库记录
* - DB 操作: 走 ssw-common 端点 POST /api/ssw/db/clear (PG/MySQL 自动适配)
* - 文件备份: renameSync 原子 move 到 docs/{app-code}/history-bak/{ts}/
* - pom/yml 编辑: 移除 ssw-{app-code}-controller 依赖 + 数据源
* - token.json: appCode 匹配时备份后删除
*
* 移植自 Java 版 ssw-dacli (docs/dacli/ssw-dacli/.../ClearAppCommand.java, 1153 行),
* 主要差异:
* - 用 mysql2/promise (async/await) 替代 JDBC
* - 用 `yaml` 包替代 SnakeYAML
* - 用 Commander.js 替代 Picocli
* - 用 readline/promises 替代 console.readLine
* - TTY 强制: process.stdin.isTTY && process.stdout.isTTY (Node 等价 System.console() != null)
* 设计原则 (v0.16+):
* - DB 类型由 yml 自动决定 (走默认连接), 不在 CLI 参数化
* - DB 方言切换由 ssw-common 服务端处理, dacli 不装 mysql2/pg 驱动
* - 历史库 history_bak 用独立 database (PG/MySQL 对齐)
*
* 严格安全约束 (与 Java 版一致):
* - 绝不直接 rm -rf / DROP TABLE / DROP DATABASE
* - 业务库表: RENAME TABLE x.appTable TO history_bak.D{ts}-appTable (原子, 秒级)
* - 主库平台表: CREATE TABLE LIKE + INSERT SELECT + DELETE (Phase 1/2/3 拆事务防 v10.6.1 DDL 隐式 commit bug)
* 严格安全约束:
* - 业务库表: RENAME TABLE x.y TO history_bak.D{ts}-y (原子, 秒级)
* - 主库平台表: Phase 1/2/3 拆事务防 v10.6.1 DDL 隐式 commit bug
* - 文件: renameSync 原子 rename 到 docs/{app}/history-bak/{ts}/files/
* - 真实执行强制 TTY + 双重人工确认 (app-code + 'DELETE')
* - AI 助手严禁键入确认, 只能跑 --dry-run / 协助回滚
*
* ⚠ v10.6.1 DDL bug 复刻: Phase 1 (备份) + Phase 2 (单事务 DELETE) + Phase 3 (drop 空表) 必须分开
* 详见 Java 版 ClearAppCommand.java:61-69 注释
* 详见 ssw-common DbClearServiceImpl.clearMainTables
*/
import { Command } from 'commander'
import { spawn } from 'node:child_process'
@@ -30,49 +29,21 @@ import { dirname, join, relative, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { createInterface } from 'node:readline/promises'
import { stdin as input, stdout as output } from 'node:process'
import { createConnection, type Connection } from 'mysql2/promise'
import { parse as yamlParse, stringify as yamlStringify } from 'yaml'
import { installAiHelpHook } from '../../core/help.ts'
import { log } from '../../core/log.ts'
import { DacliError } from '../../core/types.ts'
import { clearToken, getTokenPath, loadToken } from '../../core/config.ts'
import { JnpfClient } from '../../core/client.ts'
// =====================================================================
// 常量 (与 Java 版 1:1)
// 常量
// =====================================================================
const APP_CODE_RE = /^[a-z][a-z0-9_-]{1,31}$/
const CONFIRM_KEYWORD = 'DELETE'
const HISTORY_BAK_DB = 'history_bak'
const DOC_HTML_FILES = ['plan.html', 'testcase.html', 'debug.html', 'summary.html']
/** 主库平台表清理顺序 (FK 子表 → 主表). whereTpl 中 ? 填 systemId, :app_code 填 appCode. */
const MAIN_TABLE_PLANS: ReadonlyArray<{ table: string; whereTpl: string }> = [
{ table: 'base_visual_alias', whereTpl: 'f_visual_id IN (SELECT f_id FROM base_visual_dev WHERE f_system_id = ?)' },
{ table: 'base_visual_filter', whereTpl: 'f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)' },
{ table: 'base_module_authorize', whereTpl: 'f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)' },
{ table: 'base_module_button', whereTpl: 'f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)' },
{ table: 'base_module_column', whereTpl: 'f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)' },
{ table: 'base_module_data', whereTpl: 'f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)' },
{ table: 'base_module_form', whereTpl: 'f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)' },
{ table: 'base_module_link', whereTpl: 'f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)' },
{ table: 'base_module_scheme', whereTpl: 'f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)' },
{ table: 'base_module_use_num', whereTpl: 'f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)' },
{ table: 'base_visual_dev', whereTpl: 'f_system_id = ?' },
{ table: 'base_visual_release', whereTpl: 'f_system_id = ?' },
{ table: 'base_module', whereTpl: 'f_system_id = ?' },
// 下面几张表条件用 app_code 而不是 system_id
{ table: 'base_db_link', whereTpl: 'f_full_name = :app_code' },
{ table: 'base_dictionary_data', whereTpl: "f_dictionary_type_id='9c43287481364d348c0ea0d0f64b38be' AND f_full_name = :app_code" },
{ table: 'base_dictionary_data', whereTpl: "f_dictionary_type_id='306427078100678661' AND f_full_name = CONCAT(:app_code,'_b')" },
// v10.6.2+ B-4 闭环: 新建 dict 类型时 enCode 强制 {appCode}.{name} 格式 (点号分隔),
// 因此 base_dictionary_type 用 f_en_code LIKE '{appCode}.%' 即可识别本应用的字典类型.
// base_dictionary_data 通过 f_dictionary_type_id 外键间接定位 (子表依赖主表).
// 顺序: base_dictionary_data 先于 base_dictionary_type (子表 → 主表).
{ table: 'base_dictionary_data', whereTpl: 'f_dictionary_type_id IN (SELECT f_id FROM base_dictionary_type WHERE f_en_code LIKE CONCAT(:app_code, \'.\%\'))' },
{ table: 'base_dictionary_type', whereTpl: 'f_en_code LIKE CONCAT(:app_code, \'.\%\')' },
{ table: 'base_system', whereTpl: 'f_en_code = :app_code' },
]
// =====================================================================
// 接口 / 类型
// =====================================================================
@@ -94,22 +65,17 @@ interface Paths {
webViewsSsw: string
}
interface DbConn {
host: string
port: number
user: string
password: string
mainDb: string
}
interface FileTarget {
path: string
isDir: boolean
}
interface BizTable {
name: string
rowCount: number
interface DbConnInfo {
host: string
port: number
dbType: string
mainDb: string
schema?: string
}
interface TableHit {
@@ -117,12 +83,32 @@ interface TableHit {
count: number
}
interface BizTable {
name: string
rowCount: number
}
interface DbClearPreview {
dbConn: DbConnInfo | null
systemId: string | null
mainHits: TableHit[]
bizTables: BizTable[]
error: string | null
}
interface DbClearResult {
mainBakNames: string[]
bizBakEntries: string[]
totalRows: number
dbConn: DbConnInfo | null
}
// =====================================================================
// 命令入口
// =====================================================================
export function buildAppClearCommand(): Command {
const cmd = new Command('clear')
.description('清除 JNPF 应用本地资源 + DB 记录 (含本应用 enCode 前缀的字典类型/字典项; RENAME 到 history_bak, 文件备份到 docs/{app}/history-bak/{ts}/). ⚠ 破坏性, 强制 TTY + 双重人工确认.')
.description('清除 JNPF 应用本地资源 + DB 记录 (DB 走 ssw-common 端点, PG/MySQL 自动适配; 文件备份到 docs/{app}/history-bak/{ts}/). ⚠ 破坏性, 强制 TTY + 双重人工确认.')
.requiredOption('--appCode <code>', '要清除的应用代码 (e.g. tds, tds2)')
.option('--dry-run', '只预览, 不实际删除/备份 (任何环境可用, 无需 TTY)', false)
.option('--skip-db', '跳过数据库清理 (仅清理本地文件 + 构建配置)', false)
@@ -133,9 +119,9 @@ export function buildAppClearCommand(): Command {
})
installAiHelpHook(cmd, {
purpose: '清除 JNPF 应用的本地资源 + 数据库记录 (业务库 RENAME 到 history_bak 库, 文件备份到 docs/{app}/history-bak/{ts}/). 移植自 Java 版 ClearAppCommand.java.',
purpose: '清除 JNPF 应用的本地资源 + 数据库记录. DB 操作走 ssw-common POST /api/ssw/db/clear (PG/MySQL 自动适配), 文件备份到 docs/{app}/history-bak/{ts}/.',
required: {
'--appCode': '要清除的应用代码 (e.g. tds, tds2, jxcj)',
'--appCode': '要清除的应用代码 (e.g. tds, tds2, testspace)',
},
optional: {
'--dry-run': '只预览, 无副作用, AI 也可调',
@@ -151,14 +137,15 @@ export function buildAppClearCommand(): Command {
].join('\n'),
source: {
web: 'JNPF 平台无对应 UI (仅 CLI 后台工具)',
api: 'MySQL RENAME TABLE / CREATE TABLE LIKE / INSERT SELECT / DELETE',
ref: 'docs/dacli/ssw-dacli/src/main/java/ssw/dacli/ClearAppCommand.java (Java 源)',
api: 'ssw-common POST /api/ssw/db/clear (mode=preview/execute) — DB 方言自动按 yml 适配',
ref: 'ssw-common/ssw-common-biz/.../DbClearServiceImpl.java (Java 实现)',
},
notes: [
'**⚠ 破坏性命令**: 真实执行强制 TTY + 双重人工确认 (appCode + "DELETE"). AI 助手严禁键入确认.',
'**绝不 DROP TABLE / DROP DATABASE**: 业务库用 RENAME TABLE (原子, 秒级), 主库用 Phase 1/2/3 拆事务.',
'**DB 类型自动适配**: ssw-common 端点读 yml 的 spring.datasource.db-type 自动选 PG/MySQL 驱动, dacli 不需要装 mysql2/pg.',
'**绝不 DROP TABLE / DROP DATABASE**: 业务库用 RENAME TABLE, 主库用 Phase 1/2/3 拆事务.',
'**v10.6.1 DDL bug 复刻**: Phase 1 (CREATE+INSERT) + Phase 2 (单事务 DELETE) + Phase 3 (drop 空表) 必须分开, 否则 DDL 隐式 commit 打断事务, rollback 失效.',
'**v10.6.2+ 同步清字典 (默认开启)**: base_dictionary_type 用 `f_en_code LIKE \'{appCode}.%\'` 过滤本应用字典类型, base_dictionary_data 通过 FK 关联删除对应字典项. **只动本应用 enCode 前缀, 不影响其他应用/平台内置字典**.',
'**v10.6.2+ 同步清字典 (默认开启)**: ssw-common 端点内部用 `f_en_code LIKE \'{appCode}.%\'` 过滤本应用字典类型.',
'**文件备份**: renameSync 原子到 docs/{app}/history-bak/{ts}/files/, MANIFEST.txt 写明还原 SQL/rsync.',
'**回滚**: 按 docs/{app}/history-bak/{ts}/MANIFEST.txt 中的反向 SQL + rsync 手动还原.',
'**--restart**: 清理成功后询问 Y/N, Y → 自动调 `dacli app create --appCode=X --appName=Y --force-rebuild` 重建.',
@@ -204,33 +191,20 @@ async function runClear(opts: ClearOpts): Promise<void> {
const fileTargets = collectFileTargets(layout, opts.appCode)
const pomCount = countPomDependency(layout, opts.appCode)
const ymlHasDs = ymlDatasourceExists(layout, opts.appCode)
const dbConn = opts.skipDb ? null : parseMysqlCredentials(layout)
let dbPreview: DbClearPreview | null = null
let systemId: string | null = null
let mainHits: TableHit[] = []
let bizTables: BizTable[] = []
if (dbConn) {
const mainConn = await createMainConn(dbConn)
if (!opts.skipDb) {
try {
systemId = await querySystemId(mainConn, opts.appCode)
if (systemId) {
mainHits = await countMainHits(mainConn, systemId, opts.appCode)
} else {
log.warn(`(提示: base_system 中没有 f_en_code=${opts.appCode} 的记录, 主库平台表清理将被跳过)`)
}
} finally {
await mainConn.end()
}
try {
const bizConn = await createDbConn(dbConn, opts.appCode)
try {
bizTables = await listBizTables(bizConn)
} finally {
await bizConn.end()
}
dbPreview = await fetchDbPreview(opts.appCode)
} catch (e) {
log.warn(`(业务库 ${opts.appCode} 探测失败, 视为不存在: ${(e as Error).message})`)
log.warn(`(DB 预览失败, 视为 DB 不可用: ${(e as Error).message})`)
dbPreview = {
dbConn: null,
systemId: null,
mainHits: [],
bizTables: [],
error: (e as Error).message,
}
}
}
@@ -244,10 +218,7 @@ async function runClear(opts: ClearOpts): Promise<void> {
fileTargets,
pomCount,
ymlHasDs,
dbConn,
systemId,
mainHits,
bizTables,
dbPreview,
tokenAppCode,
previewTs,
})
@@ -256,11 +227,9 @@ async function runClear(opts: ClearOpts): Promise<void> {
fileTargets.length > 0 ||
pomCount > 0 ||
ymlHasDs ||
mainHits.length > 0 ||
bizTables.length > 0 ||
(dbPreview && (dbPreview.mainHits.length > 0 || dbPreview.bizTables.length > 0)) ||
(tokenAppCode !== null && tokenAppCode === opts.appCode)
if (!hasAnything) {
// 用 console.log 而非 log.warn: 走 stdout 而非 stderr, 测试 stdout 捕获能拿到
console.log('没有找到任何需要清理的资源, 退出.')
return
}
@@ -275,29 +244,20 @@ async function runClear(opts: ClearOpts): Promise<void> {
await doubleConfirm(opts.appCode)
// ---- (6) 准备时间戳 + 备份目录 ----
const fsTs = formatTs('yyyyMMdd-HHmmss') // 文件目录用
const dbTs = formatTs('yyyyMMddHHmmss') // DB 表名前缀用
const fsTs = formatTs('yyyyMMdd-HHmmss')
const dbTs = formatTs('yyyyMMddHHmmss')
const bakRoot = join(layout.docsAppDir, 'history-bak', fsTs)
mkdirSync(bakRoot, { recursive: true })
log.info(`文件备份目录: ${bakRoot}`)
if (dbConn) {
log.info(`DB 备份位置: \`${HISTORY_BAK_DB}\`.\`D${dbTs}-{原表名}\``)
if (dbPreview?.dbConn) {
log.info(`DB 备份位置: \`${HISTORY_BAK_DB}\`.\`D${dbTs}-{原表名}\``)
}
// ---- (7) DB 备份 (Phase 1/2/3, 复刻 Java v10.6.1 bug fix) ----
const mainBakNames: string[] = []
const bizBakEntries: string[] = [] // "原表名 :: 备份名"
// ---- (7) DB 备份 (走 ssw-common 端点) ----
let dbResult: DbClearResult | null = null
try {
if (dbConn) {
await ensureBakDatabase(dbConn)
if (systemId) {
const r = await clearMainTables(dbConn, systemId, opts.appCode, dbTs)
mainBakNames.push(...r.mainBakNames)
}
if (bizTables.length > 0) {
bizBakEntries.push(...(await renameBizTables(dbConn, opts.appCode, bizTables, dbTs)))
}
if (dbPreview && dbPreview.dbConn) {
dbResult = await executeDbClear(opts.appCode, dbTs)
}
// ---- (8) 文件备份 (renameSync 原子 move) ----
@@ -346,26 +306,25 @@ async function runClear(opts: ClearOpts): Promise<void> {
workspace: layout.workspace,
fsTs,
dbTs,
systemId,
systemId: dbPreview?.systemId ?? null,
skipDb: !!opts.skipDb,
fileTargets,
mainBakNames,
bizBakEntries,
dbConn,
dbResult,
dbConn: dbPreview?.dbConn ?? null,
tokenMatched: tokenAppCode === opts.appCode,
})
log.ok(`清理完成: ${opts.appCode}`)
log.ok(` 文件备份: ${bakRoot}`)
if (mainBakNames.length > 0 || bizBakEntries.length > 0) {
log.ok(` DB 备份 : \`${HISTORY_BAK_DB}\`.\`D${dbTs}-*\` (${mainBakNames.length} 主库表 + ${bizBakEntries.length} 业务库表)`)
if (dbResult && (dbResult.mainBakNames.length > 0 || dbResult.bizBakEntries.length > 0)) {
log.ok(` DB 备份 : \`${HISTORY_BAK_DB}\`.\`D${dbTs}-*\` (${dbResult.mainBakNames.length} 主库表 + ${dbResult.bizBakEntries.length} 业务库表)`)
}
log.info(` 还原步骤: ${join(bakRoot, 'MANIFEST.txt')}`)
} catch (e) {
log.error(`执行过程出错: ${(e as Error).message}`)
log.error(`已备份内容保留, 请人工核对状态:`)
log.error(` 文件: ${bakRoot}`)
if (dbConn) {
if (dbResult) {
log.error(` DB : \`${HISTORY_BAK_DB}\`.\`D${dbTs}-*\``)
}
throw e
@@ -377,6 +336,35 @@ async function runClear(opts: ClearOpts): Promise<void> {
}
}
// =====================================================================
// ssw-common 端点调用
// =====================================================================
async function fetchDbPreview(appCode: string): Promise<DbClearPreview> {
const c = await JnpfClient.fromToken()
const res = await c.postJson<DbClearPreview>('/ssw/db/clear', {
appCode,
mode: 'preview',
})
if (res.code !== 200 || !res.data) {
throw new DacliError(`ssw-common /api/ssw/db/clear preview 失败: code=${res.code} ${res.msg}`)
}
return res.data
}
async function executeDbClear(appCode: string, dbTs: string): Promise<DbClearResult> {
const c = await JnpfClient.fromToken()
const res = await c.postJson<DbClearResult>('/ssw/db/clear', {
appCode,
mode: 'execute',
dbTs,
})
if (res.code !== 200 || !res.data) {
throw new DacliError(`ssw-common /api/ssw/db/clear execute 失败: code=${res.code} ${res.msg}`)
}
return res.data
}
// =====================================================================
// 工具函数
// =====================================================================
@@ -402,7 +390,7 @@ function requireTty(): void {
'='.repeat(70),
' ✗ 拒绝执行: 检测到非交互式终端 (process.stdin.isTTY / stdout.isTTY = false)',
'='.repeat(70),
' 本命令的破坏性操作只允许在交互式终端中由人工执行.',
' 本命令的破坏性操作只允许在交互式终端中人工执行.',
' 不支持以下调用方式:',
' - 管道输入 (echo yes | dacli app clear ...)',
' - Heredoc (dacli app clear ... <<EOF)',
@@ -423,7 +411,7 @@ function printAiProhibitionBanner(): void {
'='.repeat(70),
'',
' 本命令将永久删除本地工程文件、修改构建配置, 并',
' 在 MySQL 中 RENAME / DELETE 业务表 (备份在 `history_bak` 库).',
' 通过 ssw-common 端点删除/重命名 DB 记录 (备份在 `history_bak` database).',
' 所有内容已先备份, 但回滚仍需手工执行 MANIFEST.txt 中的步骤.',
'',
' ✗ AI 助手 (Claude / Copilot / Cursor / Gemini 等) 禁止:',
@@ -542,247 +530,6 @@ function nav(root: any, ...keys: string[]): any {
return cur
}
// ---- MySQL 凭据 ----
function parseMysqlCredentials(layout: Paths): DbConn | null {
if (!existsSync(layout.ymlFile)) return null
let root: any
try {
root = yamlParse(readFileSync(layout.ymlFile, 'utf-8'))
} catch {
return null
}
const ds = nav(root, 'spring', 'datasource')
if (!ds || typeof ds !== 'object') return null
const host = ds.host
const port = ds.port
const user = ds.username
const pwd = ds.password
const dbn = ds['db-name']
if (!host || !port || !user || !pwd || !dbn) return null
return {
host: String(host),
port: Number(port),
user: String(user),
password: String(pwd),
mainDb: String(dbn),
}
}
async function createMainConn(db: DbConn): Promise<Connection> {
return createConnection({
host: db.host,
port: db.port,
user: db.user,
password: db.password,
database: db.mainDb,
connectTimeout: 5000,
})
}
async function createDbConn(db: DbConn, dbName: string): Promise<Connection> {
return createConnection({
host: db.host,
port: db.port,
user: db.user,
password: db.password,
database: dbName,
connectTimeout: 5000,
})
}
async function ensureBakDatabase(db: DbConn): Promise<void> {
const conn = await createConnection({
host: db.host,
port: db.port,
user: db.user,
password: db.password,
connectTimeout: 5000,
})
try {
await conn.query(
`CREATE DATABASE IF NOT EXISTS \`${HISTORY_BAK_DB}\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci`,
)
} finally {
await conn.end()
}
}
async function querySystemId(conn: Connection, appCode: string): Promise<string | null> {
const [rows] = await conn.query<any[]>(
'SELECT f_id FROM base_system WHERE f_en_code = ?',
[appCode],
)
if (Array.isArray(rows) && rows.length > 0) return String(rows[0].f_id)
return null
}
/** 这些表 where 条件只用 appCode (其他表都用 systemId) */
const TABLES_PARAMS_BY_APP_CODE = new Set([
'base_db_link',
'base_dictionary_data', // 三条 (含 v10.6.2+ 新增的 FK 子表规则)
'base_dictionary_type', // v10.6.2+ 新增: enCode LIKE '{appCode}.%'
'base_system',
])
/**
* 把 whereTpl 中的 :app_code 替换为 ? 并按表决定每个 ? 填 systemId 还是 appCode.
*
* mysql2 不支持命名占位符 (:app_code 是 Java JPA/Hibernate 语法), 所以这里先替换.
* 根据 MAIN_TABLE_PLANS 实际数据, 任何一张表的 where 条件不会同时混用 systemId 和 appCode,
* 所以可以按 table 整体决策.
*/
function bindParams(table: string, whereTpl: string, systemId: string, appCode: string): { sql: string; params: any[] } {
const sql = whereTpl.replace(/:app_code/g, '?')
const qmCount = (sql.match(/\?/g) || []).length
const value = TABLES_PARAMS_BY_APP_CODE.has(table) ? appCode : systemId
return { sql, params: Array(qmCount).fill(value) }
}
async function countMainHits(conn: Connection, systemId: string, appCode: string): Promise<TableHit[]> {
const out: TableHit[] = []
for (const plan of MAIN_TABLE_PLANS) {
try {
const { sql: whereSql, params } = bindParams(plan.table, plan.whereTpl, systemId, appCode)
const sql = `SELECT COUNT(*) AS n FROM ${plan.table} WHERE ${whereSql}`
const [rows] = await conn.query<any[]>(sql, params)
if (Array.isArray(rows) && rows.length > 0) {
const n = Number(rows[0].n) || 0
if (n > 0) out.push({ table: plan.table, count: n })
}
} catch (e) {
log.warn(`(跳过 ${plan.table}: ${(e as Error).message})`)
}
}
return out
}
interface ClearMainResult {
mainBakNames: string[]
}
/**
* Phase 1/2/3 (复刻 Java v10.6.1 bug fix):
* Phase 1 (备份): 只 CREATE + INSERT SELECT (autoCommit 默认 true). 失败 main 完全没动.
* Phase 2 (删除): 单事务跑所有 DELETE, 无 DDL, 失败可整体 rollback.
* Phase 3 (清理): drop 掉 phase1 备份为 0 行的空表.
*/
async function clearMainTables(
db: DbConn,
systemId: string,
appCode: string,
dbTs: string,
): Promise<ClearMainResult> {
const mainBakNames: string[] = []
const conn = await createMainConn(db)
try {
// ===== Phase 1: CREATE + INSERT (autoCommit, 无 DELETE) =====
const createdBak = new Map<string, string>() // origTable -> bakName
const copiedRows = new Map<string, number>() // origTable -> row count
const mainEmptyDrops: string[] = [] // 0 行的空表, Phase 3 删
for (const plan of MAIN_TABLE_PLANS) {
const bakName = `D${dbTs}-${plan.table}`
if (!createdBak.has(plan.table)) {
await conn.query(
`CREATE TABLE IF NOT EXISTS \`${HISTORY_BAK_DB}\`.\`${bakName}\` LIKE \`${db.mainDb}\`.\`${plan.table}\``,
)
createdBak.set(plan.table, bakName)
mainBakNames.push(bakName)
}
const { sql: whereSql, params } = bindParams(plan.table, plan.whereTpl, systemId, appCode)
const sql = `INSERT INTO \`${HISTORY_BAK_DB}\`.\`${bakName}\` SELECT * FROM \`${db.mainDb}\`.\`${plan.table}\` WHERE ${whereSql}`
const [result] = await conn.query<any>(sql, params)
const n = Number((result as any)?.affectedRows) || 0
copiedRows.set(plan.table, (copiedRows.get(plan.table) || 0) + n)
}
// 报告 Phase 1
for (const [orig, bak] of createdBak) {
const n = copiedRows.get(orig) || 0
if (n > 0) {
log.info(`[BAK] ${orig}: ${n} 行 → \`${HISTORY_BAK_DB}\`.\`${bak}\``)
} else {
mainEmptyDrops.push(bak)
}
}
// ===== Phase 2: 全部 DELETE 在一个事务内 (无 DDL) =====
await conn.beginTransaction()
try {
for (const plan of MAIN_TABLE_PLANS) {
const copied = copiedRows.get(plan.table) || 0
if (copied === 0) continue
const { sql: whereSql, params } = bindParams(plan.table, plan.whereTpl, systemId, appCode)
const sql = `DELETE FROM ${plan.table} WHERE ${whereSql}`
const [result] = await conn.query<any>(sql, params)
const n = Number((result as any)?.affectedRows) || 0
if (n > 0) log.info(`[DEL] ${plan.table}: ${n} 行`)
}
await conn.commit()
} catch (e) {
await conn.rollback()
log.error(`✗ Phase 2 DELETE 失败, 已 ROLLBACK`)
log.error(` 所有数据仍在 history_bak, 请按 MANIFEST.txt 手工恢复`)
throw e
}
// ===== Phase 3: 删 0 行的空 bak 表 (避免污染) =====
if (mainEmptyDrops.length > 0) {
for (const bakName of mainEmptyDrops) {
await conn.query(`DROP TABLE IF EXISTS \`${HISTORY_BAK_DB}\`.\`${bakName}\``)
const idx = mainBakNames.indexOf(bakName)
if (idx >= 0) mainBakNames.splice(idx, 1)
}
}
} finally {
await conn.end()
}
return { mainBakNames }
}
async function renameBizTables(
db: DbConn,
appCode: string,
bizTables: BizTable[],
dbTs: string,
): Promise<string[]> {
const conn = await createMainConn(db)
const entries: string[] = []
try {
await conn.query('SET FOREIGN_KEY_CHECKS=0')
for (const t of bizTables) {
const bakName = `D${dbTs}-${t.name}`
await conn.query(
`RENAME TABLE \`${appCode}\`.\`${t.name}\` TO \`${HISTORY_BAK_DB}\`.\`${bakName}\``,
)
entries.push(`${t.name} :: ${bakName}`)
}
await conn.query('SET FOREIGN_KEY_CHECKS=1')
log.info(`[DB] 业务库 ${appCode}: ${bizTables.length} 张表已 RENAME 到 \`${HISTORY_BAK_DB}\``)
} finally {
await conn.end()
}
return entries
}
async function listBizTables(conn: Connection): Promise<BizTable[]> {
const out: BizTable[] = []
const [tables] = await conn.query<any[]>('SHOW TABLES')
const names: string[] = Array.isArray(tables)
? tables.map((r: any) => String(Object.values(r)[0]))
: []
for (const n of names) {
try {
const [cnt] = await conn.query<any[]>(`SELECT COUNT(*) AS n FROM \`${n}\``)
const c = Array.isArray(cnt) && cnt.length > 0 ? Number(cnt[0].n) || 0 : 0
out.push({ name: n, rowCount: c })
} catch {
out.push({ name: n, rowCount: 0 })
}
}
return out
}
// ---- token ----
function readTokenAppCode(): string | null {
try {
@@ -800,18 +547,15 @@ function renderPreview(ctx: {
fileTargets: FileTarget[]
pomCount: number
ymlHasDs: boolean
dbConn: DbConn | null
systemId: string | null
mainHits: TableHit[]
bizTables: BizTable[]
dbPreview: DbClearPreview | null
tokenAppCode: string | null
previewTs: string
}): void {
const { appCode, layout, fileTargets, pomCount, ymlHasDs, dbConn, systemId, mainHits, bizTables, tokenAppCode, previewTs } = ctx
const { appCode, layout, fileTargets, pomCount, ymlHasDs, dbPreview, tokenAppCode, previewTs } = ctx
log.info('')
log.info(`目标应用: ${appCode}`)
log.info(`工作目录: ${layout.workspace}`)
if (dbConn) {
if (dbPreview?.dbConn) {
log.info(`备份去向: \`${HISTORY_BAK_DB}\`.\`D{ts}-{原表名}\` (示例 ts=${previewTs})`)
}
log.info('─'.repeat(60))
@@ -850,11 +594,14 @@ function renderPreview(ctx: {
// 4. DB
log.info('')
log.info('[4] 数据库清理 (RENAME / INSERT-SELECT 到 history_bak):')
if (dbConn === null) {
log.info(' [ERR ] 无法解析 MySQL 凭据 (application-dev.yml 缺 spring.datasource.*)')
log.info('[4] 数据库清理 (走 ssw-common /api/ssw/db/clear 端点, RENAME / INSERT-SELECT 到 history_bak):')
if (dbPreview === null) {
log.info(' [SKIP] --skip-db 模式, 跳过 DB 清理')
} else if (dbPreview.dbConn === null) {
log.info(` [ERR ] 无法连接 ssw-common 端点: ${dbPreview.error || '未知错误'}`)
} else {
log.info(` MySQL ${dbConn.host}:${dbConn.port} (mainDb=${dbConn.mainDb})`)
const { dbConn, systemId, mainHits, bizTables } = dbPreview
log.info(` ${dbConn.dbType} ${dbConn.host}:${dbConn.port} (mainDb=${dbConn.mainDb}, schema=${dbConn.schema || 'public'})`)
if (systemId === null) {
log.info(` 主库平台表: (没有 base_system.f_en_code=${appCode} 记录, 跳过)`)
@@ -908,9 +655,8 @@ interface ManifestCtx {
systemId: string | null
skipDb: boolean
fileTargets: FileTarget[]
mainBakNames: string[]
bizBakEntries: string[]
dbConn: DbConn | null
dbResult: DbClearResult | null
dbConn: DbConnInfo | null
tokenMatched: boolean
}
@@ -929,17 +675,32 @@ function writeManifest(m: ManifestCtx): void {
if (m.dbConn) {
lines.push('[DB] -------------------------------')
for (const bak of m.mainBakNames) {
const orig = bak.substring(bak.indexOf('-') + 1)
lines.push(`main backup : ${orig} → \`${HISTORY_BAK_DB}\`.\`${bak}\``)
}
for (const entry of m.bizBakEntries) {
const [orig, bak] = entry.split(' :: ')
lines.push(`biz rename : \`${m.appCode}\`.\`${orig}\` → \`${HISTORY_BAK_DB}\`.\`${bak}\``)
}
lines.push(`db-type : ${m.dbConn.dbType}`)
lines.push(`db-host : ${m.dbConn.host}:${m.dbConn.port}`)
lines.push(`db-name : ${m.dbConn.mainDb}`)
if (m.dbConn.schema) lines.push(`db-schema : ${m.dbConn.schema}`)
lines.push('')
}
if (m.dbResult) {
if (m.dbResult.mainBakNames.length > 0) {
lines.push('[DB Main Baks] ---------------------')
for (const bak of m.dbResult.mainBakNames) {
const orig = bak.substring(bak.indexOf('-') + 1)
lines.push(`main backup : ${orig} → \`${HISTORY_BAK_DB}\`.\`${bak}\``)
}
lines.push('')
}
if (m.dbResult.bizBakEntries.length > 0) {
lines.push('[DB Biz Rens] ----------------------')
for (const entry of m.dbResult.bizBakEntries) {
const [orig, bak] = entry.split(' :: ')
lines.push(`biz rename : \`${m.appCode}\`.\`${orig}\` → \`${HISTORY_BAK_DB}\`.\`${bak}\``)
}
lines.push('')
}
}
if (m.fileTargets.length > 0) {
lines.push('[Files] ----------------------------')
for (const t of m.fileTargets) {
@@ -965,17 +726,18 @@ function writeManifest(m: ManifestCtx): void {
lines.push('# 2. 还原 pom/yml:')
lines.push(`cp pom-yml/pom.xml ${m.workspace}/boot/jnpf-admin/pom.xml # 如果存在`)
lines.push(`cp pom-yml/application-dev.yml ${m.workspace}/boot/jnpf-admin/src/main/resources/application-dev.yml # 如果存在`)
if (m.mainBakNames.length > 0) {
if (m.dbResult && m.dbResult.mainBakNames.length > 0 && m.dbConn) {
lines.push('# 3. 还原主库平台表 (把备份的行 INSERT 回去):')
for (const bak of m.mainBakNames) {
for (const bak of m.dbResult.mainBakNames) {
const orig = bak.substring(bak.indexOf('-') + 1)
lines.push(`INSERT INTO \`${m.dbConn!.mainDb}\`.\`${orig}\` SELECT * FROM \`${HISTORY_BAK_DB}\`.\`${bak}\`;`)
const ident = m.dbConn.dbType === 'PostgreSQL' ? `"${orig}"` : `\`${orig}\``
lines.push(`INSERT INTO ${ident} SELECT * FROM \`${HISTORY_BAK_DB}\`.\`${bak}\`;`)
}
}
if (m.bizBakEntries.length > 0) {
if (m.dbResult && m.dbResult.bizBakEntries.length > 0 && m.dbConn) {
lines.push('# 4. 还原业务库 (反向 RENAME):')
lines.push(`CREATE DATABASE IF NOT EXISTS \`${m.appCode}\`;`)
for (const entry of m.bizBakEntries) {
lines.push(`CREATE DATABASE ${m.dbConn.dbType === 'PostgreSQL' ? `"${m.appCode}"` : `\`${m.appCode}\``};`)
for (const entry of m.dbResult.bizBakEntries) {
const [orig, bak] = entry.split(' :: ')
lines.push(`RENAME TABLE \`${HISTORY_BAK_DB}\`.\`${bak}\` TO \`${m.appCode}\`.\`${orig}\`;`)
}
@@ -1047,7 +809,7 @@ function printCreateHint(): void {
function locateSelfCli(): string | null {
try {
const __filename = fileURLToPath(import.meta.url)
const dir = dirname(__filename) // .../src/commands/app
const dir = dirname(__filename)
const cliTs = join(dir, '..', '..', 'cli.ts')
if (existsSync(cliTs)) return cliTs
return null
+7 -5
View File
@@ -31,6 +31,7 @@ import { log } from '../../core/log.ts'
import { saveToken } from '../../core/config.ts'
import type { TokenFile } from '../../core/types.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
import { loadDbProfile } from '../../core/db-driver.ts'
import {
initWebapi as initSystemWebapi,
getSystemList,
@@ -142,12 +143,13 @@ export function buildAppCreateCommand(): Command {
log.ok(` ✓ 系统创建: systemId=${systemId}`)
}
// === step 2 创建 MySQL 数据库 (ssw-common /dblinkExt/createDatabase) ===
// === 2. 创建 MySQL 数据库 (ssw-common /dblinkExt/createDatabase) ===
// === step 2 创建 DB (ssw-common /dblinkExt/createDatabase) ===
// dbType 自动从 yml 的 spring.datasource.db-type 推断 (默认连接, 避免硬编码)
let dbLinkId = ''
if (!opts.skipDb) {
log.info(`[2/5] 创建 MySQL DB '${dbName}'...`)
const r2 = await c.postJson<boolean>('/common/dblinkExt/createDatabase', { dbName, dbType: 'MySQL' })
const dbType = loadDbProfile().dbType
log.info(`[2/5] 创建 ${dbType} DB '${dbName}'...`)
const r2 = await c.postJson<boolean>('/common/dblinkExt/createDatabase', { dbName, dbType })
if (r2.code === 200) {
log.ok(` ✓ DB '${dbName}' 创建/已存在`)
} else if (r2.code === 404) {
@@ -178,7 +180,7 @@ export function buildAppCreateCommand(): Command {
// 3.2 用 ssw-common dblinkExt/create (默认从 yml 读 host/port/user/password)
const body = {
fullName: opts.appCode,
dbType: 'MySQL',
dbType: loadDbProfile().dbType,
serviceName: dbName,
description: `${opts.appName} 数据源`,
}
+106
View File
@@ -0,0 +1,106 @@
/**
* core/db-driver.ts — yml DB profile 读取 (dacli v0.16+)
*
* 设计原则:
* - 只读 application-dev.yml 的 spring.datasource.*, 缓存到内存
* - 暴露 dbType / host / port / username / password / dbName / schema
* - **不直接连接 DB** — 所有 DB 操作走 ssw-common HTTP 端点
* - 这样 dacli 不需要装 mysql2/pg 驱动, 也不需要方言抽象
*
* 用法:
* import { loadDbProfile } from '../../core/db-driver'
* const profile = loadDbProfile() // 一次性读 yml
* c.postJson('/common/dblinkExt/createDatabase', { dbName, dbType: profile.dbType })
*
* 将来加 Oracle/DM/KingbaseES: 只改 ssw-common + 后端配置, dacli 不动
*/
import { existsSync, readFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { parse as yamlParse } from 'yaml'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
// =====================================================================
// 类型
// =====================================================================
export type DbType = 'MySQL' | 'PostgreSQL'
export interface DbProfile {
dbType: DbType
host: string
port: number
username: string
password: string
dbName: string // 主库 dbName (PG: database, MySQL: database)
schema?: string // PG 默认 schema, 默认 'public'; MySQL 忽略
}
// =====================================================================
// Profile 加载 (单例缓存)
// =====================================================================
let _cachedProfile: DbProfile | null = null
let _cachedYmlPath: string | null = null
function defaultYmlPath(): string {
// 工程根目录 = src/core 的上上上级
return resolve(__dirname, '..', '..', '..', 'boot', 'jnpf-admin', 'src', 'main', 'resources', 'application-dev.yml')
}
function nav(root: any, ...keys: string[]): any {
let cur = root
for (const k of keys) {
if (!cur || typeof cur !== 'object') return null
cur = cur[k]
}
return cur
}
/**
* 读 application-dev.yml, 返回 DB profile.
* 单例缓存, 同进程内多次调用只解析一次.
*
* @param ymlPath 可显式覆盖 (测试用), 默认走工程约定路径
*/
export function loadDbProfile(ymlPath?: string): DbProfile {
const p = ymlPath || _cachedYmlPath || defaultYmlPath()
if (_cachedProfile && p === _cachedYmlPath) return _cachedProfile
if (!existsSync(p)) {
throw new Error(`application-dev.yml 不存在: ${p}`)
}
let root: any
try {
root = yamlParse(readFileSync(p, 'utf-8'))
} catch (e) {
throw new Error(`解析 application-dev.yml 失败: ${(e as Error).message}`)
}
const ds = nav(root, 'spring', 'datasource')
if (!ds) throw new Error('yml 缺 spring.datasource 节点')
// db-type 大小写归一
const rawType = String(ds['db-type'] || 'MySQL')
const dbType: DbType = rawType.toLowerCase() === 'postgresql' ? 'PostgreSQL' : 'MySQL'
const profile: DbProfile = {
dbType,
host: String(ds.host),
port: Number(ds.port),
username: String(ds.username),
password: String(ds.password),
dbName: String(ds['db-name']),
schema: ds['db-schema'] ? String(ds['db-schema']) : undefined,
}
_cachedProfile = profile
_cachedYmlPath = p
return profile
}
/** 清缓存 (测试用) */
export function resetDbProfileCache(): void {
_cachedProfile = null
_cachedYmlPath = null
}