auto(bfc0cfa2c3a4): session end @ 2026-07-15T17:17:04 (+1f)

This commit is contained in:
ssw committed 2026-07-15 17:17:04 +08:00
1 parent e3440e037b
commit d318864297
1 file changed
+44 -24
@@ -106,9 +106,13 @@ public class DbClearServiceImpl implements DbClearService {
Connection mainConn = null;
try {
// MySQL: history_bak 是独立 DATABASE (走 ensureHistoryBakDb 创库)
// PG: history_bak 是 schema, 必须在 mainConn + biz conn 中各自 CREATE SCHEMA
ensureHistoryBakDb();
mainConn = openConn(mainDb, null);
ensureHistoryBakSchema(mainConn);
String systemId = querySystemId(mainConn, appCode);
if (systemId != null) {
clearMainTables(mainConn, appCode, systemId, dbTs, r);
@@ -151,7 +155,7 @@ public class DbClearServiceImpl implements DbClearService {
createBakTable(conn, bakName, plan.table);
mainBakNames.add(bakName);
}
String sql = buildInsertSelectSql(plan.table, plan.whereTpl);
String sql = buildInsertSelectSql(bakName, 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) {
@@ -199,6 +203,7 @@ public class DbClearServiceImpl implements DbClearService {
Connection conn = null;
try {
conn = openConn(appCode, null);
ensureHistoryBakSchema(conn); // PG: history_bak schema in biz db
conn.setAutoCommit(false);
try {
disableFkChecks(conn);
@@ -315,20 +320,22 @@ public class DbClearServiceImpl implements DbClearService {
if ("MySQL".equalsIgnoreCase(dbType)) {
return "CREATE TABLE IF NOT EXISTS " + qid(bakDb) + "." + qid(bakTbl) + " LIKE " + qid(mainDb) + "." + qid(srcTbl);
} else if ("PostgreSQL".equalsIgnoreCase(dbType)) {
// PG 无跨库查询, schema (defaultSchema) 替代 mainDb
return "CREATE TABLE IF NOT EXISTS " + qid(bakDb) + "." + qid(bakTbl) +
" AS SELECT * FROM " + qid(mainDb) + "." + qid(srcTbl) + " WITH NO DATA";
" AS SELECT * FROM " + qid(defaultSchema) + "." + qid(srcTbl) + " WITH NO DATA";
}
throw new RuntimeException("不支持的数据库类型: " + dbType);
}
/** INSERT INTO bak SELECT * FROM src WHERE ... */
private String buildInsertSelectSql(String srcTbl, String whereTpl) {
private String buildInsertSelectSql(String bakName, String srcTbl, String whereTpl) {
if ("MySQL".equalsIgnoreCase(dbType)) {
return "INSERT INTO " + qid(HISTORY_BAK_DB) + "." + qid("D{ts}-" + srcTbl) +
return "INSERT INTO " + qid(HISTORY_BAK_DB) + "." + qid(bakName) +
" 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 ";
// PG 无跨库查询, schema (defaultSchema) 替代 mainDb
return "INSERT INTO " + qid(HISTORY_BAK_DB) + "." + qid(bakName) +
" SELECT * FROM " + qid(defaultSchema) + "." + qid(srcTbl) + " WHERE ";
}
throw new RuntimeException("不支持的数据库类型: " + dbType);
}
@@ -353,8 +360,8 @@ public class DbClearServiceImpl implements DbClearService {
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);
// PG 无跨库, fromDb 是业务库名 (jxcj), 实际 schema 是 defaultSchema (public)
String sql = "ALTER TABLE " + qid(defaultSchema) + "." + qid(fromTbl) + " SET SCHEMA " + qid(toDb);
if (!fromTbl.equals(toTbl)) {
sql += "; ALTER TABLE " + qid(toDb) + "." + qid(fromTbl) + " RENAME TO " + qid(toTbl);
}
@@ -416,24 +423,36 @@ public class DbClearServiceImpl implements DbClearService {
}
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);
// MySQL: history_bak 是独立 DATABASE, 用单独连接 (连到 server 而非具体 db) 创建
// PG: history_bak 是 schema, 必须在每个相关 db 的 conn 内 CREATE SCHEMA (PG 无跨库)
if ("MySQL".equalsIgnoreCase(dbType)) {
Connection conn = null;
try {
conn = openConn(null, null);
try (Statement s = conn.createStatement()) {
s.execute("CREATE DATABASE IF NOT EXISTS `" + HISTORY_BAK_DB + "` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci");
}
log.info("history_bak database 已就绪");
} finally {
closeQuietly(conn);
}
} else if ("PostgreSQL".equalsIgnoreCase(dbType)) {
log.info("PG: history_bak schema 将在 mainConn + biz conn 中分别 CREATE SCHEMA IF NOT EXISTS");
} else {
throw new RuntimeException("不支持的数据库类型: " + dbType);
}
}
/**
* PG: 在当前 conn 所属 db 内 CREATE SCHEMA IF NOT EXISTS history_bak (幂等)
* MySQL: no-op (history_bak 已作为独立 db 在 ensureHistoryBakDb 中建好)
*/
private void ensureHistoryBakSchema(Connection conn) throws Exception {
if ("PostgreSQL".equalsIgnoreCase(dbType)) {
try (Statement s = conn.createStatement()) {
s.execute(sql);
s.execute("CREATE SCHEMA IF NOT EXISTS \"" + HISTORY_BAK_DB + "\"");
}
log.info("history_bak database 已就绪");
} finally {
closeQuietly(conn);
log.info("history_bak schema 已就绪 (current db)");
}
}
@@ -451,8 +470,9 @@ public class DbClearServiceImpl implements DbClearService {
s.execute(sql);
}
} else if ("PostgreSQL".equalsIgnoreCase(dbType)) {
// PG 无跨库, schema (defaultSchema) 替代 mainDb
String sql = "CREATE TABLE IF NOT EXISTS " + qid(HISTORY_BAK_DB) + "." + qid(bakName) +
" AS SELECT * FROM " + qid(mainDb) + "." + qid(srcTbl) + " WITH NO DATA";
" AS SELECT * FROM " + qid(defaultSchema) + "." + qid(srcTbl) + " WITH NO DATA";
try (Statement s = conn.createStatement()) {
s.execute(sql);
}