test: enforce chinese schema comments

This commit is contained in:
2026-09-20 20:43:21 +08:00
parent 9875bb847c
commit e966061025
3 changed files with 60 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ import (
"regexp"
"strings"
"testing"
"unicode"
)
// 已应用的历史文件不改写;移除迁移之后的DDL不能再引入数据库引用约束。
@@ -52,6 +53,63 @@ func TestFreshInstallBaselineContainsNoReferenceDDL(t *testing.T) {
}
}
// 当前基线是持久化模型的可执行说明:表和每个字段都必须保留中文注释,
// 防止后续新增模型只写类型、不写职责、单位和可空语义。
func TestFreshInstallBaselineHasChineseModelComments(t *testing.T) {
inTable := false
tableName := ""
columns := 0
for lineNumber, line := range strings.Split(freshInstallBaseline, "\n") {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "CREATE TABLE `") {
if inTable {
t.Fatalf("line %d starts a table before %s was closed", lineNumber+1, tableName)
}
start := strings.Index(trimmed, "`") + 1
end := strings.Index(trimmed[start:], "`")
if start <= 0 || end < 0 {
t.Fatalf("line %d has malformed table declaration: %s", lineNumber+1, trimmed)
}
tableName = trimmed[start : start+end]
inTable = true
columns = 0
continue
}
if !inTable {
continue
}
if strings.HasPrefix(trimmed, "`") {
columns++
comment := strings.Index(line, " COMMENT '")
if comment < 0 || !hasChineseComment(line[comment:]) {
t.Fatalf("table %s column on line %d must have a Chinese COMMENT", tableName, lineNumber+1)
}
}
if strings.HasPrefix(trimmed, ") ENGINE=") {
if columns == 0 {
t.Fatalf("table %s has no columns", tableName)
}
if !strings.Contains(line, " COMMENT='") || !hasChineseComment(line) {
t.Fatalf("table %s must have a Chinese table COMMENT", tableName)
}
inTable = false
tableName = ""
}
}
if inTable {
t.Fatalf("table %s is not closed", tableName)
}
}
func hasChineseComment(s string) bool {
for _, r := range s {
if unicode.Is(unicode.Han, r) {
return true
}
}
return false
}
func TestPostRemovalMigrationGuard(t *testing.T) {
if err := validateNoReferenceDDL(9, []string{"ALTER TABLE pulse_tasks ADD CONSTRAINT fk FOREIGN KEY (queue_id) REFERENCES pulse_queues(id)"}); err == nil {
t.Fatal("post-removal migration accepted foreign-key DDL")