1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

fix(contrib/drivers/pgsql): add unix socket connection support (#4028)

This commit is contained in:
John Guo 2024-12-11 09:58:26 +08:00 committed by GitHub
parent 69969b1723
commit b7c74c9a35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 226 additions and 230 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2017 john@goframe.org https://goframe.org
Copyright (c) 2017 GoFrame Team https://goframe.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -14,28 +14,12 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
)
// Open creates and returns an underlying sql.DB object for clickhouse.
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
source := config.Link
var source string
// clickhouse://username:password@host1:9000,host2:9000/database?dial_timeout=200ms&max_execution_time=60
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(replaceSchemaPattern, "@$1/"+config.Name, config.Link)
} else {
// If no schema, the link is matched for replacement
dbName, _ := gregex.MatchString(replaceSchemaPattern, config.Link)
if len(dbName) > 0 {
config.Name = dbName[len(dbName)-1]
}
}
} else {
if config.Pass != "" {
source = fmt.Sprintf(
"clickhouse://%s:%s@%s:%s/%s?debug=%t",
@ -51,7 +35,6 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
if config.Extra != "" {
source = fmt.Sprintf("%s&%s", source, config.Extra)
}
}
if db, err = sql.Open(driverName, source); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,

View File

@ -544,7 +544,7 @@ func TestDriverClickhouse_Open(t *testing.T) {
// link
// DSM
// clickhouse://username:password@host1:9000,host2:9000/database?dial_timeout=200ms&max_execution_time=60
link := "clickhouse://default@127.0.0.1:9000,127.0.0.1:9000/default?dial_timeout=200ms&max_execution_time=60"
link := "clickhouse:default:@tcp(127.0.0.1:9000)/default?dial_timeout=200ms&max_execution_time=60"
db, err := gdb.New(gdb.ConfigNode{
Link: link,
Type: "clickhouse",

View File

@ -13,41 +13,16 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)
// Open creates and returns an underlying sql.DB object for mssql.
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
var (
source string
underlyingDriverName = "sqlserver"
)
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(`database=([\w\.\-]+)+`, "database="+config.Name, source)
}
} else {
source = fmt.Sprintf(
"user id=%s;password=%s;server=%s;port=%s;database=%s;encrypt=disable",
config.User, config.Pass, config.Host, config.Port, config.Name,
)
if config.Extra != "" {
var extraMap map[string]interface{}
if extraMap, err = gstr.Parse(config.Extra); err != nil {
source, err := configNodeToSource(config)
if err != nil {
return nil, err
}
for k, v := range extraMap {
source += fmt.Sprintf(`;%s=%s`, k, v)
}
}
}
underlyingDriverName := "sqlserver"
if db, err = sql.Open(underlyingDriverName, source); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,
@ -57,3 +32,31 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
}
return
}
func configNodeToSource(config *gdb.ConfigNode) (string, error) {
var source string
source = fmt.Sprintf(
"user id=%s;password=%s;server=%s;encrypt=disable",
config.User, config.Pass, config.Host,
)
if config.Name != "" {
source = fmt.Sprintf("%s;database=%s", source, config.Name)
}
if config.Port != "" {
source = fmt.Sprintf("%s;port=%s", source, config.Port)
}
if config.Extra != "" {
extraMap, err := gstr.Parse(config.Extra)
if err != nil {
return "", gerror.WrapCodef(
gcode.CodeInvalidParameter,
err,
`invalid extra configuration: %s`, config.Extra,
)
}
for k, v := range extraMap {
source += fmt.Sprintf(`;%s=%s`, k, v)
}
}
return source, nil
}

View File

@ -15,7 +15,6 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
)
// Open creates and returns an underlying sql.DB object for mysql.
@ -35,20 +34,12 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
return
}
func configNodeToSource(config *gdb.ConfigNode) string {
var source string
// [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(`/([\w\.\-]+)+`, "/"+config.Name, source)
}
} else {
var portStr string
func configNodeToSource(config *gdb.ConfigNode) string {
var (
source string
portStr string
)
if config.Port != "" {
portStr = ":" + config.Port
}
@ -65,6 +56,5 @@ func configNodeToSource(config *gdb.ConfigNode) string {
if config.Extra != "" {
source = fmt.Sprintf("%s&%s", source, config.Extra)
}
}
return source
}

View File

@ -15,7 +15,6 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/util/gconv"
)
@ -35,16 +34,6 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
options["TRACE FILE"] = "oracle_trace.log"
}
// [username:[password]@]host[:port][/service_name][?param1=value1&...&paramN=valueN]
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(`@(.+?)/([\w\.\-]+)+`, "@$1/"+config.Name, source)
}
} else {
if config.Extra != "" {
// fix #3226
list := strings.Split(config.Extra, "&")
@ -58,7 +47,6 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
source = gora.BuildUrl(
config.Host, gconv.Int(config.Port), config.Name, config.User, config.Pass, options,
)
}
if db, err = sql.Open(underlyingDriverName, source); err != nil {
err = gerror.WrapCodef(

View File

@ -13,54 +13,17 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)
// Open creates and returns an underlying sql.DB object for pgsql.
// https://pkg.go.dev/github.com/lib/pq
func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
var (
source string
underlyingDriverName = "postgres"
)
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
// Custom changing the schema in runtime.
if config.Name != "" {
source, _ = gregex.ReplaceString(`dbname=([\w\.\-]+)+`, "dbname="+config.Name, source)
}
} else {
source = fmt.Sprintf(
"user=%s password='%s' host=%s port=%s sslmode=disable",
config.User, config.Pass, config.Host, config.Port)
if config.Name != "" {
source = fmt.Sprintf("%s dbname=%s", source, config.Name)
}
if config.Namespace != "" {
source = fmt.Sprintf("%s search_path=%s", source, config.Namespace)
}
if config.Timezone != "" {
source = fmt.Sprintf("%s timezone=%s", source, config.Timezone)
}
if config.Extra != "" {
var extraMap map[string]interface{}
if extraMap, err = gstr.Parse(config.Extra); err != nil {
source, err := configNodeToSource(config)
if err != nil {
return nil, err
}
for k, v := range extraMap {
source += fmt.Sprintf(` %s=%s`, k, v)
}
}
}
underlyingDriverName := "postgres"
if db, err = sql.Open(underlyingDriverName, source); err != nil {
err = gerror.WrapCodef(
gcode.CodeDbOperationError, err,
@ -70,3 +33,37 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
}
return
}
func configNodeToSource(config *gdb.ConfigNode) (string, error) {
var source string
source = fmt.Sprintf(
"user=%s password='%s' host=%s sslmode=disable",
config.User, config.Pass, config.Host,
)
if config.Port != "" {
source = fmt.Sprintf("%s port=%s", source, config.Port)
}
if config.Name != "" {
source = fmt.Sprintf("%s dbname=%s", source, config.Name)
}
if config.Namespace != "" {
source = fmt.Sprintf("%s search_path=%s", source, config.Namespace)
}
if config.Timezone != "" {
source = fmt.Sprintf("%s timezone=%s", source, config.Timezone)
}
if config.Extra != "" {
extraMap, err := gstr.Parse(config.Extra)
if err != nil {
return "", gerror.WrapCodef(
gcode.CodeInvalidParameter,
err,
`invalid extra configuration: %s`, config.Extra,
)
}
for k, v := range extraMap {
source += fmt.Sprintf(` %s=%s`, k, v)
}
}
return source, nil
}

View File

@ -26,19 +26,11 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
source string
underlyingDriverName = "sqlite"
)
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
} else {
source = config.Name
}
// It searches the source file to locate its absolute path..
if absolutePath, _ := gfile.Search(source); absolutePath != "" {
source = absolutePath
}
// Multiple PRAGMAs can be specified, e.g.:
// path/to/some.db?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)
if config.Extra != "" {

View File

@ -27,7 +27,7 @@ func Test_New(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
node := gdb.ConfigNode{
Type: "sqlite",
Link: gfile.Join(dbDir, "test.db"),
Name: gfile.Join(dbDir, "test.db"),
Charset: "utf8",
}
newDb, err := gdb.New(node)

View File

@ -34,14 +34,7 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
source string
underlyingDriverName = "sqlite3"
)
if config.Link != "" {
// ============================================================================
// Deprecated from v2.2.0.
// ============================================================================
source = config.Link
} else {
source = config.Name
}
// It searches the source file to locate its absolute path..
if absolutePath, _ := gfile.Search(source); absolutePath != "" {
source = absolutePath

View File

@ -27,7 +27,7 @@ func Test_New(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
node := gdb.ConfigNode{
Type: "sqlite",
Link: gfile.Join(dbDir, "test.db"),
Name: gfile.Join(dbDir, "test.db"),
Charset: "utf8",
}
newDb, err := gdb.New(node)

View File

@ -638,8 +638,8 @@ const (
ctxKeyCatchSQL gctx.StrKey = `CtxKeyCatchSQL`
ctxKeyInternalProducedSQL gctx.StrKey = `CtxKeyInternalProducedSQL`
// type:[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
linkPattern = `(\w+):([\w\-\$]*):(.*?)@(\w+?)\((.+?)\)/{0,1}([^\?]*)\?{0,1}(.*)`
linkPatternDescription = `type:username:password@protocol(host:port)/dbname?param1=value1&...&paramN=valueN`
)
type ctxTimeoutType int
@ -843,7 +843,10 @@ func NewByGroup(group ...string) (db DB, err error) {
// So all db type configurations in the same group should be the same.
func newDBByConfigNode(node *ConfigNode, group string) (db DB, err error) {
if node.Link != "" {
node = parseConfigNodeLink(node)
node, err = parseConfigNodeLink(node)
if err != nil {
return
}
}
c := &Core{
group: group,

View File

@ -10,6 +10,8 @@ import (
"sync"
"time"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/text/gregex"
@ -73,42 +75,64 @@ func init() {
// SetConfig sets the global configuration for package.
// It will overwrite the old configuration of package.
func SetConfig(config Config) {
func SetConfig(config Config) error {
defer instances.Clear()
configs.Lock()
defer configs.Unlock()
for k, nodes := range config {
for i, node := range nodes {
nodes[i] = parseConfigNode(node)
parsedNode, err := parseConfigNode(node)
if err != nil {
return err
}
nodes[i] = parsedNode
}
config[k] = nodes
}
configs.config = config
return nil
}
// SetConfigGroup sets the configuration for given group.
func SetConfigGroup(group string, nodes ConfigGroup) {
func SetConfigGroup(group string, nodes ConfigGroup) error {
defer instances.Clear()
configs.Lock()
defer configs.Unlock()
for i, node := range nodes {
nodes[i] = parseConfigNode(node)
parsedNode, err := parseConfigNode(node)
if err != nil {
return err
}
nodes[i] = parsedNode
}
configs.config[group] = nodes
return nil
}
// AddConfigNode adds one node configuration to configuration of given group.
func AddConfigNode(group string, node ConfigNode) {
func AddConfigNode(group string, node ConfigNode) error {
defer instances.Clear()
configs.Lock()
defer configs.Unlock()
configs.config[group] = append(configs.config[group], parseConfigNode(node))
parsedNode, err := parseConfigNode(node)
if err != nil {
return err
}
configs.config[group] = append(configs.config[group], parsedNode)
return nil
}
// parseConfigNode parses `Link` configuration syntax.
func parseConfigNode(node ConfigNode) ConfigNode {
func parseConfigNode(node ConfigNode) (ConfigNode, error) {
if node.Link != "" {
node = *parseConfigNodeLink(&node)
parsedLinkNode, err := parseConfigNodeLink(&node)
if err != nil {
return node, err
}
node = *parsedLinkNode
}
if node.Link != "" && node.Type == "" {
match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.Link)
@ -117,17 +141,17 @@ func parseConfigNode(node ConfigNode) ConfigNode {
node.Link = gstr.Trim(match[2])
}
}
return node
return node, nil
}
// AddDefaultConfigNode adds one node configuration to configuration of default group.
func AddDefaultConfigNode(node ConfigNode) {
AddConfigNode(DefaultGroupName, node)
func AddDefaultConfigNode(node ConfigNode) error {
return AddConfigNode(DefaultGroupName, node)
}
// AddDefaultConfigGroup adds multiple node configurations to configuration of default group.
func AddDefaultConfigGroup(nodes ConfigGroup) {
SetConfigGroup(DefaultGroupName, nodes)
func AddDefaultConfigGroup(nodes ConfigGroup) error {
return SetConfigGroup(DefaultGroupName, nodes)
}
// GetConfig retrieves and returns the configuration of given group.
@ -266,11 +290,17 @@ func (c *Core) GetSchema() string {
return schema
}
func parseConfigNodeLink(node *ConfigNode) *ConfigNode {
func parseConfigNodeLink(node *ConfigNode) (*ConfigNode, error) {
var match []string
if node.Link != "" {
match, _ = gregex.MatchString(linkPattern, node.Link)
if len(match) > 5 {
if len(match) <= 5 {
return nil, gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid link configuration: %s, shuold be pattern like: %s`,
node.Link, linkPatternDescription,
)
}
node.Type = match[1]
node.User = match[2]
node.Pass = match[3]
@ -294,7 +324,6 @@ func parseConfigNodeLink(node *ConfigNode) *ConfigNode {
}
node.Link = ""
}
}
if node.Extra != "" {
if m, _ := gstr.Parse(node.Extra); len(m) > 0 {
_ = gconv.Struct(m, &node)
@ -307,5 +336,5 @@ func parseConfigNodeLink(node *ConfigNode) *ConfigNode {
if node.Protocol == "" {
node.Protocol = defaultProtocol
}
return node
return node, nil
}

View File

@ -53,7 +53,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss?loc=Local&parseTime=true&charset=latin`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `mysql`)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
@ -68,7 +69,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss?`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `mysql`)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
@ -83,7 +85,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/khaos_oss`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `mysql`)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
@ -99,7 +102,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/?loc=Local&parseTime=true&charset=latin`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `mysql`)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
@ -114,7 +118,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)?loc=Local&parseTime=true&charset=latin`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `mysql`)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
@ -129,7 +134,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)/`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `mysql`)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
@ -144,7 +150,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `mysql:root:CxzhD*624:27jh@tcp(9.135.69.119:3306)`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `mysql`)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
@ -160,7 +167,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `mysql:root:CxzhD*624:27jh@udp(9.135.69.119:3306)`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `mysql`)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
@ -175,7 +183,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `sqlite:root:CxzhD*624:27jh@file(/var/data/db.sqlite3)?local=Local&parseTime=true`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `sqlite`)
t.Assert(newNode.User, `root`)
t.Assert(newNode.Pass, `CxzhD*624:27jh`)
@ -190,7 +199,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `sqlite::CxzhD*624:2@7jh@file(/var/data/db.sqlite3)`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `sqlite`)
t.Assert(newNode.User, ``)
t.Assert(newNode.Pass, `CxzhD*624:2@7jh`)
@ -205,7 +215,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `sqlite::@file(/var/data/db.sqlite3)`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `sqlite`)
t.Assert(newNode.User, ``)
t.Assert(newNode.Pass, ``)
@ -221,7 +232,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: `pgsql:BASIC$xxxx:123456@tcp(xxxx.hologres.aliyuncs.com:80)/xxx`,
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `pgsql`)
t.Assert(newNode.User, `BASIC$xxxx`)
t.Assert(newNode.Pass, `123456`)
@ -237,7 +249,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: "mysql:user:pwd@tcp(rdsid.mysql.rds.aliyuncs.com)/dbname?charset=utf8&loc=Local",
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `mysql`)
t.Assert(newNode.User, `user`)
t.Assert(newNode.Pass, `pwd`)
@ -253,7 +266,8 @@ func Test_parseConfigNodeLink_WithType(t *testing.T) {
node := &ConfigNode{
Link: "mysql:username:password@unix(/tmp/mysql.sock)/dbname",
}
newNode := parseConfigNodeLink(node)
newNode, err := parseConfigNodeLink(node)
t.AssertNil(err)
t.Assert(newNode.Type, `mysql`)
t.Assert(newNode.User, `username`)
t.Assert(newNode.Pass, `password`)

View File

@ -91,7 +91,9 @@ func Database(name ...string) gdb.DB {
if len(cg) > 0 {
if gdb.GetConfig(group) == nil {
intlog.Printf(ctx, "add configuration for group: %s, %#v", g, cg)
gdb.SetConfigGroup(g, cg)
if err := gdb.SetConfigGroup(g, cg); err != nil {
panic(err)
}
} else {
intlog.Printf(ctx, "ignore configuration as it already exists for group: %s, %#v", g, cg)
intlog.Printf(ctx, "%s, %#v", g, cg)
@ -108,7 +110,9 @@ func Database(name ...string) gdb.DB {
if len(cg) > 0 {
if gdb.GetConfig(group) == nil {
intlog.Printf(ctx, "add configuration for group: %s, %#v", gdb.DefaultGroupName, cg)
gdb.SetConfigGroup(gdb.DefaultGroupName, cg)
if err := gdb.SetConfigGroup(gdb.DefaultGroupName, cg); err != nil {
panic(err)
}
} else {
intlog.Printf(
ctx,