diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index 3a15024b42c..ae9c7c85ba2 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -7,6 +7,7 @@ package gdb import ( + "fmt" "sync" "time" @@ -381,14 +382,22 @@ func (c *Core) GetSchema() string { } func parseConfigNodeLink(node *ConfigNode) (*ConfigNode, error) { - var match []string - if node.Link != "" { - match, _ = gregex.MatchString(linkPattern, node.Link) + var ( + link = node.Link + match []string + ) + if link != "" { + // To be compatible with old configuration, + // it checks and converts the link to new configuration. + if node.Type != "" && !gstr.HasPrefix(link, node.Type+":") { + link = fmt.Sprintf("%s:%s", node.Type, link) + } + match, _ = gregex.MatchString(linkPattern, link) if len(match) <= 5 { return nil, gerror.NewCodef( gcode.CodeInvalidParameter, `invalid link configuration: %s, shuold be pattern like: %s`, - node.Link, linkPatternDescription, + link, linkPatternDescription, ) } node.Type = match[1] @@ -412,7 +421,6 @@ func parseConfigNodeLink(node *ConfigNode) (*ConfigNode, error) { if len(match) > 6 && match[7] != "" { node.Extra = match[7] } - node.Link = "" } if node.Extra != "" { if m, _ := gstr.Parse(node.Extra); len(m) > 0 { diff --git a/database/gdb/gdb_z_mysql_internal_test.go b/database/gdb/gdb_z_mysql_internal_test.go index b2ec6e83b95..ec70000b907 100644 --- a/database/gdb/gdb_z_mysql_internal_test.go +++ b/database/gdb/gdb_z_mysql_internal_test.go @@ -278,6 +278,23 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) { t.Assert(newNode.Charset, `utf8`) t.Assert(newNode.Protocol, `unix`) }) + gtest.C(t, func(t *gtest.T) { + node := &ConfigNode{ + Type: "mysql", + Link: "username:password@unix(/tmp/mysql.sock)/dbname", + } + newNode, err := parseConfigNodeLink(node) + t.AssertNil(err) + t.Assert(newNode.Type, `mysql`) + t.Assert(newNode.User, `username`) + t.Assert(newNode.Pass, `password`) + t.Assert(newNode.Host, `/tmp/mysql.sock`) + t.Assert(newNode.Port, ``) + t.Assert(newNode.Name, `dbname`) + t.Assert(newNode.Extra, ``) + t.Assert(newNode.Charset, `utf8`) + t.Assert(newNode.Protocol, `unix`) + }) } func Test_Func_doQuoteWord(t *testing.T) {