暂存提交
This commit is contained in:
+140
@@ -0,0 +1,140 @@
|
||||
package com.baiyue.lastleg.schema;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class SchemaChineseCommentContractTest {
|
||||
|
||||
private static final Pattern CHINESE = Pattern.compile("[\\u4e00-\\u9fff]");
|
||||
private static final Pattern TABLE = Pattern.compile(
|
||||
"(?is)CREATE\\s+TABLE\\s+IF\\s+NOT\\s+EXISTS\\s+(ll_[a-z0-9_]+)"
|
||||
+ "\\s*\\((.*?)\\)\\s*ENGINE\\s*=\\s*InnoDB\\s+COMMENT\\s*=\\s*'([^']*)'\\s*;");
|
||||
|
||||
@Test
|
||||
void allLastlegTablesAndColumnsHaveChineseComments() throws Exception {
|
||||
String baseline = Files.readString(
|
||||
Path.of("src", "main", "resources", "db", "lastleg.sql"), StandardCharsets.UTF_8);
|
||||
String incremental = Files.readString(
|
||||
Path.of("..", "scripts", "db", "incremental.sql"), StandardCharsets.UTF_8);
|
||||
|
||||
List<TableDefinition> baselineTables = assertAllChineseComments(baseline, "基线 SQL", 55);
|
||||
assertAllChineseComments(incremental, "增量 SQL", 49);
|
||||
assertCommentMigrationCovers(incremental, baselineTables);
|
||||
}
|
||||
|
||||
@Test
|
||||
void newlyAddedColumnsCarryChineseCommentsInBaselineAndIncrementalSql() throws Exception {
|
||||
String baseline = Files.readString(
|
||||
Path.of("src", "main", "resources", "db", "lastleg.sql"), StandardCharsets.UTF_8);
|
||||
String incremental = Files.readString(
|
||||
Path.of("..", "scripts", "db", "incremental.sql"), StandardCharsets.UTF_8);
|
||||
|
||||
List<String> columns = List.of(
|
||||
"allocation_idempotency_key", "allocation_request_hash", "outbound_idempotency_key",
|
||||
"outbound_request_hash", "outbound_at", "pallet_count", "received_pallet_count",
|
||||
"allocated_pallet_count", "shipped_pallet_count", "receipt_idempotency_key",
|
||||
"receipt_request_hash"
|
||||
);
|
||||
for (String column : columns) {
|
||||
assertColumnHasChineseComment(baseline, column, "基线 SQL");
|
||||
assertColumnHasChineseComment(incremental, column, "增量 SQL");
|
||||
assertIncrementalAddColumnHasChineseComment(incremental, column);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<TableDefinition> assertAllChineseComments(String sql, String source, int expectedCount) {
|
||||
List<TableDefinition> tables = new ArrayList<>();
|
||||
Matcher matcher = TABLE.matcher(sql);
|
||||
while (matcher.find()) {
|
||||
String table = matcher.group(1);
|
||||
assertTrue(CHINESE.matcher(matcher.group(3)).find(), source + " 表备注必须包含中文: " + table);
|
||||
List<String> columns = splitTopLevel(matcher.group(2));
|
||||
assertFalse(columns.isEmpty(), source + " 表定义没有字段: " + table);
|
||||
List<String> columnNames = new ArrayList<>();
|
||||
for (String definition : columns) {
|
||||
if (isConstraint(definition)) {
|
||||
continue;
|
||||
}
|
||||
String column = definition.stripLeading().split("\\s+", 2)[0].replace("`", "");
|
||||
columnNames.add(column);
|
||||
assertTrue(Pattern.compile("(?is)\\bCOMMENT\\s+'[^']*[\\u4e00-\\u9fff][^']*'")
|
||||
.matcher(definition).find(),
|
||||
source + " 字段备注必须包含中文: " + table + "." + column);
|
||||
}
|
||||
tables.add(new TableDefinition(table, columnNames));
|
||||
}
|
||||
assertTrue(tables.size() >= expectedCount,
|
||||
source + " 解析到的尾程表数量不足,实际=" + tables.size() + ",期望至少=" + expectedCount);
|
||||
return tables;
|
||||
}
|
||||
|
||||
private static void assertCommentMigrationCovers(String incremental, List<TableDefinition> tables) {
|
||||
int marker = incremental.indexOf("2026-08-18 / 025 Chinese table and column comments");
|
||||
assertTrue(marker >= 0, "增量 SQL 缺少 025 中文备注迁移");
|
||||
String migration = incremental.substring(marker);
|
||||
for (TableDefinition table : tables) {
|
||||
assertTrue(migration.contains("table_name='" + table.name() + "'"),
|
||||
"025 备注迁移缺少表: " + table.name());
|
||||
for (String column : table.columns()) {
|
||||
assertTrue(migration.contains("table_name='" + table.name()
|
||||
+ "' AND column_name='" + column + "'"),
|
||||
"025 备注迁移缺少字段: " + table.name() + "." + column);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isConstraint(String definition) {
|
||||
String normalized = definition.stripLeading().toUpperCase();
|
||||
return normalized.startsWith("PRIMARY ") || normalized.startsWith("UNIQUE ")
|
||||
|| normalized.startsWith("KEY ") || normalized.startsWith("CONSTRAINT ")
|
||||
|| normalized.startsWith("FOREIGN ") || normalized.startsWith("CHECK ");
|
||||
}
|
||||
|
||||
private static void assertColumnHasChineseComment(String sql, String column, String source) {
|
||||
Pattern columnPattern = Pattern.compile(
|
||||
"(?is)\\b" + Pattern.quote(column) + "\\b[^,;]*?COMMENT\\s+''?[^',;]*[\\u4e00-\\u9fff][^',;]*''?");
|
||||
assertTrue(columnPattern.matcher(sql).find(), source + " 新增字段缺少中文备注: " + column);
|
||||
}
|
||||
|
||||
private static void assertIncrementalAddColumnHasChineseComment(String sql, String column) {
|
||||
Pattern addColumnPattern = Pattern.compile(
|
||||
"(?is)ADD\\s+COLUMN\\s+" + Pattern.quote(column)
|
||||
+ "\\b[^;]*?COMMENT\\s+''[^';]*[\\u4e00-\\u9fff][^';]*''");
|
||||
assertTrue(addColumnPattern.matcher(sql).find(), "增量 SQL 的 ADD COLUMN 缺少中文备注: " + column);
|
||||
}
|
||||
|
||||
private static List<String> splitTopLevel(String body) {
|
||||
List<String> definitions = new ArrayList<>();
|
||||
int start = 0;
|
||||
int parentheses = 0;
|
||||
boolean quoted = false;
|
||||
for (int i = 0; i < body.length(); i++) {
|
||||
char current = body.charAt(i);
|
||||
if (current == '\'' && (i == 0 || body.charAt(i - 1) != '\\')) {
|
||||
quoted = !quoted;
|
||||
} else if (!quoted && current == '(') {
|
||||
parentheses++;
|
||||
} else if (!quoted && current == ')') {
|
||||
parentheses--;
|
||||
} else if (!quoted && parentheses == 0 && current == ',') {
|
||||
definitions.add(body.substring(start, i).trim());
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
definitions.add(body.substring(start).trim());
|
||||
return definitions.stream().filter(value -> !value.isEmpty()).toList();
|
||||
}
|
||||
|
||||
private record TableDefinition(String name, List<String> columns) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user