Add default port for mysql connections (#13)

The pq driver already supports this natively, but the mysql driver does not. This commit adds a default port 3306 for mysql connections.
This commit is contained in:
Adrian Macneil 2017-05-04 20:58:39 -07:00 committed by GitHub
parent e393f387b3
commit 9029bbbe3d
2 changed files with 26 additions and 1 deletions

View file

@ -16,6 +16,13 @@ type MySQLDriver struct {
func normalizeMySQLURL(u *url.URL) string {
normalizedURL := *u
normalizedURL.Scheme = ""
// set default port
if normalizedURL.Port() == "" {
normalizedURL.Host = fmt.Sprintf("%s:3306", normalizedURL.Host)
}
// host format required by go-sql-driver/mysql
normalizedURL.Host = fmt.Sprintf("tcp(%s)", normalizedURL.Host)
query := normalizedURL.Query()

View file

@ -9,7 +9,7 @@ import (
)
func mySQLTestURL(t *testing.T) *url.URL {
u, err := url.Parse("mysql://root:root@mysql:3306/dbmate")
u, err := url.Parse("mysql://root:root@mysql/dbmate")
require.Nil(t, err)
return u
@ -34,6 +34,24 @@ func prepTestMySQLDB(t *testing.T) *sql.DB {
return db
}
func TestNormalizeMySQLURLDefaults(t *testing.T) {
u, err := url.Parse("mysql://host/foo")
require.Nil(t, err)
require.Equal(t, "", u.Port())
s := normalizeMySQLURL(u)
require.Equal(t, "tcp(host:3306)/foo?multiStatements=true", s)
}
func TestNormalizeMySQLURLCustom(t *testing.T) {
u, err := url.Parse("mysql://bob:secret@host:123/foo?flag=on")
require.Nil(t, err)
require.Equal(t, "123", u.Port())
s := normalizeMySQLURL(u)
require.Equal(t, "bob:secret@tcp(host:123)/foo?flag=on&multiStatements=true", s)
}
func TestMySQLCreateDropDatabase(t *testing.T) {
drv := MySQLDriver{}
u := mySQLTestURL(t)