Skip to content

Commit

Permalink
MySQL DSN no longer strips driver options (#91)
Browse files Browse the repository at this point in the history
* MySQL DSN no longer strips driver options
Fixes #90

* CHANGELOG update
  • Loading branch information
neilotoole authored Mar 13, 2021
1 parent b7cb0a0 commit 2d843c9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## v0.15.3
- [#91](https://github.com/neilotoole/sq/pull/91): MySQL driver options no longer stripped

## v0.15.2
- [#89](https://github.com/neilotoole/sq/pull/89): Bug with SQL generated for joins.

Expand Down
47 changes: 47 additions & 0 deletions drivers/mysql/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/source"
)

var KindFromDBTypeName = kindFromDBTypeName
Expand Down Expand Up @@ -43,3 +44,49 @@ func TestHasErrCode(t *testing.T) {
err = errz.Err(err)
require.True(t, hasErrCode(err, errNumTableNotExist))
}

func TestDSNFromLocation(t *testing.T) {
testCases := []struct {
loc string
wantDSN string
wantErr bool
}{
{
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest",
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest",
wantErr: false,
},
{
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest?allowOldPasswords=1",
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?allowOldPasswords=1",
wantErr: false,
},
{
loc: "mysql://sakila:p_ssW0rd@localhost:3306/sqtest?allowCleartextPasswords=true&allowOldPasswords=1",
wantDSN: "sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?allowCleartextPasswords=true&allowOldPasswords=1",
wantErr: false,
},
}

for _, tc := range testCases {
tc := tc

t.Run(tc.loc, func(t *testing.T) {
src := &source.Source{
Handle: "@testhandle",
Type: Type,
Location: tc.loc,
}

gotDSN, gotErr := dsnFromLocation(src)
if tc.wantErr {
require.Error(t, gotErr)
return
}

require.NoError(t, gotErr)
require.Equal(t, tc.wantDSN, gotDSN)
})
}

}
6 changes: 3 additions & 3 deletions drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ func dsnFromLocation(src *source.Source) (string, error) {
}

// Convert the location to the desired driver DSN.
// Location: mysql://sakila:p_ssW0rd@localhost:3306/sqtest
// Driver DSN: sakila:p_ssW0rd@tcp(localhost:3306)/sqtest
driverDSN := fmt.Sprintf("%s@tcp(%s)%s", u.User.String(), u.Host, u.Path)
// Location: mysql://sakila:p_ssW0rd@localhost:3306/sqtest?allowOldPasswords=1
// Driver DSN: sakila:p_ssW0rd@tcp(localhost:3306)/sqtest?allowOldPasswords=1
driverDSN := u.DSN

_, err = mysql.ParseDSN(driverDSN) // verify
if err != nil {
Expand Down

0 comments on commit 2d843c9

Please sign in to comment.