소스 검색

fix(db): 修复DBI启动日志WARN(RENAME语法+不存在表/列的迁移)

- 修复 3 处 RENAME TABLE IF EXISTS(MySQL 8 不支持 IF EXISTS 语法)
  → 改用 tableExists() 前置检查 + RENAME TABLE(无 IF EXISTS)
- 修复 1 处 CHANGE COLUMN min_quantity(列已不存在时报错)
  → 改用 columnExists() 前置检查,不存在则跳过
- 新增 tableExists() + columnExists() 辅助方法
- 不影响业务,仅消除启动日志噪声
Xiaogang Liao 1 개월 전
부모
커밋
115f80296c
1개의 변경된 파일38개의 추가작업 그리고 8개의 파일을 삭제
  1. 38 8
      cfc-backend/src/main/java/com/etotem/cfc/config/DatabaseInitializer.java

+ 38 - 8
cfc-backend/src/main/java/com/etotem/cfc/config/DatabaseInitializer.java

@@ -2939,7 +2939,9 @@ log.info("已添加template_id列到tasks表");
 
     private void migrateHealthDimensionsTable() {
         try {
-            jdbcTemplate.execute("RENAME TABLE IF EXISTS `health_dimension_score` TO `health_dimension_score_old`");
+            if (tableExists("health_dimension_score")) {
+                jdbcTemplate.execute("RENAME TABLE `health_dimension_score` TO `health_dimension_score_old`");
+            }
         } catch (Exception e) {
             log.warn("health_dimension_score rename skipped: {}", e.getMessage());
         }
@@ -2965,7 +2967,9 @@ log.info("已添加template_id列到tasks表");
         }
 
         try {
-            jdbcTemplate.execute("RENAME TABLE IF EXISTS `health_data_source_record` TO `health_data_source_record_old`");
+            if (tableExists("health_data_source_record")) {
+                jdbcTemplate.execute("RENAME TABLE `health_data_source_record` TO `health_data_source_record_old`");
+            }
         } catch (Exception e) {
             log.warn("health_data_source_record rename skipped: {}", e.getMessage());
         }
@@ -2987,7 +2991,9 @@ log.info("已添加template_id列到tasks表");
         }
 
         try {
-            jdbcTemplate.execute("RENAME TABLE IF EXISTS `health_norm_reference` TO `health_norm_reference_old`");
+            if (tableExists("health_norm_reference")) {
+                jdbcTemplate.execute("RENAME TABLE `health_norm_reference` TO `health_norm_reference_old`");
+            }
         } catch (Exception e) {
             log.warn("health_norm_reference rename skipped: {}", e.getMessage());
         }
@@ -4693,6 +4699,28 @@ try {
         log.info("食品相关表初始化完成");
     }
 
+    private boolean tableExists(String tableName) {
+        try {
+            Integer count = jdbcTemplate.queryForObject(
+                "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?",
+                Integer.class, tableName);
+            return count != null && count > 0;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+    private boolean columnExists(String table, String column) {
+        try {
+            Integer count = jdbcTemplate.queryForObject(
+                "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?",
+                Integer.class, table, column);
+            return count != null && count > 0;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
     private void ensureColumn(String table, String column, String definition) {
         try {
             jdbcTemplate.execute("ALTER TABLE " + table + " ADD COLUMN " + column + " " + definition);
@@ -6042,11 +6070,13 @@ try {
     ensureColumn("points_exchange_records", "family_member_id", "BIGINT COMMENT '家庭成员ID'");
 
     // 迁移133: products表列对齐 — rename min_quantity→purchase_count_threshold, add missing columns
-    try {
-        jdbcTemplate.execute("ALTER TABLE products CHANGE COLUMN min_quantity purchase_count_threshold INT DEFAULT 0 COMMENT '最小购买数量限制'");
-        log.info("已重命名products.min_quantity→purchase_count_threshold");
-    } catch (Exception e) {
-        log.warn("重命名products列可能已处理: {}", e.getMessage());
+    if (columnExists("products", "min_quantity")) {
+        try {
+            jdbcTemplate.execute("ALTER TABLE products CHANGE COLUMN min_quantity purchase_count_threshold INT DEFAULT 0 COMMENT '最小购买数量限制'");
+            log.info("已重命名products.min_quantity→purchase_count_threshold");
+        } catch (Exception e) {
+            log.warn("重命名products列失败(可能已处理): {}", e.getMessage());
+        }
     }
     ensureColumn("products", "recommendation_tags", "VARCHAR(500) DEFAULT NULL COMMENT '推荐标签'");
     ensureColumn("products", "repurchase_interval_days", "INT DEFAULT 30 COMMENT '复购间隔天数'");