fix: preserve lastleg comments in overseas migration

This commit is contained in:
2026-09-01 16:30:41 +08:00
23 changed files with 267 additions and 608 deletions
@@ -61,6 +61,36 @@ class SchemaChineseCommentContractTest {
}
}
@Test
void inboundPlanMigrationsPreserveChineseCommentsForEveryAlteredColumn() throws Exception {
String baseline = Files.readString(
Path.of("src", "main", "resources", "db", "lastleg.sql"), StandardCharsets.UTF_8);
String overseasUnified = Files.readString(
Path.of("..", "deploy", "sql", "海外区域统一增量-MySQL8.sql"), StandardCharsets.UTF_8);
List<MigrationColumn> required = List.of(
new MigrationColumn("ll_inbound_plan", "status"),
new MigrationColumn("ll_inbound_plan_line", "required_package_count"),
new MigrationColumn("ll_inbound_plan_pallet", "actual_package_count"),
new MigrationColumn("ll_inbound_plan_pallet", "actual_volume_cbm"),
new MigrationColumn("ll_inbound_plan_pallet", "assignment_status"),
new MigrationColumn("ll_inbound_plan_pallet", "locked_at"),
new MigrationColumn("ll_inbound_plan_pallet", "warehouse_location_id"),
new MigrationColumn("ll_inbound_plan_pallet", "warehouse_pallet_id"),
new MigrationColumn("ll_inbound_plan_pallet", "warehouse_resource_lock_id"),
new MigrationColumn("ll_inbound_plan_pallet_allocation", "actual_volume_cbm"),
new MigrationColumn("ll_inbound_plan_pallet_allocation", "counted_package_count"),
new MigrationColumn("ll_inbound_plan_pallet_allocation", "picked_package_count"),
new MigrationColumn("ll_inbound_plan_pallet_allocation", "planned_package_count"),
new MigrationColumn("ll_inbound_plan_pallet_allocation", "status"),
new MigrationColumn("ll_inbound_plan_pallet_allocation", "version")
);
for (MigrationColumn column : required) {
assertSpecificAlterationHasChineseComment(baseline, column);
}
assertAllAlteredColumnsHaveChineseComments(overseasUnified, "海外统一增量 SQL");
}
private static List<TableDefinition> assertAllChineseComments(String sql, String source, int expectedCount) {
List<TableDefinition> tables = new ArrayList<>();
Matcher matcher = TABLE.matcher(sql);
@@ -152,6 +182,44 @@ class SchemaChineseCommentContractTest {
assertTrue(addColumnPattern.matcher(sql).find(), "增量 SQL 的 ADD COLUMN 缺少中文备注: " + column);
}
private static void assertSpecificAlterationHasChineseComment(String sql, MigrationColumn target) {
Pattern pattern = Pattern.compile(
"(?is)ALTER\\s+TABLE\\s+" + Pattern.quote(target.table())
+ "\\s+(?:ADD|MODIFY)\\s+COLUMN\\s+" + Pattern.quote(target.column()) + "\\b");
Matcher matcher = pattern.matcher(sql);
assertTrue(matcher.find(), "缺少字段增量定义: " + target.table() + "." + target.column());
assertAlterationSegmentHasChineseComment(sql, matcher.start(), matcher.end(),
"字段增量缺少中文备注: " + 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);
int count = 0;
while (matcher.find()) {
count++;
assertAlterationSegmentHasChineseComment(sql, matcher.start(), matcher.end(),
source + " 字段增量缺少中文备注: " + matcher.group(1));
}
assertTrue(count > 0, source + " 未解析到 ADD/MODIFY COLUMN");
}
private static void assertAlterationSegmentHasChineseComment(
String sql,
int start,
int tokenEnd,
String message
) {
Matcher next = Pattern.compile("(?i)\\b(?:ADD|MODIFY)\\s+COLUMN\\s+[a-z0-9_]+\\b")
.matcher(sql);
int end = sql.indexOf(';', tokenEnd);
if (end < 0) end = sql.length();
if (next.find(tokenEnd) && next.start() < end) end = next.start();
String definition = sql.substring(start, end);
assertTrue(Pattern.compile("(?is)\\bCOMMENT\\s+''?[^;]*[\\u4e00-\\u9fff][^;]*''?")
.matcher(definition).find(), message);
}
private static List<String> splitTopLevel(String body) {
List<String> definitions = new ArrayList<>();
int start = 0;
@@ -176,4 +244,7 @@ class SchemaChineseCommentContractTest {
private record TableDefinition(String name, List<String> columns) {
}
private record MigrationColumn(String table, String column) {
}
}