Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(database/gdb): add compatibility for old configiration with both Type and part of Link configurations #4058

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading