mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
pull connection string args from url instead
This commit is contained in:
parent
a693d42e31
commit
5bc8d50864
@ -195,6 +195,23 @@ func (ss *SqlStore) ensureAdminUser() error {
|
|||||||
return err
|
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) {
|
func (ss *SqlStore) buildConnectionString() (string, error) {
|
||||||
cnnstr := ss.dbCfg.ConnectionString
|
cnnstr := ss.dbCfg.ConnectionString
|
||||||
|
|
||||||
@ -222,9 +239,7 @@ func (ss *SqlStore) buildConnectionString() (string, error) {
|
|||||||
cnnstr += "&tls=custom"
|
cnnstr += "&tls=custom"
|
||||||
}
|
}
|
||||||
|
|
||||||
if ss.dbCfg.ExtraConnectionStringArgs != "" {
|
cnnstr += ss.buildExtraConnectionString('&')
|
||||||
cnnstr += "&" + ss.dbCfg.ExtraConnectionStringArgs
|
|
||||||
}
|
|
||||||
case migrator.POSTGRES:
|
case migrator.POSTGRES:
|
||||||
var host, port = "127.0.0.1", "5432"
|
var host, port = "127.0.0.1", "5432"
|
||||||
fields := strings.Split(ss.dbCfg.Host, ":")
|
fields := strings.Split(ss.dbCfg.Host, ":")
|
||||||
@ -241,19 +256,15 @@ func (ss *SqlStore) buildConnectionString() (string, error) {
|
|||||||
ss.dbCfg.User = "''"
|
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)
|
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:
|
case migrator.SQLITE:
|
||||||
// special case for tests
|
// special case for tests
|
||||||
if !filepath.IsAbs(ss.dbCfg.Path) {
|
if !filepath.IsAbs(ss.dbCfg.Path) {
|
||||||
ss.dbCfg.Path = filepath.Join(ss.Cfg.DataPath, ss.dbCfg.Path)
|
ss.dbCfg.Path = filepath.Join(ss.Cfg.DataPath, ss.dbCfg.Path)
|
||||||
}
|
}
|
||||||
os.MkdirAll(path.Dir(ss.dbCfg.Path), os.ModePerm)
|
os.MkdirAll(path.Dir(ss.dbCfg.Path), os.ModePerm)
|
||||||
cnnstr = fmt.Sprintf("file:%s?cache=%s&mode=rwc", ss.dbCfg.Path, ss.dbCfg.CacheMode)
|
cnnstr += ss.buildExtraConnectionString('&')
|
||||||
if ss.dbCfg.ExtraConnectionStringArgs != "" {
|
|
||||||
cnnstr += "&" + ss.dbCfg.ExtraConnectionStringArgs
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("Unknown database type: %s", ss.dbCfg.Type)
|
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.User = userInfo.Username()
|
||||||
ss.dbCfg.Pwd, _ = userInfo.Password()
|
ss.dbCfg.Pwd, _ = userInfo.Password()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ss.dbCfg.UrlQueryParams = dbURL.Query()
|
||||||
} else {
|
} else {
|
||||||
ss.dbCfg.Type = sec.Key("type").String()
|
ss.dbCfg.Type = sec.Key("type").String()
|
||||||
ss.dbCfg.Host = sec.Key("host").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.Path = sec.Key("path").MustString("data/grafana.db")
|
||||||
|
|
||||||
ss.dbCfg.CacheMode = sec.Key("cache_mode").MustString("private")
|
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 {
|
func InitTestDB(t *testing.T) *SqlStore {
|
||||||
@ -421,5 +432,5 @@ type DatabaseConfig struct {
|
|||||||
MaxIdleConn int
|
MaxIdleConn int
|
||||||
ConnMaxLifetime int
|
ConnMaxLifetime int
|
||||||
CacheMode string
|
CacheMode string
|
||||||
ExtraConnectionStringArgs string
|
UrlQueryParams map[string][]string
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user