auto(14b5ea1b9bd9): session end @ 2026-07-23T19:20:56 (+3f)

This commit is contained in:
ssw committed 2026-07-23 19:20:57 +08:00
1 parent c948fc33b2
commit 05b8696a5a
3 files changed
+201 -1

No files matched your search

@@ -58,6 +58,11 @@ public interface DbClearService {
public boolean includeSchedule; // base_schedule 等 3 张
public boolean includeDict; // base_dictionary_type + data
public boolean includeBiz; // 业务库 RENAME
// P1-2e (2026-07-23): ghost 模式 (跨 systemId 残留清理)
// 应用被删再重建时, 旧 systemId 的 workflow/di/portal/print/report/schedule 记录残留
// 旧 enCode 占用导致重建后 "Encoding cannot be repeated"
// 开启后扫描 systemId NOT IN (current systemId) 且 enCode LIKE appCode% 的所有记录
public boolean includeGhost;
public DbClearScope() {
this.includeWorkflow = true;
@@ -68,6 +73,7 @@ public interface DbClearService {
this.includeSchedule = true;
this.includeDict = true;
this.includeBiz = true;
this.includeGhost = false; // 默认关 (高危: 跨 systemId 清, 慎用)
}
/** 全开 (默认行为) */
@@ -87,6 +93,13 @@ public interface DbClearService {
s.includeBiz = false;
return s;
}
/** ghost 模式 (P1-2e 2026-07-23): 默认全开 + includeGhost=true */
public static DbClearScope withGhost() {
DbClearScope s = all();
s.includeGhost = true;
return s;
}
}
// =====================================================================
@@ -131,6 +144,8 @@ public interface DbClearService {
public List<TableHit> reportHits; // report_template
public List<TableHit> scheduleHits; // base_schedule + 子表
public List<TableHit> dictHits; // base_dictionary_type + base_dictionary_data
// P1-2e: ghost 残留 (跨 systemId, 旧 enCode 占用)
public List<TableHit> ghostHits; // 跨 systemId 同 enCode 残留, 默认不清理, --include-ghost 开启
public DbClearPreview() {
this.mainHits = new java.util.ArrayList<>();
@@ -142,6 +157,7 @@ public interface DbClearService {
this.reportHits = new java.util.ArrayList<>();
this.scheduleHits = new java.util.ArrayList<>();
this.dictHits = new java.util.ArrayList<>();
this.ghostHits = new java.util.ArrayList<>();
}
}
@@ -250,4 +266,47 @@ public interface DbClearService {
"base_data_interface", "base_data_interface_log", "base_data_interface_oauth",
"base_data_interface_user", "base_data_interface_variate"
));
/**
* P1-2e (2026-07-23): GHOST 清理计划
* 跨 systemId 残留: 应用被删再重建时, 旧 systemId 的 workflow/di/portal/print/report/schedule 记录
* 旧 enCode 占用导致重建后 "Encoding cannot be repeated" 报错
*
* whereTpl 用 :app_code 和 :current_system_id 两个占位符:
* :app_code → appCode (用于 enCode 匹配)
* :current_system_id → systemId (排除当前活跃系统, 只清旧的"幽灵"系统)
*
* 模板按 FK 子表 → 主表 顺序排列, 跟 MAIN_TABLE_PLANS 一致
*/
java.util.List<TablePlan> GHOST_PLANS = java.util.Arrays.asList(
// -------- workflow ghost --------
new TablePlan("workflow_form_relation", "f_flow_id IN (SELECT f_id FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id)"),
new TablePlan("workflow_node", "f_flow_id IN (SELECT f_id FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id)"),
new TablePlan("workflow_common", "f_flow_id IN (SELECT f_id FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id)"),
new TablePlan("workflow_delegate_info", "f_delegate_id IN (SELECT f_id FROM workflow_delegate WHERE f_flow_id IN (SELECT f_id FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_delegate", "f_flow_id IN (SELECT f_id FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id)"),
new TablePlan("workflow_template_use_num", "f_template_id IN (SELECT f_id FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id)"),
new TablePlan("workflow_version", "f_template_id IN (SELECT f_id FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id)"),
new TablePlan("workflow_trigger_launchflow", "f_trigger_id IN (SELECT f_id FROM workflow_trigger_task WHERE f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id)))"),
new TablePlan("workflow_trigger_record", "f_trigger_id IN (SELECT f_id FROM workflow_trigger_task WHERE f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id)))"),
new TablePlan("workflow_trigger_task", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_event_log", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_node_record", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_task_line", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_comment", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_candidates", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_circulate", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_operator", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_revoke", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_record", "f_task_id IN (SELECT f_id FROM workflow_task WHERE f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id))"),
new TablePlan("workflow_task", "f_flow_code IN (SELECT f_en_code FROM workflow_template WHERE f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id)"),
new TablePlan("workflow_template", "f_en_code LIKE CONCAT(:app_code, '%') AND f_system_id != :current_system_id"),
// -------- DI ghost (DI 表无 systemId, 靠 enCode 匹配 + 全平台共享性, 慎用) --------
new TablePlan("base_data_interface", "f_en_code LIKE CONCAT(:app_code, '%') AND f_creator_user_id IS NOT NULL")
);
/** GHOST 模式下, 哪些表用 (appCode, currentSystemId) 双占位符 (其它只用 appCode) */
java.util.Set<String> GHOST_PARAMS_BY_APP_CODE = new java.util.HashSet<>(java.util.Arrays.asList(
"base_data_interface"
));
}
@@ -82,6 +82,13 @@ public class DbClearServiceImpl implements DbClearService {
} else {
log.warn("(提示: base_system 中没有 f_en_code={} 的记录, 主库平台表清理将被跳过)", appCode);
}
// P1-2e: ghost 模式预览 (跨 systemId 残留)
// 不需要 systemId 当前存在, 但要能查 base_system 确认当前活跃 systemId (用于排除)
if (p.systemId != null) {
p.ghostHits = countGhostHits(mainConn, p.systemId, appCode);
} else {
p.ghostHits = countGhostHits(mainConn, null, appCode);
}
} catch (Exception e) {
p.error = "主库连接失败: " + e.getMessage();
log.error("主库预览失败", e);
@@ -110,6 +117,42 @@ public class DbClearServiceImpl implements DbClearService {
return out;
}
/**
* P1-2e (2026-07-23): 统计 ghost 残留
* 扫描 f_en_code LIKE 'appCode%' 但 f_system_id != currentSystemId 的记录
* (DI 表无 systemId, 用 f_creator_user_id IS NOT NULL 兜底)
* 返回按 table 分组的 hits
*/
private List<TableHit> countGhostHits(Connection conn, String currentSystemId, String appCode) {
List<TableHit> out = new java.util.ArrayList<>();
for (TablePlan plan : GHOST_PLANS) {
try {
String filled = fillGhostWhereTpl(plan.whereTpl, appCode, currentSystemId);
String sql = "SELECT COUNT(*) AS n FROM " + qid(plan.table) + " WHERE " + filled;
try (PreparedStatement ps = conn.prepareStatement(sql)) {
// ghost plan 全用 appCode (DI 也用 appCode)
ps.setString(1, appCode);
if (!GHOST_PARAMS_BY_APP_CODE.contains(plan.table)) {
ps.setString(2, currentSystemId == null ? "" : currentSystemId);
}
try (ResultSet rs = ps.executeQuery()) {
if (rs.next() && rs.getLong("n") > 0) {
out.add(new TableHit(plan.table, rs.getLong("n")));
}
}
}
} catch (Exception e) {
log.warn("(ghost 跳过 {}: {})", plan.table, e.getMessage());
}
}
return out;
}
/** P1-2e: ghost plan 占位符替换 (:app_code → ?, :current_system_id → ?) */
private String fillGhostWhereTpl(String whereTpl, String appCode, String systemId) {
return whereTpl.replaceAll(":app_code", "?").replaceAll(":current_system_id", "?");
}
// =====================================================================
// 执行
// =====================================================================
@@ -146,6 +189,12 @@ public class DbClearServiceImpl implements DbClearService {
renameBizTables(appCode, bizTables, dbTs, r);
}
}
// P1-2e: ghost 模式 (跨 systemId 残留, 高危, 默认 false)
if (scope.includeGhost && appCode != null && !appCode.isEmpty()) {
log.warn("⚠️ GHOST 模式开启: 将清理 f_en_code LIKE '{}%' 但 f_system_id != current 的所有记录 (含历史已删应用的 enCode 残留)", appCode);
clearGhostTables(mainConn, appCode, systemId, dbTs, r);
}
log.info("DB 清理完成: appCode={}, mainBaks={}, bizBaks={}", appCode,
r.mainBakNames.size(), r.bizBakEntries.size());
} catch (Exception e) {
@@ -217,7 +266,6 @@ public class DbClearServiceImpl implements DbClearService {
for (TablePlan plan : plans) {
String filled = fillWhereTpl(plan.table, plan.whereTpl, systemId, appCode);
String sql = "DELETE FROM " + qid(plan.table) + " WHERE " + filled;
// P1-2e: 调试时改 ERROR 级别进 log_error, 生产可改回 INFO; 留 log.info 看 SQL 便于排查
int n = executeUpdate(conn, sql, plan.table, systemId, appCode);
if (n > 0) log.info("[DEL] {}: {} 行", plan.table, n);
}
@@ -240,6 +288,96 @@ public class DbClearServiceImpl implements DbClearService {
result.mainBakNames.addAll(mainBakNames);
}
// =====================================================================
// P1-2e (2026-07-23): Ghost 模式清理
// 跨 systemId 残留, 复用 Phase 1/2/3 拆事务, 但 sql 走 GHOST_PLANS
// 当 currentSystemId 为空 (app 已全删), 改用 "f_en_code LIKE 'appCode%' AND f_system_id NOT IN current_set OR f_system_id IS NULL"
// =====================================================================
private void clearGhostTables(Connection conn, String appCode, String currentSystemId, String dbTs,
DbClearResult result) throws Exception {
// P1-2e: 两种模式 — currentSystemId 存在时排除它; 不存在时清所有同 enCode (因为 app 已删, 全是 ghost)
boolean noCurrent = (currentSystemId == null);
if (noCurrent) {
log.warn("GHOST 模式: 当前 systemId 为空, 改用'清所有 f_en_code LIKE appCode% 的记录'模式 (因为 app 已删, 无活跃 systemId)");
currentSystemId = ""; // 改成空字符串, 让 SQL: f_system_id != '' 匹配所有非空 systemId
}
// Phase 1: BAK
List<String> mainBakNames = new ArrayList<>();
List<String> emptyDrops = new ArrayList<>();
for (TablePlan plan : GHOST_PLANS) {
String bakName = "D" + dbTs + "-ghost-" + plan.table;
try {
createBakTable(conn, bakName, plan.table);
mainBakNames.add(bakName);
} catch (Exception e) {
log.warn("(ghost BAK skip {}: {})", plan.table, e.getMessage());
continue;
}
String filled = fillGhostWhereTpl(plan.whereTpl, appCode, currentSystemId);
String sql = buildInsertSelectSql(bakName, plan.table, filled);
try {
int n = executeGhostUpdate(conn, sql, plan.table, appCode, currentSystemId);
if (n == 0) {
emptyDrops.add(bakName);
log.info("[GHOST-BAK-EMPTY] {} → 无数据, Phase 3 将 DROP", bakName);
} else {
log.info("[GHOST-BAK] {}: {} 行 → `{}`", plan.table, n, bakName);
}
} catch (Exception e) {
log.warn("(ghost BAK INSERT-SELECT skip {}: {})", plan.table, e.getMessage());
}
}
// Phase 2: DELETE 单事务
conn.setAutoCommit(false);
try {
int totalDel = 0;
for (TablePlan plan : GHOST_PLANS) {
String filled = fillGhostWhereTpl(plan.whereTpl, appCode, currentSystemId);
String sql = "DELETE FROM " + qid(plan.table) + " WHERE " + filled;
try {
int n = executeGhostUpdate(conn, sql, plan.table, appCode, currentSystemId);
if (n > 0) {
log.info("[GHOST-DEL] {}: {} 行", plan.table, n);
totalDel += n;
}
} catch (Exception e) {
log.warn("(ghost DEL skip {}: {})", plan.table, e.getMessage());
}
}
conn.commit();
log.info("GHOST Phase 2 提交: {} 张表, 共 {} 行", GHOST_PLANS.size(), totalDel);
} catch (Exception e) {
conn.rollback();
log.error("GHOST 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);
}
/** P1-2e: ghost plan 的执行 update (param 顺序: appCode, currentSystemId) */
private int executeGhostUpdate(Connection conn, String sql, String table, String appCode, String currentSystemId) throws Exception {
try (PreparedStatement ps = conn.prepareStatement(sql)) {
if (GHOST_PARAMS_BY_APP_CODE.contains(table)) {
// 只用 appCode
ps.setString(1, appCode);
} else {
ps.setString(1, appCode);
ps.setString(2, currentSystemId == null ? "" : currentSystemId);
}
return ps.executeUpdate();
}
}
// =====================================================================
// 业务库清理: SET FK_OFF + RENAME TABLE
// =====================================================================
@@ -98,6 +98,8 @@ public class DbClearController {
* 输入格式: "workflow,di,portal,print,report,schedule,dict,biz" 逗号分隔
* 传 "all" 或 null → 全开
* 传 "conservative" → 仅 form/menu/dict
* 传 "ghost" → includeGhost=true (其它按 all 处理)
* 传 "conservative,ghost" → conservative + includeGhost=true
* 不传 → 全开 (与历史行为一致)
*/
private DbClearService.DbClearScope parseScope(String scopeStr) {
@@ -128,6 +130,7 @@ public class DbClearController {
case "schedule": scope.includeSchedule = true; break;
case "dict": scope.includeDict = true; break;
case "biz": scope.includeBiz = true; break;
case "ghost": scope.includeGhost = true; break; // P1-2e
case "form": /* 始终清, 不用 flag */ break;
default: log.warn("(未知 scope key: {}, 已忽略)", k);
}