Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-parse the DB URL and add parseTime=true if the backend is mysql #1150

Merged
merged 7 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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