Skip to content

Commit

Permalink
fix(database/gdb): add compatibility for old configiration with both …
Browse files Browse the repository at this point in the history
…`Type` and part of `Link` configurations (#4058)
  • Loading branch information
gqcn authored Dec 18, 2024
1 parent 67a9db9 commit 92eab81
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
18 changes: 13 additions & 5 deletions database/gdb/gdb_core_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gdb

import (
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -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]
Expand All @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions database/gdb/gdb_z_mysql_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 92eab81

Please sign in to comment.