Skip to content

Commit 7959c7f

Browse files
authored
feat: use custom dialer when connecting to mysql (#5609)
When connecting to mysql use the Dialer taken from the provided Dependencies.
1 parent fb88155 commit 7959c7f

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/eclipse/paho.mqtt.golang v1.5.1
2020
github.com/fatih/color v1.15.0
2121
github.com/foxcpp/go-mockdns v0.0.0-20201212160233-ede2f9158d15
22-
github.com/go-sql-driver/mysql v1.5.0
22+
github.com/go-sql-driver/mysql v1.9.3
2323
github.com/gofrs/uuid v3.3.0+incompatible
2424
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec
2525
github.com/google/flatbuffers v25.9.23+incompatible
@@ -62,6 +62,7 @@ require (
6262
cloud.google.com/go/iam v1.2.2 // indirect
6363
cloud.google.com/go/longrunning v0.6.2 // indirect
6464
cloud.google.com/go/monitoring v1.21.2 // indirect
65+
filippo.io/edwards25519 v1.1.0 // indirect
6566
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
6667
github.com/99designs/keyring v1.2.2 // indirect
6768
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo
4040
cloud.google.com/go/storage v1.43.0 h1:CcxnSohZwizt4LCzQHWvBf1/kvtHUn7gk9QERXPyXFs=
4141
cloud.google.com/go/storage v1.43.0/go.mod h1:ajvxEa7WmZS1PxvKRq4bq0tFT3vMd502JwstCcYv0Q0=
4242
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
43+
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
44+
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
4345
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs=
4446
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4=
4547
github.com/99designs/keyring v1.2.2 h1:pZd3neh/EmUzWONb35LxQfvuY7kiSXAq3HQd97+XBn0=
@@ -197,8 +199,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
197199
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
198200
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
199201
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
200-
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
201-
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
202+
github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo=
203+
github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
202204
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
203205
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
204206
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0=

stdlib/sql/from_private_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,30 @@ func TestMssqlOpenFunctionDialer(t *testing.T) {
249249
}
250250
}
251251

252+
func TestMysqlOpenFunctionDialer(t *testing.T) {
253+
expectErr := errors.New("test dial error")
254+
deps := mockDeps{
255+
Deps: flux.NewDefaultDependencies(),
256+
dialer: &mockDialer{err: expectErr},
257+
}
258+
259+
openFn := mysqlOpenFunction("username:password@tcp(localhost:3306)/dbname")
260+
db, err := openFn(deps)
261+
if err != nil {
262+
t.Fatalf("unexpected error from open function: %v", err)
263+
}
264+
defer db.Close()
265+
266+
// Ping triggers a real connection attempt, which will use our mock dialer.
267+
err = db.Ping()
268+
if err == nil {
269+
t.Fatal("expected error from Ping, got nil")
270+
}
271+
if !errors.Is(err, expectErr) {
272+
t.Fatalf("expected error %q, got: %v", expectErr, err)
273+
}
274+
}
275+
252276
func TestFromSqliteUrlValidation(t *testing.T) {
253277
testCases := executetest.SourceUrlValidationTestCases{
254278
{

stdlib/sql/mysql.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"time"
99

10+
"github.com/go-sql-driver/mysql"
1011
"github.com/influxdata/flux"
1112
"github.com/influxdata/flux/codes"
1213
"github.com/influxdata/flux/execute"
@@ -197,3 +198,22 @@ func MysqlColumnTranslateFunc() translationFunc {
197198
func mysqlQuoteIdent(name string) string {
198199
return fmt.Sprintf("`%s`", strings.ReplaceAll(name, "`", "``"))
199200
}
201+
202+
func mysqlOpenFunction(dataSourceName string) openFunc {
203+
return func(deps flux.Dependencies) (*sql.DB, error) {
204+
cfg, err := mysql.ParseDSN(dataSourceName)
205+
if err != nil {
206+
return nil, err
207+
}
208+
dialer, err := deps.Dialer()
209+
if err != nil {
210+
return nil, err
211+
}
212+
cfg.DialFunc = dialer.DialContext
213+
connector, err := mysql.NewConnector(cfg)
214+
if err != nil {
215+
return nil, err
216+
}
217+
return sql.OpenDB(connector), nil
218+
}
219+
}

stdlib/sql/open.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func getOpenFunc(driverName, dataSourceName string) openFunc {
4040
switch driverName {
4141
case "mssql", "sqlserver":
4242
return mssqlOpenFunction(dataSourceName)
43+
case "mysql":
44+
return mysqlOpenFunction(dataSourceName)
4345
case "postgres":
4446
return postgresOpenFunction(dataSourceName)
4547
default:

0 commit comments

Comments
 (0)