修复MySQL例程注释编码并完成双版本隔离回归
分离5.7与8.0声明头和注释编码,增加无损往返校验及中文错误上下文,补充五种异常元数据拒绝测试。 最终Windows MySQL 5.7.44与8.0.45完整专项分别通过110、121个叶子用例,零失败零跳过。后端常规回归、静态检查、模块校验及Linux测试包交叉编译通过;32个常规条件测试跳过,不作为通过证据。 同步七份任务、支持矩阵及运维文档;P0-05保持进行中,全部44项范围不变。本批不代表Linux实库、其余数据库、前端或生产验收通过。
This commit is contained in:
1 parent
c4051c9166
commit
0dcda4f5ed
10 files changed
+148
-25
No files matched your search
@@ -8,8 +8,8 @@ import (
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// MySQL 8.0 将 UTF-8 目录中的声明头和注释与原客户端编码的正文拼接,
|
||||
// 再按客户端字符集返回 SHOW CREATE;非 UTF-8 例程必须分段还原,不能整体反解正文。
|
||||
// 两个版本的目录注释均需单独还原,8.0 还需还原声明头;
|
||||
// 非 UTF-8 例程必须分段处理,不能整体反解原客户端编码的正文。
|
||||
func normalizeMySQLRoutineMetadata(ctx context.Context, tx *sql.Tx, routine mysqlRoutine, definition map[string]string, column string) error {
|
||||
charset := definition["character_set_client"]
|
||||
if charset == "utf8" || charset == "utf8mb3" || charset == "utf8mb4" {
|
||||
@@ -21,19 +21,23 @@ func normalizeMySQLRoutineMetadata(ctx context.Context, tx *sql.Tx, routine mysq
|
||||
return err
|
||||
}
|
||||
decode := func(value string) (string, error) {
|
||||
var encoded []byte
|
||||
query := "SELECT CAST(CONVERT(? USING " + quoteMySQLIdentifier(charset) + ") AS BINARY)"
|
||||
if err := tx.QueryRowContext(ctx, query, value).Scan(&encoded); err != nil {
|
||||
return "", err
|
||||
var encoded, roundTrip []byte
|
||||
identifier := quoteMySQLIdentifier(charset)
|
||||
query := "SELECT CAST(CONVERT(? USING " + identifier + ") AS BINARY), CAST(CONVERT(CONVERT(? USING " + identifier + ") USING utf8mb4) AS BINARY)"
|
||||
if err := tx.QueryRowContext(ctx, query, value, value).Scan(&encoded, &roundTrip); err != nil {
|
||||
return "", fmt.Errorf("MySQL 例程目录元数据无法无损还原为 UTF-8(%s):%w", routine.name, err)
|
||||
}
|
||||
if !utf8.Valid(encoded) {
|
||||
if string(roundTrip) != value || !utf8.Valid(encoded) {
|
||||
return "", fmt.Errorf("MySQL 例程目录元数据无法无损还原为 UTF-8:%s", routine.name)
|
||||
}
|
||||
return string(encoded), nil
|
||||
}
|
||||
header, err := decode(original[:end])
|
||||
if err != nil {
|
||||
return err
|
||||
header := original[:end]
|
||||
if routine.utf8MetadataHeader {
|
||||
header, err = decode(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var comment string
|
||||
var body sql.NullString
|
||||
|
||||
@@ -115,10 +115,8 @@ SET @scert_routine_collation = @@SESSION.collation_connection;`); err != nil {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if routine.utf8MetadataHeader {
|
||||
if err := normalizeMySQLRoutineMetadata(ctx, tx, routine, definition, column); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := normalizeMySQLRoutineMetadata(ctx, tx, routine, definition, column); err != nil {
|
||||
return err
|
||||
}
|
||||
// 同名例程必须替换;定义者和权限语义来自原始 SHOW CREATE,不擅自改写。
|
||||
drop := "DROP " + routine.kind + " IF EXISTS " + quoteMySQLIdentifier(routine.name)
|
||||
|
||||
@@ -59,6 +59,9 @@ func TestMySQLRealNativeRoutines(t *testing.T) {
|
||||
}
|
||||
}
|
||||
definition := readMySQLRoutineDefinitions(t, db.DB)
|
||||
t.Run("metadata_rejection", func(t *testing.T) {
|
||||
checkMySQLRoutineMetadataRejection(t, db.DB, server.IsMySQL8())
|
||||
})
|
||||
output := filepath.Join(server.Root(), "native-routines.sql")
|
||||
if err := backupMySQLDatabase(t.Context(), source, output); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -219,6 +222,50 @@ func TestMySQLRealRoutineVisibility(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func checkMySQLRoutineMetadataRejection(t *testing.T, db *sql.DB, mysql8 bool) {
|
||||
t.Helper()
|
||||
tx, err := db.BeginTx(t.Context(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
routine := mysqlRoutine{name: "legacy_café", kind: "FUNCTION", utf8MetadataHeader: mysql8}
|
||||
const column = "Create Function"
|
||||
for _, item := range []struct {
|
||||
name string
|
||||
alter func(string) string
|
||||
message string
|
||||
}{
|
||||
{"incomplete_header", func(string) string { return "CREATE FUNCTION" }, "参数列表"},
|
||||
{"body_mismatch", func(value string) string { return value + " " }, "正文与目录元数据不一致"},
|
||||
{"missing_comment", func(value string) string {
|
||||
return strings.Replace(value, "\n COMMENT ", "\n REMARK ", 1)
|
||||
}, "注释声明不完整"},
|
||||
{"lossy_comment", func(value string) string {
|
||||
return strings.Replace(value, "\n COMMENT '", "\n COMMENT '中文", 1)
|
||||
}, "无法无损还原"},
|
||||
{"invalid_utf8_comment", func(value string) string {
|
||||
return strings.Replace(value, "\n COMMENT '", "\n COMMENT 'é", 1)
|
||||
}, "无法无损还原"},
|
||||
} {
|
||||
t.Run(item.name, func(t *testing.T) {
|
||||
definition, err := readMySQLStoredDefinition(t.Context(), tx, routine.kind, routine.name, column)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
definition[column] = item.alter(definition[column])
|
||||
original := definition[column]
|
||||
err = normalizeMySQLRoutineMetadata(t.Context(), tx, routine, definition, column)
|
||||
if err == nil || !strings.Contains(err.Error(), item.message) {
|
||||
t.Fatalf("无效例程元数据必须明确拒绝:%v", err)
|
||||
}
|
||||
if definition[column] != original {
|
||||
t.Fatal("失败时不能写回部分转换的定义")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func readMySQLRoutineDefinitions(t *testing.T, db *sql.DB) string {
|
||||
t.Helper()
|
||||
queries := []string{
|
||||
|
||||
Reference in new issue
Block a user