MySQL DSN no longer strips driver options (#91)

* MySQL DSN no longer strips driver options
Fixes #90

* CHANGELOG update
This commit is contained in:
Neil O'Toole 2021-03-13 10:08:53 -07:00 committed by GitHub
parent b7cb0a0b66
commit 2d843c9550
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 3 deletions

View File

@ -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.

View File

@ -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
@ -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)
})
}
}

View File

@ -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 {