Browse Source

fix(survey): survey_records.template_id改为可空(HL-01健康阅卷source=health_review不绑定模板, 提交时templateId=null被MyBatis跳过致NOT NULL无默认值400) schema.sql+幂等迁移500+回归测试

Xiaogang Liao 3 days ago
parent
commit
1e894f7e6e

+ 16 - 0
cfc-backend/src/main/java/com/etotem/cfc/config/DatabaseInitializer.java

@@ -3371,6 +3371,9 @@ insertSysConfigSeed("gift_membership_pay_price", "198000", "送会员商品达
         // 迁移404: articles.audio_url 由 varchar(500) 改为 MEDIUMTEXT(多段音频JSON数组可超500字符)
         alterArticleAudioUrlToMediumtext();
 
+        // 迁移500: survey_records.template_id 改为可空(HL-01 健康阅卷 source='health_review' 不绑定模板)
+        alterSurveyRecordTemplateIdNullable();
+
         // 迁移406: activity_poster_templates 增加 layout_config 列(海报模板元素位置/样式可配置,需求:活动详情海报预览重设计)
         addActivityPosterTemplateLayoutConfigColumn();
 
@@ -14779,6 +14782,19 @@ log.info("迁移298: 已为无 openid 的 child 账号补齐 family_members 记
         }
     }
 
+    // 迁移500: survey_records.template_id 改为可空(HL-01 健康阅卷不绑定模板)。
+    // 健康阅卷提交 source='health_review' 不传 templateId,原 NOT NULL 约束导致
+    // "Field 'template_id' doesn't have a default value" → 400。幂等 MODIFY 可重复执行。
+    private void alterSurveyRecordTemplateIdNullable() {
+        try {
+            jdbcTemplate.execute("ALTER TABLE survey_records MODIFY COLUMN template_id BIGINT NULL "
+                    + "COMMENT '调研模板ID(NULL=健康阅卷等非模板场景)'");
+            log.info("迁移500: survey_records.template_id 已改为可空");
+        } catch (Exception e) {
+            log.warn("迁移500: survey_records.template_id 改可空失败(幂等容忍): {}", e.getMessage());
+        }
+    }
+
     // 迁移404: articles.audio_url 由 varchar(500) 改为 MEDIUMTEXT
     // 多段音频 JSON 数组(每段 OSS URL ~94 字符)在长文中可超 2900 字符,
     // 超出 varchar(500) 导致 DataIntegrityViolationException("数据不完整或格式错误")

+ 1 - 1
cfc-backend/src/main/resources/schema.sql

@@ -5483,7 +5483,7 @@ CREATE TABLE IF NOT EXISTS survey_records (
     id            BIGINT AUTO_INCREMENT PRIMARY KEY,
     family_id     BIGINT NOT NULL COMMENT '家庭ID',
     member_id     BIGINT NOT NULL COMMENT '成员ID',
-    template_id   BIGINT NOT NULL COMMENT '调研模板ID',
+    template_id   BIGINT NULL COMMENT '调研模板ID(NULL=健康阅卷等非模板场景)',
     answers_json  TEXT COMMENT '答案JSON',
     submitted_at  DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '提交时间',
     period_start  DATE COMMENT '周期开始',

+ 33 - 0
cfc-backend/src/test/java/com/etotem/cfc/unit/SurveyTemplateIdNullableTest.java

@@ -0,0 +1,33 @@
+package com.etotem.cfc.unit;
+
+import org.junit.jupiter.api.Test;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.*;
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * 回归防护:survey_records.template_id 必须可空(HL-01 健康阅卷不绑定模板)。
+ * 纯源码文本断言,不连库。
+ */
+class SurveyTemplateIdNullableTest {
+
+    private String read(String rel) throws Exception {
+        Path p = Paths.get(rel);
+        if (!Files.exists(p)) p = Paths.get("../cfc-backend/" + rel);
+        return new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
+    }
+
+    @Test
+    void schema_allows_null_template_id() throws Exception {
+        String sql = read("src/main/resources/schema.sql");
+        assertTrue(sql.contains("template_id   BIGINT NULL COMMENT '调研模板ID(NULL=健康阅卷等非模板场景)'"),
+                "schema.sql 中 survey_records.template_id 必须可空");
+    }
+
+    @Test
+    void migration_makes_template_id_nullable() throws Exception {
+        String src = read("src/main/java/com/etotem/cfc/config/DatabaseInitializer.java");
+        assertTrue(src.contains("alterSurveyRecordTemplateIdNullable"), "应有幂等迁移方法");
+        assertTrue(src.contains("MODIFY COLUMN template_id BIGINT NULL"), "迁移应把列改为可空");
+    }
+}