fix: 兼容租户标准MySQL连接串

This commit is contained in:
richjohnrambo
2026-09-07 18:47:50 +08:00
parent 845f19fd1c
commit e5b8db41ea
2 changed files with 23 additions and 1 deletions
+4 -1
View File
@@ -148,7 +148,10 @@ func replaceMySQLDatabase(link string, dbName string) (string, error) {
func normalizeMySQLTimeOptions(link string) (string, error) {
if !strings.HasPrefix(link, "mysql:") {
return "", gerror.New("租户业务库仅支持 MySQL")
if !strings.Contains(link, "@tcp(") {
return "", gerror.New("租户业务库仅支持 MySQL")
}
link = "mysql:" + link
}
parts := strings.SplitN(link, "?", 2)
values := make(url.Values)
@@ -6,6 +6,25 @@ import (
"testing"
)
func TestNormalizeMySQLTimeOptions(t *testing.T) {
link, err := normalizeMySQLTimeOptions("user:pass@tcp(127.0.0.1:3306)/tinode?charset=utf8mb4")
if err != nil {
t.Fatalf("normalize raw mysql dsn: %v", err)
}
if !strings.HasPrefix(link, "mysql:user:pass@tcp(127.0.0.1:3306)/tinode?") {
t.Fatalf("unexpected normalized link: %s", link)
}
if !strings.Contains(link, "loc=UTC") || !strings.Contains(link, "parseTime=true") {
t.Fatalf("missing required time options: %s", link)
}
}
func TestNormalizeMySQLTimeOptionsRejectsUnsupportedFormat(t *testing.T) {
if _, err := normalizeMySQLTimeOptions("postgres://user:pass@127.0.0.1:5432/tinode"); err == nil {
t.Fatal("expected unsupported format error")
}
}
func TestReplaceMySQLDatabaseUsesUTC(t *testing.T) {
link, err := replaceMySQLDatabase("mysql:user:pass@tcp(127.0.0.1:3306)/hotgo?loc=Local&parseTime=false&charset=utf8mb4", "tinode")
if err != nil {