Skip to content
Open
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
35 changes: 32 additions & 3 deletions pkg/driver/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/amacneil/dbmate/v2/pkg/dbmate"
"github.com/amacneil/dbmate/v2/pkg/dbutil"

_ "github.com/go-sql-driver/mysql" // database/sql driver
"github.com/go-sql-driver/mysql"
)

// for mocking out during tests
Expand Down Expand Up @@ -81,9 +81,38 @@ func connectionString(u *url.URL) string {
return normalizedString
}

// parseConfig parses a DSN and silences the driver's own logger. Otherwise
// go-sql-driver prints lines like "packets.go:37: unexpected EOF" straight to
// stderr, which is noise during --wait where a failed connection is expected
// and dbmate already reports the real error itself.
func parseConfig(dsn string) (*mysql.Config, error) {
config, err := mysql.ParseDSN(dsn)
if err != nil {
return nil, err
}
config.Logger = &mysql.NopLogger{}

return config, nil
}

// openConnection opens a connection using the given DSN.
func openConnection(dsn string) (*sql.DB, error) {
config, err := parseConfig(dsn)
if err != nil {
return nil, err
}

connector, err := mysql.NewConnector(config)
if err != nil {
return nil, err
}

return sql.OpenDB(connector), nil
}

// Open creates a new database connection
func (drv *Driver) Open() (*sql.DB, error) {
return sql.Open("mysql", connectionString(drv.databaseURL))
return openConnection(connectionString(drv.databaseURL))
}

func (drv *Driver) openRootDB() (*sql.DB, error) {
Expand All @@ -96,7 +125,7 @@ func (drv *Driver) openRootDB() (*sql.DB, error) {
// connect to no particular database
rootURL.Path = "/"

return sql.Open("mysql", connectionString(rootURL))
return openConnection(connectionString(rootURL))
}

func (drv *Driver) quoteIdentifier(str string) string {
Expand Down
16 changes: 16 additions & 0 deletions pkg/driver/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/amacneil/dbmate/v2/pkg/dbtest"
"github.com/amacneil/dbmate/v2/pkg/dbutil"

"github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -118,6 +119,21 @@ func TestConnectionString(t *testing.T) {
})
}

func TestParseConfig(t *testing.T) {
t.Run("silences the driver logger", func(t *testing.T) {
u := dbtest.MustParseURL(t, "mysql://bob:secret@host:123/foo")

config, err := parseConfig(connectionString(u))
require.NoError(t, err)
require.IsType(t, &mysql.NopLogger{}, config.Logger)
})

t.Run("returns an error for an invalid dsn", func(t *testing.T) {
_, err := parseConfig("this is not a valid dsn")
require.Error(t, err)
})
}

func TestMySQLCreateDropDatabase(t *testing.T) {
drv := testMySQLDriver(t)

Expand Down