Skip to content

Commit

Permalink
Merge pull request #6138 from derekperkins/add-vitessdriver-config-op…
Browse files Browse the repository at this point in the history
…tion

vitessdriver: allow overriding the driver name
  • Loading branch information
sougou authored May 2, 2020
2 parents 9fe7fd3 + 13437ec commit 3079017
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 11 additions & 1 deletion go/vt/vitessdriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func OpenWithConfiguration(c Configuration) (*sql.DB, error) {
vtgateconn.RegisterDialer(c.Protocol, grpcvtgateconn.DialWithOpts(context.TODO(), c.GRPCDialOptions...))
}

return sql.Open("vitess", json)
return sql.Open(c.DriverName, json)
}

type drv struct {
Expand Down Expand Up @@ -160,6 +160,12 @@ type Configuration struct {
//
// Default: none
GRPCDialOptions []grpc.DialOption `json:"-"`

// Driver is the name registered with the database/sql package. This override
// is here in case you have wrapped the driver for stats or other interceptors.
//
// Default: "vitess"
DriverName string `json:"-"`
}

// toJSON converts Configuration to the JSON string which is required by the
Expand All @@ -179,6 +185,10 @@ func (c *Configuration) setDefaults() {
if c.Protocol == "" {
c.Protocol = "grpc"
}

if c.DriverName == "" {
c.DriverName = "vitess"
}
}

type conn struct {
Expand Down
14 changes: 9 additions & 5 deletions go/vt/vitessdriver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ func TestOpen(t *testing.T) {
connStr: fmt.Sprintf(`{"address": "%s", "target": "@replica", "timeout": %d}`, testAddress, int64(30*time.Second)),
conn: &conn{
Configuration: Configuration{
Protocol: "grpc",
Target: "@replica",
Protocol: "grpc",
DriverName: "vitess",
Target: "@replica",
},
convert: &converter{
location: time.UTC,
Expand All @@ -94,7 +95,8 @@ func TestOpen(t *testing.T) {
connStr: fmt.Sprintf(`{"address": "%s", "timeout": %d}`, testAddress, int64(30*time.Second)),
conn: &conn{
Configuration: Configuration{
Protocol: "grpc",
Protocol: "grpc",
DriverName: "vitess",
},
convert: &converter{
location: time.UTC,
Expand All @@ -106,8 +108,9 @@ func TestOpen(t *testing.T) {
connStr: fmt.Sprintf(`{"protocol": "grpc", "address": "%s", "target": "ks:0@replica", "timeout": %d}`, testAddress, int64(30*time.Second)),
conn: &conn{
Configuration: Configuration{
Protocol: "grpc",
Target: "ks:0@replica",
Protocol: "grpc",
DriverName: "vitess",
Target: "ks:0@replica",
},
convert: &converter{
location: time.UTC,
Expand All @@ -122,6 +125,7 @@ func TestOpen(t *testing.T) {
conn: &conn{
Configuration: Configuration{
Protocol: "grpc",
DriverName: "vitess",
DefaultLocation: "America/Los_Angeles",
},
convert: &converter{
Expand Down

0 comments on commit 3079017

Please sign in to comment.