mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2026-02-02 17:35:08 +01:00
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:
parent
e393f387b3
commit
9029bbbe3d
2 changed files with 26 additions and 1 deletions
7
mysql.go
7
mysql.go
|
|
@ -16,6 +16,13 @@ type MySQLDriver struct {
|
||||||
func normalizeMySQLURL(u *url.URL) string {
|
func normalizeMySQLURL(u *url.URL) string {
|
||||||
normalizedURL := *u
|
normalizedURL := *u
|
||||||
normalizedURL.Scheme = ""
|
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)
|
normalizedURL.Host = fmt.Sprintf("tcp(%s)", normalizedURL.Host)
|
||||||
|
|
||||||
query := normalizedURL.Query()
|
query := normalizedURL.Query()
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func mySQLTestURL(t *testing.T) *url.URL {
|
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)
|
require.Nil(t, err)
|
||||||
|
|
||||||
return u
|
return u
|
||||||
|
|
@ -34,6 +34,24 @@ func prepTestMySQLDB(t *testing.T) *sql.DB {
|
||||||
return 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) {
|
func TestMySQLCreateDropDatabase(t *testing.T) {
|
||||||
drv := MySQLDriver{}
|
drv := MySQLDriver{}
|
||||||
u := mySQLTestURL(t)
|
u := mySQLTestURL(t)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue