pull connection string args from url instead

This commit is contained in:
tpetr 2019-01-04 23:52:36 -05:00
parent a693d42e31
commit 5bc8d50864

View File

@ -195,6 +195,23 @@ func (ss *SqlStore) ensureAdminUser() error {
return err
}
func (ss *SqlStore) buildExtraConnectionString(sep rune) string {
if ss.dbCfg.UrlQueryParams == nil {
return ""
}
var sb strings.Builder
for key, values := range ss.dbCfg.UrlQueryParams {
for _, value := range values {
sb.WriteRune(sep)
sb.WriteString(key)
sb.WriteRune('=')
sb.WriteString(value)
}
}
return sb.String()
}
func (ss *SqlStore) buildConnectionString() (string, error) {
cnnstr := ss.dbCfg.ConnectionString
@ -222,9 +239,7 @@ func (ss *SqlStore) buildConnectionString() (string, error) {
cnnstr += "&tls=custom"
}
if ss.dbCfg.ExtraConnectionStringArgs != "" {
cnnstr += "&" + ss.dbCfg.ExtraConnectionStringArgs
}
cnnstr += ss.buildExtraConnectionString('&')
case migrator.POSTGRES:
var host, port = "127.0.0.1", "5432"
fields := strings.Split(ss.dbCfg.Host, ":")
@ -241,19 +256,15 @@ func (ss *SqlStore) buildConnectionString() (string, error) {
ss.dbCfg.User = "''"
}
cnnstr = fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s sslcert=%s sslkey=%s sslrootcert=%s", ss.dbCfg.User, ss.dbCfg.Pwd, host, port, ss.dbCfg.Name, ss.dbCfg.SslMode, ss.dbCfg.ClientCertPath, ss.dbCfg.ClientKeyPath, ss.dbCfg.CaCertPath)
if ss.dbCfg.ExtraConnectionStringArgs != "" {
cnnstr += " " + ss.dbCfg.ExtraConnectionStringArgs
}
cnnstr += ss.buildExtraConnectionString(' ')
case migrator.SQLITE:
// special case for tests
if !filepath.IsAbs(ss.dbCfg.Path) {
ss.dbCfg.Path = filepath.Join(ss.Cfg.DataPath, ss.dbCfg.Path)
}
os.MkdirAll(path.Dir(ss.dbCfg.Path), os.ModePerm)
cnnstr = fmt.Sprintf("file:%s?cache=%s&mode=rwc", ss.dbCfg.Path, ss.dbCfg.CacheMode)
if ss.dbCfg.ExtraConnectionStringArgs != "" {
cnnstr += "&" + ss.dbCfg.ExtraConnectionStringArgs
}
cnnstr += ss.buildExtraConnectionString('&')
default:
return "", fmt.Errorf("Unknown database type: %s", ss.dbCfg.Type)
}
@ -310,6 +321,8 @@ func (ss *SqlStore) readConfig() {
ss.dbCfg.User = userInfo.Username()
ss.dbCfg.Pwd, _ = userInfo.Password()
}
ss.dbCfg.UrlQueryParams = dbURL.Query()
} else {
ss.dbCfg.Type = sec.Key("type").String()
ss.dbCfg.Host = sec.Key("host").String()
@ -331,8 +344,6 @@ func (ss *SqlStore) readConfig() {
ss.dbCfg.Path = sec.Key("path").MustString("data/grafana.db")
ss.dbCfg.CacheMode = sec.Key("cache_mode").MustString("private")
ss.dbCfg.ExtraConnectionStringArgs = sec.Key("extra_connection_string_args").String()
}
func InitTestDB(t *testing.T) *SqlStore {
@ -421,5 +432,5 @@ type DatabaseConfig struct {
MaxIdleConn int
ConnMaxLifetime int
CacheMode string
ExtraConnectionStringArgs string
UrlQueryParams map[string][]string
}