Skip to content

Commit 92eab81

Browse files
authored
fix(database/gdb): add compatibility for old configiration with both Type and part of Link configurations (#4058)
1 parent 67a9db9 commit 92eab81

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

database/gdb/gdb_core_config.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package gdb
88

99
import (
10+
"fmt"
1011
"sync"
1112
"time"
1213

@@ -381,14 +382,22 @@ func (c *Core) GetSchema() string {
381382
}
382383

383384
func parseConfigNodeLink(node *ConfigNode) (*ConfigNode, error) {
384-
var match []string
385-
if node.Link != "" {
386-
match, _ = gregex.MatchString(linkPattern, node.Link)
385+
var (
386+
link = node.Link
387+
match []string
388+
)
389+
if link != "" {
390+
// To be compatible with old configuration,
391+
// it checks and converts the link to new configuration.
392+
if node.Type != "" && !gstr.HasPrefix(link, node.Type+":") {
393+
link = fmt.Sprintf("%s:%s", node.Type, link)
394+
}
395+
match, _ = gregex.MatchString(linkPattern, link)
387396
if len(match) <= 5 {
388397
return nil, gerror.NewCodef(
389398
gcode.CodeInvalidParameter,
390399
`invalid link configuration: %s, shuold be pattern like: %s`,
391-
node.Link, linkPatternDescription,
400+
link, linkPatternDescription,
392401
)
393402
}
394403
node.Type = match[1]
@@ -412,7 +421,6 @@ func parseConfigNodeLink(node *ConfigNode) (*ConfigNode, error) {
412421
if len(match) > 6 && match[7] != "" {
413422
node.Extra = match[7]
414423
}
415-
node.Link = ""
416424
}
417425
if node.Extra != "" {
418426
if m, _ := gstr.Parse(node.Extra); len(m) > 0 {

database/gdb/gdb_z_mysql_internal_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,23 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
278278
t.Assert(newNode.Charset, `utf8`)
279279
t.Assert(newNode.Protocol, `unix`)
280280
})
281+
gtest.C(t, func(t *gtest.T) {
282+
node := &ConfigNode{
283+
Type: "mysql",
284+
Link: "username:password@unix(/tmp/mysql.sock)/dbname",
285+
}
286+
newNode, err := parseConfigNodeLink(node)
287+
t.AssertNil(err)
288+
t.Assert(newNode.Type, `mysql`)
289+
t.Assert(newNode.User, `username`)
290+
t.Assert(newNode.Pass, `password`)
291+
t.Assert(newNode.Host, `/tmp/mysql.sock`)
292+
t.Assert(newNode.Port, ``)
293+
t.Assert(newNode.Name, `dbname`)
294+
t.Assert(newNode.Extra, ``)
295+
t.Assert(newNode.Charset, `utf8`)
296+
t.Assert(newNode.Protocol, `unix`)
297+
})
281298
}
282299

283300
func Test_Func_doQuoteWord(t *testing.T) {

0 commit comments

Comments
 (0)