Skip to content
This repository was archived by the owner on Oct 8, 2018. It is now read-only.

Commit ac45750

Browse files
author
David Wilkins
committed
Added unit test.
Signed-off-by: David Wilkins <[email protected]>
1 parent 1007344 commit ac45750

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

connector.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,12 @@ func (c *SQLConnection) GetDatabase() *sqlx.DB {
8282

8383
var err error
8484

85-
userinfo := c.URL.User.Username()
86-
if password, hasPassword := c.URL.User.Password(); hasPassword {
87-
userinfo = userinfo + ":" + password
88-
}
89-
c.URL.User = nil
9085
clean := cleanURLQuery(c.URL)
9186

9287
if err = retry(c.L, time.Second*15, time.Minute*2, func() error {
9388
c.L.Infof("Connecting with %s", c.URL.Scheme+"://*:*@"+c.URL.Host+c.URL.Path+"?"+clean.RawQuery)
9489

95-
if clean.Scheme == "mysql" {
96-
q := clean.Query()
97-
q.Set("parseTime", "true")
98-
clean.RawQuery = q.Encode()
99-
}
100-
u := clean.String()
101-
if strings.HasPrefix(u, clean.Scheme+"://") {
102-
u = strings.Replace(u, clean.Scheme+"://", clean.Scheme+"://"+userinfo+"@", 1)
103-
}
104-
if clean.Scheme == "mysql" {
105-
u = strings.Replace(u, "mysql://", "", -1)
106-
}
90+
u := connectionString(clean)
10791
if c.db, err = sqlx.Open(clean.Scheme, u); err != nil {
10892
return errors.Errorf("Could not Connect to SQL: %s", err)
10993
} else if err := c.db.Ping(); err != nil {
@@ -161,3 +145,29 @@ func maxParallelism() int {
161145
}
162146
return numCPU
163147
}
148+
149+
func connectionString(clean *url.URL) string {
150+
if clean.Scheme == "mysql" {
151+
q := clean.Query()
152+
q.Set("parseTime", "true")
153+
clean.RawQuery = q.Encode()
154+
}
155+
156+
username := clean.User.Username()
157+
userinfo := username
158+
password, hasPassword := clean.User.Password()
159+
if hasPassword {
160+
userinfo = userinfo + ":" + password
161+
}
162+
clean.User = nil
163+
u := clean.String()
164+
clean.User = url.UserPassword(username, password)
165+
166+
if strings.HasPrefix(u, clean.Scheme+"://") {
167+
u = strings.Replace(u, clean.Scheme+"://", clean.Scheme+"://"+userinfo+"@", 1)
168+
}
169+
if clean.Scheme == "mysql" {
170+
u = strings.Replace(u, "mysql://", "", -1)
171+
}
172+
return u
173+
}

connector_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ func TestCleanQueryURL(t *testing.T) {
7878
assert.Equal(t, false, strings.Contains(b.String(), "max_conn_lifetime"))
7979
}
8080

81+
func TestConnectionString(t *testing.T) {
82+
a, _ := url.Parse("mysql://foo@bar:baz@qux/db")
83+
b := connectionString(a)
84+
assert.NotEqual(t, b, a.String())
85+
assert.True(t, strings.HasPrefix(b, "foo@bar:baz@qux"))
86+
}
87+
8188
func mustSQL(t *testing.T, db string) *SQLConnection {
8289
c, err := NewSQLConnection(db, logrus.New())
8390
require.NoError(t, err)

0 commit comments

Comments
 (0)