feat(phase-B75): 计次上报加 source 维度(V188)——「谁在烧」一列读数,归因不再扫设备日志
- V188:biz_user_secret_usage_daily 加 source varchar(24) NOT NULL DEFAULT 'legacy'(存量行诚实落 legacy 不回填),唯一键 uk_user_module_date 扩成四元组(user/module/date/source),加列与建索引均 information_schema 判存可重跑(列按 V157 款、索引按 V184 款),八段式含验证 SQL 与人工回滚
This commit is contained in:
+117
@@ -0,0 +1,117 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import com.nanri.aiimage.modules.usersecret.model.entity.UserSecretUsageEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* V188 给 {@code biz_user_secret_usage_daily} 加 {@code source} 列并把唯一键扩成四元组,
|
||||
* 本类钉它的形状(Expand 半边,2026-11-04 客户反馈「代理IP用的很猛」的归因前置)。
|
||||
*
|
||||
* <p>四件事各守一个炸法:
|
||||
* <ol>
|
||||
* <li><b>NOT NULL + DEFAULT 'legacy' 且不回填</b>:存量行无法追溯是哪一路报的,
|
||||
* 统一落 legacy(诚实的"不知道"),不按今天的上报方名单猜历史。</li>
|
||||
* <li><b>唯一键必须扩成四元组</b>:不扩键的话同一天多路的上报会在旧三元组上互相
|
||||
* ON DUPLICATE(source 记成"最后写入者"),按来源归因直接失真;只加列不扩键
|
||||
* 等于白加。</li>
|
||||
* <li><b>可重跑</b>:加列与建索引都先查 information_schema(列按 V157 款、索引按
|
||||
* V184 款),迁移中途失败重跑不许撞 1060/1061。</li>
|
||||
* <li><b>列名与实体字段对齐</b>:MyBatis-Plus 驼峰↔下划线,对不上写入会静默不落库。</li>
|
||||
* </ol>
|
||||
*/
|
||||
class UserSecretUsageSourceMigrationV188Test {
|
||||
|
||||
private static final Path SQL = Path.of(System.getProperty("user.dir"),
|
||||
"src/main/resources/db/V188__user_secret_usage_source.sql");
|
||||
|
||||
private static String sql() throws IOException {
|
||||
assertTrue(Files.exists(SQL), "V188 迁移脚本不在:" + SQL);
|
||||
return Files.readString(SQL);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 列是not_null的窄列且默认落legacy() throws IOException {
|
||||
String text = sql();
|
||||
|
||||
assertTrue(text.contains("ALTER TABLE `biz_user_secret_usage_daily`"), "迁移对象必须是本表");
|
||||
// DDL 藏在 SET @ddl 的字符串字面量里,引号按 SQL 规则写成两个单引号(''legacy'')
|
||||
assertTrue(text.contains("ADD COLUMN `source` varchar(24) NOT NULL DEFAULT ''legacy''"),
|
||||
"NOT NULL + DEFAULT 'legacy':存量行诚实落 legacy,NULL 会让 GROUP BY 多出一种真空来源");
|
||||
assertTrue(text.contains("AFTER `module_key`"),
|
||||
"紧跟 module_key:来源是模块的下一级维度,读的人要能一眼成对");
|
||||
assertFalse(text.toUpperCase().contains("UPDATE `BIZ_USER_SECRET_USAGE_DAILY` SET `SOURCE`"),
|
||||
"Expand 阶段不回填:历史行的来源不能按今天的上报方名单猜");
|
||||
assertTrue(text.contains("DROP COLUMN `source`"),
|
||||
"回滚语句必须写在脚本注释里(与 V157 同一口径)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 唯一键必须扩成四元组() throws IOException {
|
||||
String text = sql();
|
||||
|
||||
assertTrue(text.contains("DROP INDEX `uk_user_module_date`"),
|
||||
"旧三元组键必须摘除:留着它与新键并存,旧形状的 upsert 会绕过 source 维度");
|
||||
assertTrue(text.contains("ADD UNIQUE KEY `uk_user_module_date_source` "
|
||||
+ "(`user_id`, `module_key`, `business_date`, `source`)"),
|
||||
"新键必须含 source:同一天多路各占一行,读侧 SUM 对多出的行透明");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 加列与建索引都必须判存可重跑() throws IOException {
|
||||
String text = sql();
|
||||
|
||||
assertTrue(text.contains("information_schema.COLUMNS"),
|
||||
"加列前必须查列存在(V157 款)");
|
||||
assertTrue(text.contains("information_schema.STATISTICS"),
|
||||
"建索引前必须查索引存在(V184 款)");
|
||||
assertEquals(2, countMatches(text, "EXECUTE stmt"),
|
||||
"两条 DDL 各自走 PREPARE/EXECUTE 判存重跑(DEALLOCATE 不计)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 头注释版本号与文件名一致() throws IOException {
|
||||
String first = sql().lines().findFirst().orElse("");
|
||||
assertTrue(first.startsWith("-- V188:"), "头注释必须写 V188,实际是:" + first);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 实体字段与列名按驼峰下划线一一对应() throws Exception {
|
||||
assertNotNull(UserSecretUsageEntity.class.getDeclaredField("source"),
|
||||
"实体缺 source 字段 ⇒ 列永远不会被写");
|
||||
assertEquals("source", camelToSnake("source"),
|
||||
"单词列名没有驼峰转换空间,这一格防的是字段名拼错(如 soource)");
|
||||
assertTrue(sql().contains("`source`"));
|
||||
}
|
||||
|
||||
private static int countMatches(String text, String needle) {
|
||||
Matcher matcher = Pattern.compile(needle, Pattern.LITERAL).matcher(text);
|
||||
int count = 0;
|
||||
while (matcher.find()) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private static String camelToSnake(String name) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (char c : name.toCharArray()) {
|
||||
if (Character.isUpperCase(c)) {
|
||||
out.append('_').append(Character.toLowerCase(c));
|
||||
} else {
|
||||
out.append(c);
|
||||
}
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user