test(sql): align contracts with clean initialization

This commit is contained in:
2026-09-17 15:10:27 +08:00
parent 1fe99eb6d0
commit 9dc344bdef
68 changed files with 851 additions and 1448 deletions
@@ -25,8 +25,8 @@ 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*;");
"(?is)CREATE\\s+TABLE\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?(ll_[a-z0-9_]+)"
+ "\\s*\\((.*?)\\)\\s*ENGINE\\s*=\\s*InnoDB[^;]*?COMMENT\\s*=\\s*'([^']*)'\\s*;");
@Test
void allLastlegTablesAndColumnsHaveChineseComments() throws Exception {
@@ -39,7 +39,7 @@ class SchemaChineseCommentContractTest {
List<TableDefinition> baselineTables = assertAllChineseComments(baseline, "基线 SQL", 56);
assertAllChineseComments(incremental, "增量 SQL", 50);
assertAllChineseComments(overseasUnified, "海外统一增量 SQL", 10);
assertAllChineseComments(overseasUnified, "海外统一初始化 SQL", 127);
assertCommentMigrationCovers(incremental, overseasUnified, baselineTables);
}
@@ -92,7 +92,9 @@ class SchemaChineseCommentContractTest {
for (MigrationColumn column : required) {
assertSpecificAlterationHasChineseComment(baseline, column);
}
assertAllAlteredColumnsHaveChineseComments(overseasUnified, "海外统一增量 SQL");
for (MigrationColumn column : required) {
assertTableColumnHasChineseComment(overseasUnified, column, "海外统一初始化 SQL");
}
}
private static List<TableDefinition> assertAllChineseComments(String sql, String source, int expectedCount) {
@@ -157,8 +159,8 @@ class SchemaChineseCommentContractTest {
private static boolean hasCommentedCreate(String sql, String table) {
return Pattern.compile(
"(?is)CREATE\\s+TABLE\\s+IF\\s+NOT\\s+EXISTS\\s+" + Pattern.quote(table)
+ "\\s*\\(.*?\\)\\s*ENGINE\\s*=\\s*InnoDB\\s+COMMENT\\s*=\\s*'[^']*[\\u4e00-\\u9fff][^']*'\\s*;")
"(?is)CREATE\\s+TABLE\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?" + Pattern.quote(table)
+ "\\s*\\(.*?\\)\\s*ENGINE\\s*=\\s*InnoDB[^;]*?COMMENT\\s*=\\s*'[^']*[\\u4e00-\\u9fff][^']*'\\s*;")
.matcher(sql).find();
}
@@ -199,6 +201,19 @@ class SchemaChineseCommentContractTest {
"字段增量缺少中文备注: " + target.table() + "." + target.column());
}
private static void assertTableColumnHasChineseComment(String sql, MigrationColumn target, String source) {
Matcher table = Pattern.compile(
"(?is)CREATE\\s+TABLE\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?" + Pattern.quote(target.table())
+ "\\s*\\((.*?)\\)\\s*ENGINE\\s*=\\s*InnoDB").matcher(sql);
assertTrue(table.find(), source + " 缺少表: " + target.table());
String definition = splitTopLevel(table.group(1)).stream()
.filter(value -> value.stripLeading().replace("`", "").startsWith(target.column() + " "))
.findFirst().orElse("");
assertTrue(Pattern.compile("(?is)\\bCOMMENT\\s+'[^']*[\\u4e00-\\u9fff][^']*'")
.matcher(definition).find(),
source + " 字段缺少中文备注: " + target.table() + "." + target.column());
}
private static void assertAllAlteredColumnsHaveChineseComments(String sql, String source) {
Pattern pattern = Pattern.compile("(?i)\\b(?:ADD|MODIFY)\\s+COLUMN\\s+([a-z0-9_]+)\\b");
Matcher matcher = pattern.matcher(sql);