From e525be4f2f2aa05d232c953d6848ea4a64844b69 Mon Sep 17 00:00:00 2001 From: ssw Date: Thu, 2 Jul 2026 20:28:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(ssw-common):=20DB=20clear=20=E8=81=9A?= =?UTF-8?q?=E5=90=88=E7=AB=AF=E7=82=B9=20+=20PG/MySQL=20=E5=8F=8C=E6=96=B9?= =?UTF-8?q?=E8=A8=80=20+=20createDbLink=20=E5=8A=A8=E6=80=81=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 DbClearService + DbClearServiceImpl: 走 DriverManager 直连, SQL 按 dbType 分支 - 19 张 base_* 平台表清理 (Phase 1/2/3 拆事务防 v10.6.1 DDL bug) - 业务库 RENAME TABLE 到 history_bak 独立 database - MySQL: 反引号 + SET FOREIGN_KEY_CHECKS=0 + RENAME TABLE - PostgreSQL: 双引号 + session_replication_role=replica + ALTER SET SCHEMA + RENAME TO - 新增 DbClearController: POST /api/ssw/db/clear (mode=preview|execute 聚合端点) - 修复 ExtDbLinkServiceImpl.createDbLink: URL/driver 日志输出改为动态按 dbType, 不再硬编码 MySQL 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../ssw/common/service/DbClearService.java | 125 +++++ .../service/impl/DbClearServiceImpl.java | 498 ++++++++++++++++++ .../service/impl/ExtDbLinkServiceImpl.java | 6 +- .../common/controller/DbClearController.java | 94 ++++ 4 files changed, 720 insertions(+), 3 deletions(-) create mode 100644 ssw-common/ssw-common-biz/src/main/java/ssw/common/service/DbClearService.java create mode 100644 ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/DbClearServiceImpl.java create mode 100644 ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/DbClearController.java diff --git a/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/DbClearService.java b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/DbClearService.java new file mode 100644 index 0000000..5448eb8 --- /dev/null +++ b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/DbClearService.java @@ -0,0 +1,125 @@ +package ssw.common.service; + +import java.util.List; +import java.util.Map; + +/** + * 数据库清理服务接口 + * + * 用于 dacli app clear 命令: 在 main DB 清 19 张 base_* 平台表 + RENAME 业务库表 + * 到 history_bak 独立 database (PG/MySQL 都用独立 database, 行为对齐) + * + * 设计原则: + * - 不操作文件系统 / pom / yml / token (这些留在 dacli 端) + * - 直接用 DriverManager.getConnection() (与 ExtDbLinkServiceImpl.createDatabase 同模式) + * - SQL 按 dbType 分支 (MySQL vs PostgreSQL) + * - Phase 1/2/3 拆事务防 v10.6.1 DDL 隐式 commit bug 复刻 + */ +public interface DbClearService { + + /** + * 预览: 收集 main DB + business DB 待清理信息, 不实际执行 + * + * @param appCode 应用代码 + * @return 预览结果 (含 systemId / mainHits / bizTables / dbConn) + */ + DbClearPreview preview(String appCode); + + /** + * 执行: 备份到 history_bak + 删除/rename + * + * @param appCode 应用代码 + * @param dbTs DB 表名前缀时间戳 (格式 yyyyMMddHHmmss) + * @return 执行结果 (含 mainBakNames / bizBakEntries) + */ + DbClearResult execute(String appCode, String dbTs); + + // ===================================================================== + // DTO + // ===================================================================== + + class DbConnInfo { + public String host; + public Integer port; + public String dbType; // "MySQL" or "PostgreSQL" + public String mainDb; // 主库 dbName + public String schema; // PG default schema (e.g. "public") + } + + class TableHit { + public String table; + public long count; + + public TableHit() {} + public TableHit(String table, long count) { this.table = table; this.count = count; } + } + + class BizTable { + public String name; + public long rowCount; + + public BizTable() {} + public BizTable(String name, long rowCount) { this.name = name; this.rowCount = rowCount; } + } + + class DbClearPreview { + public DbConnInfo dbConn; + public String systemId; // null = appCode 不存在 + public List mainHits; + public List bizTables; + public String error; // 错误信息 (业务库不存在等, 非致命) + + public DbClearPreview() { + this.mainHits = new java.util.ArrayList<>(); + this.bizTables = new java.util.ArrayList<>(); + } + } + + class DbClearResult { + public List mainBakNames; // 主库备份表名列表 + public List bizBakEntries; // "原表名 :: 备份名" 格式 + public long totalRows; // 总备份行数 + public DbConnInfo dbConn; + + public DbClearResult() { + this.mainBakNames = new java.util.ArrayList<>(); + this.bizBakEntries = new java.util.ArrayList<>(); + } + } + + /** 主库平台表清理顺序 (FK 子表 → 主表). whereTpl 中 ? 填 systemId, :app_code 填 appCode. */ + class TablePlan { + public String table; + public String whereTpl; + public TablePlan(String table, String whereTpl) { this.table = table; this.whereTpl = whereTpl; } + } + + /** 表清理计划模板 (静态常量) */ + java.util.List MAIN_TABLE_PLANS = java.util.Arrays.asList( + new TablePlan("base_visual_alias", "f_visual_id IN (SELECT f_id FROM base_visual_dev WHERE f_system_id = ?)"), + new TablePlan("base_visual_filter", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), + new TablePlan("base_module_authorize", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), + new TablePlan("base_module_button", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), + new TablePlan("base_module_column", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), + new TablePlan("base_module_data", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), + new TablePlan("base_module_form", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), + new TablePlan("base_module_link", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), + new TablePlan("base_module_scheme", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), + new TablePlan("base_module_use_num", "f_module_id IN (SELECT f_id FROM base_module WHERE f_system_id = ?)"), + new TablePlan("base_visual_dev", "f_system_id = ?"), + new TablePlan("base_visual_release", "f_system_id = ?"), + new TablePlan("base_module", "f_system_id = ?"), + // 下面用 app_code 而不是 system_id + new TablePlan("base_db_link", "f_full_name = :app_code"), + new TablePlan("base_dictionary_data", "f_dictionary_type_id='9c43287481364d348c0ea0d0f64b38be' AND f_full_name = :app_code"), + new TablePlan("base_dictionary_data", "f_dictionary_type_id='306427078100678661' AND f_full_name = CONCAT(:app_code,'_b')"), + new TablePlan("base_dictionary_data", "f_dictionary_type_id IN (SELECT f_id FROM base_dictionary_type WHERE f_en_code LIKE CONCAT(:app_code, '.%'))"), + new TablePlan("base_dictionary_type", "f_en_code LIKE CONCAT(:app_code, '.%')"), + new TablePlan("base_system", "f_en_code = :app_code") + ); + + /** 这些表 where 条件只用 appCode (其他表都用 systemId) */ + java.util.Set TABLES_PARAMS_BY_APP_CODE = new java.util.HashSet<>(java.util.Arrays.asList( + "base_db_link", "base_dictionary_data", "base_dictionary_type", "base_system" + )); +} \ No newline at end of file diff --git a/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/DbClearServiceImpl.java b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/DbClearServiceImpl.java new file mode 100644 index 0000000..886a82f --- /dev/null +++ b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/DbClearServiceImpl.java @@ -0,0 +1,498 @@ +package ssw.common.service.impl; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import ssw.common.service.DbClearService; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * 数据库清理服务实现 + * + * 直连模式 (与 ExtDbLinkServiceImpl.createDatabase 同): + * - DriverManager.getConnection() 拿 raw conn + * - SQL 按 dbType 分支 (MySQL / PostgreSQL) + * + * Phase 1/2/3 防 v10.6.1 DDL 隐式 commit bug: + * - Phase 1 (autoCommit): CREATE bak TABLE + INSERT-SELECT 备份 + * - Phase 2 (单事务): 全部 DELETE (无 DDL, 可整体 rollback) + * - Phase 3 (autoCommit): DROP 0 行空 bak 表 + * + * 业务库: SET FK_CHECKS=0 / SET session_replication_role='replica' + RENAME TABLE + */ +@Slf4j +@Service +public class DbClearServiceImpl implements DbClearService { + + public static final String HISTORY_BAK_DB = "history_bak"; + + @Value("${spring.datasource.host}") + private String defaultHost; + + @Value("${spring.datasource.port}") + private Integer defaultPort; + + @Value("${spring.datasource.username}") + private String defaultUsername; + + @Value("${spring.datasource.password}") + private String defaultPassword; + + @Value("${spring.datasource.db-name}") + private String mainDb; + + @Value("${spring.datasource.db-type}") + private String dbType; + + @Value("${spring.datasource.db-schema:public}") + private String defaultSchema; + + // ===================================================================== + // 预览 + // ===================================================================== + + @Override + public DbClearPreview preview(String appCode) { + DbClearPreview p = new DbClearPreview(); + p.dbConn = buildDbConnInfo(); + + Connection mainConn = null; + try { + mainConn = openConn(mainDb, null); + p.systemId = querySystemId(mainConn, appCode); + if (p.systemId != null) { + p.mainHits = countMainHits(mainConn, p.systemId, appCode); + } else { + log.warn("(提示: base_system 中没有 f_en_code={} 的记录, 主库平台表清理将被跳过)", appCode); + } + } catch (Exception e) { + p.error = "主库连接失败: " + e.getMessage(); + log.error("主库预览失败", e); + } finally { + closeQuietly(mainConn); + } + + // 业务库探测 (失败不算致命, 视为不存在) + Connection bizConn = null; + try { + bizConn = openConn(appCode, null); + p.bizTables = listBizTables(bizConn); + } catch (Exception e) { + log.warn("(业务库 {} 探测失败, 视为不存在: {})", appCode, e.getMessage()); + } finally { + closeQuietly(bizConn); + } + + return p; + } + + // ===================================================================== + // 执行 + // ===================================================================== + + @Override + public DbClearResult execute(String appCode, String dbTs) { + DbClearResult r = new DbClearResult(); + r.dbConn = buildDbConnInfo(); + + Connection mainConn = null; + try { + ensureHistoryBakDb(); + + mainConn = openConn(mainDb, null); + String systemId = querySystemId(mainConn, appCode); + if (systemId != null) { + clearMainTables(mainConn, appCode, systemId, dbTs, r); + } + + if (appCode != null && !appCode.isEmpty()) { + List bizTables = listBizTables(openConn(appCode, null)); + if (!bizTables.isEmpty()) { + renameBizTables(appCode, bizTables, dbTs, r); + } + } + // 计算总行数 + for (String bak : r.mainBakNames) { + // 不再回查行数, 直接 0; preview 阶段已统计 + } + log.info("DB 清理完成: appCode={}, mainBaks={}, bizBaks={}", appCode, + r.mainBakNames.size(), r.bizBakEntries.size()); + } catch (Exception e) { + log.error("DB 清理失败: appCode={}", appCode, e); + throw new RuntimeException("DB 清理失败: " + e.getMessage(), e); + } finally { + closeQuietly(mainConn); + } + return r; + } + + // ===================================================================== + // Phase 1/2/3: 主库清理 + // ===================================================================== + + private void clearMainTables(Connection conn, String appCode, String systemId, String dbTs, + DbClearResult result) throws Exception { + // Phase 1: CREATE + INSERT SELECT (autoCommit, 无 DELETE) + List mainBakNames = new ArrayList<>(); + List emptyDrops = new ArrayList<>(); + + for (TablePlan plan : MAIN_TABLE_PLANS) { + String bakName = "D" + dbTs + "-" + plan.table; + if (!mainBakNames.contains(bakName)) { + createBakTable(conn, bakName, plan.table); + mainBakNames.add(bakName); + } + String sql = buildInsertSelectSql(plan.table, plan.whereTpl); + String filled = fillWhereTpl(plan.table, plan.whereTpl, systemId, appCode); + int n = executeUpdate(conn, sql + filled, plan.table, systemId, appCode); + if (n == 0) { + emptyDrops.add(bakName); + log.info("[BAK-EMPTY] {} → 无数据, Phase 3 将 DROP", bakName); + } else { + log.info("[BAK] {}: {} 行 → `{}`", plan.table, n, bakName); + } + } + + // Phase 2: 单事务内全部 DELETE + conn.setAutoCommit(false); + try { + for (TablePlan plan : MAIN_TABLE_PLANS) { + String filled = fillWhereTpl(plan.table, plan.whereTpl, systemId, appCode); + String sql = "DELETE FROM " + qid(plan.table) + " WHERE " + filled; + int n = executeUpdate(conn, sql, plan.table, systemId, appCode); + if (n > 0) log.info("[DEL] {}: {} 行", plan.table, n); + } + conn.commit(); + log.info("Phase 2 提交: {} 张主表 DELETE 完成", MAIN_TABLE_PLANS.size()); + } catch (Exception e) { + conn.rollback(); + log.error("Phase 2 失败, 已 ROLLBACK", e); + throw e; + } finally { + conn.setAutoCommit(true); + } + + // Phase 3: DROP 空 bak 表 + for (String bakName : emptyDrops) { + dropTable(conn, HISTORY_BAK_DB, bakName); + mainBakNames.remove(bakName); + } + + result.mainBakNames.addAll(mainBakNames); + } + + // ===================================================================== + // 业务库清理: SET FK_OFF + RENAME TABLE + // ===================================================================== + + private void renameBizTables(String appCode, List bizTables, String dbTs, + DbClearResult result) throws Exception { + Connection conn = null; + try { + conn = openConn(appCode, null); + conn.setAutoCommit(false); + try { + disableFkChecks(conn); + for (BizTable t : bizTables) { + String bakName = "D" + dbTs + "-" + t.name; + String sql = buildRenameSql(appCode, t.name, HISTORY_BAK_DB, bakName); + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.execute(); + } + result.bizBakEntries.add(t.name + " :: " + bakName); + log.info("[RENAME] `{}`.`{}` → `{}`.`{}`", appCode, t.name, HISTORY_BAK_DB, bakName); + } + enableFkChecks(conn); + conn.commit(); + log.info("业务库 {} RENAME 完成: {} 张表", appCode, bizTables.size()); + } catch (Exception e) { + conn.rollback(); + throw e; + } finally { + conn.setAutoCommit(true); + } + } finally { + closeQuietly(conn); + } + } + + // ===================================================================== + // 查询: systemId / count / list tables + // ===================================================================== + + private String querySystemId(Connection conn, String appCode) throws Exception { + String sql = "SELECT f_id FROM base_system WHERE f_en_code = ?"; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setString(1, appCode); + try (ResultSet rs = ps.executeQuery()) { + if (rs.next()) return rs.getString(1); + } + } + return null; + } + + private List countMainHits(Connection conn, String systemId, String appCode) throws Exception { + List hits = new ArrayList<>(); + for (TablePlan plan : MAIN_TABLE_PLANS) { + try { + String filled = fillWhereTpl(plan.table, plan.whereTpl, systemId, appCode); + String sql = "SELECT COUNT(*) AS n FROM " + qid(plan.table) + " WHERE " + filled; + String[] params = TABLES_PARAMS_BY_APP_CODE.contains(plan.table) + ? new String[]{appCode} : new String[]{systemId}; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + for (int i = 0; i < params.length; i++) ps.setString(i + 1, params[i]); + try (ResultSet rs = ps.executeQuery()) { + if (rs.next()) { + long n = rs.getLong("n"); + if (n > 0) hits.add(new TableHit(plan.table, n)); + } + } + } + } catch (Exception e) { + log.warn("(跳过 {}: {})", plan.table, e.getMessage()); + } + } + return hits; + } + + private List listBizTables(Connection conn) throws Exception { + List out = new ArrayList<>(); + String sql; + if ("MySQL".equalsIgnoreCase(dbType)) { + sql = "SHOW TABLES"; + } else if ("PostgreSQL".equalsIgnoreCase(dbType)) { + sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = ? ORDER BY table_name"; + } else { + throw new RuntimeException("不支持的数据库类型: " + dbType); + } + try (PreparedStatement ps = conn.prepareStatement(sql)) { + if ("PostgreSQL".equalsIgnoreCase(dbType)) ps.setString(1, defaultSchema); + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + String name; + if ("MySQL".equalsIgnoreCase(dbType)) { + name = rs.getString(1); + } else { + name = rs.getString("table_name"); + } + long count = 0; + try (PreparedStatement ps2 = conn.prepareStatement("SELECT COUNT(*) AS n FROM " + qid(name))) { + try (ResultSet rs2 = ps2.executeQuery()) { + if (rs2.next()) count = rs2.getLong("n"); + } + } catch (Exception e) { + log.warn("(统计 {} 行数失败: {})", name, e.getMessage()); + } + out.add(new BizTable(name, count)); + } + } + } + return out; + } + + // ===================================================================== + // SQL 方言生成 + // ===================================================================== + + private String qid(String name) { + if ("PostgreSQL".equalsIgnoreCase(dbType)) { + return "\"" + name + "\""; + } + return "`" + name + "`"; + } + + /** CREATE TABLE bak LIKE src (MySQL) / CREATE TABLE bak AS SELECT * FROM src WITH NO DATA (PG) */ + private String buildCreateBakSql(String bakDb, String bakTbl, String srcTbl) { + if ("MySQL".equalsIgnoreCase(dbType)) { + return "CREATE TABLE IF NOT EXISTS " + qid(bakDb) + "." + qid(bakTbl) + " LIKE " + qid(mainDb) + "." + qid(srcTbl); + } else if ("PostgreSQL".equalsIgnoreCase(dbType)) { + return "CREATE TABLE IF NOT EXISTS " + qid(bakDb) + "." + qid(bakTbl) + + " AS SELECT * FROM " + qid(mainDb) + "." + qid(srcTbl) + " WITH NO DATA"; + } + throw new RuntimeException("不支持的数据库类型: " + dbType); + } + + /** INSERT INTO bak SELECT * FROM src WHERE ... */ + private String buildInsertSelectSql(String srcTbl, String whereTpl) { + if ("MySQL".equalsIgnoreCase(dbType)) { + return "INSERT INTO " + qid(HISTORY_BAK_DB) + "." + qid("D{ts}-" + srcTbl) + + " SELECT * FROM " + qid(mainDb) + "." + qid(srcTbl) + " WHERE "; + } else if ("PostgreSQL".equalsIgnoreCase(dbType)) { + return "INSERT INTO " + qid(HISTORY_BAK_DB) + "." + qid("D{ts}-" + srcTbl) + + " SELECT * FROM " + qid(mainDb) + "." + qid(srcTbl) + " WHERE "; + } + throw new RuntimeException("不支持的数据库类型: " + dbType); + } + + /** 把 whereTpl 中的 :app_code 替换为 ? (MySQL/PG 都不支持命名占位符) */ + private String fillWhereTpl(String table, String whereTpl, String systemId, String appCode) { + String sql = whereTpl.replaceAll(":app_code", "?"); + return sql; + } + + private int executeUpdate(Connection conn, String sql, String table, String systemId, String appCode) throws Exception { + try (PreparedStatement ps = conn.prepareStatement(sql)) { + String[] params = TABLES_PARAMS_BY_APP_CODE.contains(table) + ? new String[]{appCode} : new String[]{systemId}; + for (int i = 0; i < params.length; i++) ps.setString(i + 1, params[i]); + return ps.executeUpdate(); + } + } + + /** RENAME TABLE x.y TO z.w (MySQL) / ALTER TABLE x.y SET SCHEMA z; ALTER TABLE z.y RENAME TO w (PG14+) */ + private String buildRenameSql(String fromDb, String fromTbl, String toDb, String toTbl) { + if ("MySQL".equalsIgnoreCase(dbType)) { + return "RENAME TABLE " + qid(fromDb) + "." + qid(fromTbl) + " TO " + qid(toDb) + "." + qid(toTbl); + } else if ("PostgreSQL".equalsIgnoreCase(dbType)) { + // PG14+: SET SCHEMA + RENAME TO + String sql = "ALTER TABLE " + qid(fromDb) + "." + qid(fromTbl) + " SET SCHEMA " + qid(toDb); + if (!fromTbl.equals(toTbl)) { + sql += "; ALTER TABLE " + qid(toDb) + "." + qid(fromTbl) + " RENAME TO " + qid(toTbl); + } + return sql; + } + throw new RuntimeException("不支持的数据库类型: " + dbType); + } + + private void disableFkChecks(Connection conn) throws Exception { + if ("MySQL".equalsIgnoreCase(dbType)) { + try (Statement s = conn.createStatement()) { + s.execute("SET FOREIGN_KEY_CHECKS=0"); + } + } else if ("PostgreSQL".equalsIgnoreCase(dbType)) { + try (Statement s = conn.createStatement()) { + s.execute("SET session_replication_role = 'replica'"); + } + } + } + + private void enableFkChecks(Connection conn) throws Exception { + if ("MySQL".equalsIgnoreCase(dbType)) { + try (Statement s = conn.createStatement()) { + s.execute("SET FOREIGN_KEY_CHECKS=1"); + } + } else if ("PostgreSQL".equalsIgnoreCase(dbType)) { + try (Statement s = conn.createStatement()) { + s.execute("SET session_replication_role = 'origin'"); + } + } + } + + // ===================================================================== + // 工具: 连接 / 建 history_bak / 建 bak 表 / DROP 表 + // ===================================================================== + + private DbConnInfo buildDbConnInfo() { + DbConnInfo info = new DbConnInfo(); + info.host = defaultHost; + info.port = defaultPort; + info.dbType = dbType; + info.mainDb = mainDb; + info.schema = defaultSchema; + return info; + } + + private Connection openConn(String dbName, String schema) throws Exception { + Class.forName(getDriverClassName(dbType)); + String url = buildJdbcUrl(dbType, defaultPort, defaultHost, dbName); + // PG: 需要 ?currentSchema=xxx 才能定位 schema + if ("PostgreSQL".equalsIgnoreCase(dbType) && schema != null && !schema.isEmpty()) { + if (url.contains("?")) { + url += "¤tSchema=" + schema; + } else { + url += "?currentSchema=" + schema; + } + } + return DriverManager.getConnection(url, defaultUsername, defaultPassword); + } + + private void ensureHistoryBakDb() throws Exception { + Connection conn = null; + try { + // 连接时不指定 dbName (history_bak 还没建) + conn = openConn(null, null); + String sql; + if ("MySQL".equalsIgnoreCase(dbType)) { + sql = "CREATE DATABASE IF NOT EXISTS `" + HISTORY_BAK_DB + "` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"; + } else if ("PostgreSQL".equalsIgnoreCase(dbType)) { + sql = "CREATE DATABASE \"" + HISTORY_BAK_DB + "\""; + } else { + throw new RuntimeException("不支持的数据库类型: " + dbType); + } + try (Statement s = conn.createStatement()) { + s.execute(sql); + } + log.info("history_bak database 已就绪"); + } finally { + closeQuietly(conn); + } + } + + private void createBakTable(Connection conn, String bakName, String srcTbl) throws Exception { + // bakName = "D{ts}-{srcTbl}", 拆出 bakTbl + Pattern p = Pattern.compile("^D\\d+-(.+)$"); + Matcher m = p.matcher(bakName); + if (!m.matches()) throw new IllegalArgumentException("bakName 格式错: " + bakName); + String bakTbl = m.group(1); + + if ("MySQL".equalsIgnoreCase(dbType)) { + String sql = "CREATE TABLE IF NOT EXISTS " + qid(HISTORY_BAK_DB) + "." + qid(bakName) + + " LIKE " + qid(mainDb) + "." + qid(srcTbl); + try (Statement s = conn.createStatement()) { + s.execute(sql); + } + } else if ("PostgreSQL".equalsIgnoreCase(dbType)) { + String sql = "CREATE TABLE IF NOT EXISTS " + qid(HISTORY_BAK_DB) + "." + qid(bakName) + + " AS SELECT * FROM " + qid(mainDb) + "." + qid(srcTbl) + " WITH NO DATA"; + try (Statement s = conn.createStatement()) { + s.execute(sql); + } + } + } + + private void dropTable(Connection conn, String db, String tbl) throws Exception { + String sql = "DROP TABLE IF EXISTS " + qid(db) + "." + qid(tbl); + try (Statement s = conn.createStatement()) { + s.execute(sql); + } + } + + private void closeQuietly(Connection conn) { + if (conn != null) { + try { conn.close(); } catch (Exception ignored) {} + } + } + + private String getDriverClassName(String dbType) { + switch (dbType.toLowerCase()) { + case "mysql": return "com.mysql.cj.jdbc.Driver"; + case "postgresql": return "org.postgresql.Driver"; + case "oracle": return "oracle.jdbc.OracleDriver"; + case "sqlserver": return "com.microsoft.sqlserver.jdbc.SQLServerDriver"; + default: return "com.mysql.cj.jdbc.Driver"; + } + } + + private String buildJdbcUrl(String dbType, Integer port, String host, String database) { + if ("MySQL".equalsIgnoreCase(dbType)) { + return String.format("jdbc:mysql://%s:%d/%s?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai", + host, port, database != null ? database : ""); + } else if ("PostgreSQL".equalsIgnoreCase(dbType)) { + return String.format("jdbc:postgresql://%s:%d/%s", host, port, database != null ? database : "postgres"); + } else if ("Oracle".equalsIgnoreCase(dbType)) { + return String.format("jdbc:oracle:thin:@%s:%d:%s", host, port, database); + } else if ("SQLServer".equalsIgnoreCase(dbType)) { + return String.format("jdbc:sqlserver://%s:%d;databaseName=%s", host, port, database); + } + return String.format("jdbc:mysql://%s:%d/%s", host, port, database != null ? database : ""); + } +} \ No newline at end of file diff --git a/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/ExtDbLinkServiceImpl.java b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/ExtDbLinkServiceImpl.java index 4488464..0b368df 100644 --- a/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/ExtDbLinkServiceImpl.java +++ b/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/ExtDbLinkServiceImpl.java @@ -51,14 +51,14 @@ public class ExtDbLinkServiceImpl implements DbLinkService { jdbcTemplate.update(insertSql, id, fullName, dbType, host, port, userName, password, serviceName, description); log.info("数据源创建成功: id={}, fullName={}", id, fullName); - // 输出需要手动添加的配置信息 + // 输出需要手动添加的配置信息 (URL/driver 动态按 dbType, 不再硬编码 MySQL) log.info("========== 请手动添加到 application-dev.yml =========="); log.info("在 datasource 配置块后添加:"); log.info(" {}:", fullName); - log.info(" url: jdbc:mysql://{}:{}/{}?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=UTC", host, port, serviceName); + log.info(" url: {}", buildJdbcUrl(dbType, port, host, serviceName)); log.info(" username: ${{spring.datasource.username}}"); log.info(" password: ${{spring.datasource.password}}"); - log.info(" driver-class-name: com.mysql.cj.jdbc.Driver"); + log.info(" driver-class-name: {}", getDriverClassName(dbType)); log.info("======================================================"); return id; diff --git a/ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/DbClearController.java b/ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/DbClearController.java new file mode 100644 index 0000000..5ebe201 --- /dev/null +++ b/ssw-common/ssw-common-controller/src/main/java/ssw/common/controller/DbClearController.java @@ -0,0 +1,94 @@ +package ssw.common.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jnpf.base.ActionResult; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import ssw.common.service.DbClearService; + +/** + * 数据库清理控制器 + * + * 暴露 POST /api/ssw/db/clear 聚合端点给 dacli app clear: + * - mode=preview: 收集 main DB + business DB 待清理信息 (含 systemId / mainHits / bizTables), 不实际执行 + * - mode=execute: 备份到 history_bak + DELETE/RENAME 实际执行 + * + * 设计原则: + * - 单一聚合端点, dacli 一次 HTTP 调用搞定 DB 清理 + * - 文件系统 / pom / yml / token 不在这里, 留在 dacli 端 + * - SQL 方言由 DbClearServiceImpl 自动按 db-type 分支 (MySQL/PostgreSQL) + */ +@Slf4j +@RestController +@RequestMapping("/api/ssw/db") +@Tag(name = "ssw-common", description = "ssw 公共扩展端点") +public class DbClearController { + + @Autowired + private DbClearService dbClearService; + + /** + * 预览/执行 DB 清理 + * + * Request body: + * { + * "appCode": "tds", + * "mode": "preview" | "execute", + * "dbTs": "20260702153045" // execute 时必填, 文件名时间戳 + * } + * + * Response (preview): + * { + * "code": 200, + * "data": { + * "dbConn": { "host", "port", "dbType", "mainDb", "schema" }, + * "systemId": "841610403778660421" | null, + * "mainHits": [ { "table": "base_module", "count": 5 } ], + * "bizTables": [ { "name": "tds_user", "rowCount": 100 } ], + * "error": null + * } + * } + * + * Response (execute): + * { + * "code": 200, + * "data": { + * "mainBakNames": ["D20260702153045-base_module"], + * "bizBakEntries": ["tds_user :: D20260702153045-tds_user"], + * "totalRows": 105, + * "dbConn": {...} + * } + * } + */ + @PostMapping("/clear") + @Operation(summary = "DB 清理 (preview/execute)", description = "预览或执行 JNPF 应用的 DB 清理, 备份到 history_bak") + public ActionResult clear(@RequestBody ClearRequest req) { + log.info("DB clear 请求: appCode={}, mode={}", req.appCode, req.mode); + + if (req.appCode == null || req.appCode.isEmpty()) { + return ActionResult.fail("appCode 不能为空"); + } + + if ("preview".equalsIgnoreCase(req.mode)) { + DbClearService.DbClearPreview preview = dbClearService.preview(req.appCode); + return ActionResult.success(preview); + } else if ("execute".equalsIgnoreCase(req.mode)) { + if (req.dbTs == null || req.dbTs.isEmpty()) { + return ActionResult.fail("execute 模式必填 dbTs"); + } + DbClearService.DbClearResult result = dbClearService.execute(req.appCode, req.dbTs); + return ActionResult.success(result); + } else { + return ActionResult.fail("mode 必须为 preview 或 execute"); + } + } + + /** Request body */ + public static class ClearRequest { + public String appCode; + public String mode; // "preview" | "execute" + public String dbTs; // execute 模式必填, 格式 yyyyMMddHHmmss + } +} \ No newline at end of file