Skip to content

Commit

Permalink
Merge pull request #1150 from ashfall/parse-time-true
Browse files Browse the repository at this point in the history
Auto-parse the DB URL and add parseTime=true if the backend is mysql
  • Loading branch information
cyli authored May 9, 2017
2 parents 49d855c + 8cafc48 commit 23eceed
Show file tree
Hide file tree
Showing 16 changed files with 1,480 additions and 470 deletions.
11 changes: 11 additions & 0 deletions utils/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/Sirupsen/logrus"
"github.com/bugsnag/bugsnag-go"
"github.com/docker/go-connections/tlsconfig"
"github.com/go-sql-driver/mysql"
"github.com/spf13/viper"

"github.com/docker/notary"
Expand Down Expand Up @@ -109,6 +110,16 @@ func ParseSQLStorage(configuration *viper.Viper) (*Storage, error) {
"must provide a non-empty database source for %s",
store.Backend,
)
case store.Backend == notary.MySQLBackend:
urlConfig, err := mysql.ParseDSN(store.Source)
if err != nil {
return nil, fmt.Errorf("failed to parse the database source for %s",
store.Backend,
)
}

urlConfig.ParseTime = true
store.Source = urlConfig.FormatDSN()
}
return &store, nil
}
Expand Down
18 changes: 16 additions & 2 deletions utils/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ func TestParseInvalidSQLStorageNoDBSource(t *testing.T) {
}
}

// If an invalid DB source is provided, an error is returned.
func TestParseInvalidDBSourceInSQLStorage(t *testing.T) {
config := configure(`{
"storage": {
"backend": "mysql",
"db_url": "foobar"
}
}`)
_, err := ParseSQLStorage(config)
require.Error(t, err)
require.Contains(t, err.Error(),
fmt.Sprintf("failed to parse the database source for mysql"))
}

// A supported backend with DB source will be successfully parsed.
func TestParseSQLStorageDBStore(t *testing.T) {
config := configure(`{
Expand All @@ -203,7 +217,7 @@ func TestParseSQLStorageDBStore(t *testing.T) {

expected := Storage{
Backend: "mysql",
Source: "username:passord@tcp(hostname:1234)/dbname",
Source: "username:passord@tcp(hostname:1234)/dbname?parseTime=true",
}

store, err := ParseSQLStorage(config)
Expand Down Expand Up @@ -333,7 +347,7 @@ func TestParseSQLStorageWithEnvironmentVariables(t *testing.T) {

expected := Storage{
Backend: "mysql",
Source: "username:passord@tcp(hostname:1234)/dbname",
Source: "username:passord@tcp(hostname:1234)/dbname?parseTime=true",
}

store, err := ParseSQLStorage(config)
Expand Down
5 changes: 3 additions & 2 deletions vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ github.com/bugsnag/bugsnag-go 13fd6b8acda029830ef9904df6b63be0a83369d0
github.com/coreos/etcd 6acb3d67fbe131b3b2d5d010e00ec80182be4628
github.com/docker/distribution v2.6.0
github.com/docker/go-connections f549a9393d05688dff0992ef3efd8bbe6c628aeb
github.com/docker/go/canonical d30aec9fd63c35133f8f79c3412ad91a3b08be06
github.com/docker/go d30aec9fd63c35133f8f79c3412ad91a3b08be06
github.com/dvsekhvalnov/jose2go v1.2
github.com/go-sql-driver/mysql 0cc29e9fe8e25c2c58cf47bcab566e029bbaa88b
github.com/go-sql-driver/mysql v1.3
github.com/gorilla/mux e444e69cbd2e2e3e0749a2f3c717cec491552bbf
github.com/jinzhu/gorm 5409931a1bb87e484d68d649af9367c207713ea2
github.com/jinzhu/inflection 1c35d901db3da928c72a72d8458480cc9ade058f
Expand All @@ -24,6 +24,7 @@ golang.org/x/crypto 5bcd134fee4dd1475da17714aac19c0aa0142e2f
golang.org/x/net 6a513affb38dc9788b449d59ffed099b8de18fa0
google.golang.org/grpc v1.0.5


gopkg.in/dancannon/gorethink.v3 v3.0.0
# dependencies of gorethink.v3
gopkg.in/gorethink/gorethink.v2 v2.2.2
Expand Down
Loading

0 comments on commit 23eceed

Please sign in to comment.