Skip to content

Commit

Permalink
Fix in the URL parser with go 1.12.8 and github.com/go-sql-driver/mysql
Browse files Browse the repository at this point in the history
Change schemeFromURL to just split the url by :// to find the scheme.
It's not required to parse the whole URL. MySQL DSNs aren't valid URLs.

Fixes golang-migrate#264
  • Loading branch information
erikdubbelboer committed Aug 14, 2019
1 parent b071731 commit b5f8430
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 4 additions & 6 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,13 @@ func schemeFromURL(url string) (string, error) {
return "", errEmptyURL
}

u, err := nurl.Parse(url)
if err != nil {
return "", err
}
if len(u.Scheme) == 0 {
parts := strings.SplitN(url, "://", 2)

if len(parts) < 2 || parts[0] == "" {
return "", errNoScheme
}

return u.Scheme, nil
return parts[0], nil
}

// FilterCustomQuery filters all query values starting with `x-`
Expand Down
13 changes: 13 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ func TestDatabaseSchemeFromUrlSuccess(t *testing.T) {
}
}

func TestDatabaseSchemeFromUrlIssue264(t *testing.T) {
urlStr := "mysql://user:pass@tcp(host:1337)/db"
expected := "mysql"

u, err := databaseSchemeFromURL(urlStr)
if err != nil {
t.Fatalf("expected no error, but received %q", err)
}
if u != expected {
t.Fatalf("expected %q, but received %q", expected, u)
}
}

func TestDatabaseSchemeFromUrlFailure(t *testing.T) {
cases := []struct {
name string
Expand Down

0 comments on commit b5f8430

Please sign in to comment.