Skip to content

Commit

Permalink
Merge a009718 into 78a252a
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 committed Jun 19, 2019
2 parents 78a252a + a009718 commit 62cc19d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 13 additions & 2 deletions database/dbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type DbConfig struct {
Password string
Database string
Protocol string
Parameters string
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime int
Expand All @@ -73,6 +74,7 @@ func ConfigureDbDefault(v cs.Configuration, prefix, envUser, envPasswd string) {
v.SetDefault(prefix+"-password", "")
v.SetDefault(prefix+"-database", "")
v.SetDefault(prefix+"-protocol", "")
v.SetDefault(prefix+"-parameters", "")
v.SetDefault(prefix+"-max-open-conns", 10)
v.SetDefault(prefix+"-max-idle-conns", 2)
v.SetDefault(prefix+"-conn-max-lifetime", 3600)
Expand All @@ -94,6 +96,7 @@ func GetDbConfig(v cs.Configuration, prefix string, noop bool) *DbConfig {
cfg.Password = v.GetString(prefix + "-password")
cfg.Database = v.GetString(prefix + "-database")
cfg.Protocol = v.GetString(prefix + "-protocol")
cfg.Parameters = v.GetString(prefix + "-parameters")
cfg.MaxOpenConns = v.GetInt(prefix + "-max-open-conns")
cfg.MaxIdleConns = v.GetInt(prefix + "-max-idle-conns")
cfg.ConnMaxLifetime = v.GetInt(prefix + "-conn-max-lifetime")
Expand All @@ -104,16 +107,24 @@ func GetDbConfig(v cs.Configuration, prefix string, noop bool) *DbConfig {
return &cfg
}

func (cfg *DbConfig) getDbConnectionString() string {
var separ = ""
if len(cfg.Parameters) > 0 {
separ = "?"
}
return fmt.Sprintf("%s:%s@%s(%s)/%s%s%s", cfg.Username, cfg.Password, cfg.Protocol, cfg.HostPort, cfg.Database, separ, cfg.Parameters)
}

// OpenDatabase gets an access to a database
// If cfg.Noop is true, a Noop access will be provided
func (cfg *DbConfig) OpenDatabase() (CloudtrustDB, error) {
if cfg.Noop {
return &NoopDB{}, nil
}

dbConn, err := sql.Open("mysql", fmt.Sprintf("%s:%s@%s(%s)/%s", cfg.Username, cfg.Password, cfg.Protocol, cfg.HostPort, cfg.Database))
dbConn, err := sql.Open("mysql", cfg.getDbConnectionString())
if err != nil {
return &NoopDB{}, err
return nil, err
}

// DB migration version
Expand Down
18 changes: 16 additions & 2 deletions database/dbase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ func TestDbVersion(t *testing.T) {
}
}

func TestGetDbConnectionString(t *testing.T) {
var conf = DbConfig{
Username: "user",
Password: "pass",
Protocol: "proto",
HostPort: "1234",
Database: "db",
}
assert.Equal(t, "user:pass@proto(1234)/db", conf.getDbConnectionString())

conf.Parameters = "params"
assert.Equal(t, "user:pass@proto(1234)/db?params", conf.getDbConnectionString())
}

func TestConfigureDbDefault(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
Expand All @@ -37,7 +51,7 @@ func TestConfigureDbDefault(t *testing.T) {
var envUser = "the env user"
var envPass = "the env password"

for _, suffix := range []string{"-host-port", "-username", "-password", "-database", "-protocol", "-max-open-conns", "-max-idle-conns", "-conn-max-lifetime", "-migration", "-migration-version"} {
for _, suffix := range []string{"-host-port", "-username", "-password", "-database", "-protocol", "-parameters", "-max-open-conns", "-max-idle-conns", "-conn-max-lifetime", "-migration", "-migration-version"} {
mockConf.EXPECT().SetDefault(prefix+suffix, gomock.Any()).Times(1)
}
mockConf.EXPECT().BindEnv(prefix+"-username", envUser).Times(1)
Expand All @@ -53,7 +67,7 @@ func TestGetDbConfig(t *testing.T) {

var prefix = "mydb"

for _, suffix := range []string{"-host-port", "-username", "-password", "-database", "-protocol"} {
for _, suffix := range []string{"-host-port", "-username", "-password", "-database", "-protocol", "-parameters"} {
mockConf.EXPECT().GetString(prefix + suffix).Return("value" + suffix).Times(1)
}
for _, suffix := range []string{"-max-open-conns", "-max-idle-conns", "-conn-max-lifetime"} {
Expand Down

0 comments on commit 62cc19d

Please sign in to comment.