Skip to content

Commit

Permalink
Apply ConnectAttributes for postgres (cadence-workflow#4639)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenerme authored Nov 13, 2021
1 parent e2adab9 commit 2957a70
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions common/persistence/sql/sqlplugin/postgres/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,19 @@ func (d *plugin) CreateAdminDB(cfg *config.SQL) (sqlplugin.AdminDB, error) {
// SQL database and the object can be used to perform CRUD operations on
// the tables in the database
func (d *plugin) createSingleDBConn(cfg *config.SQL) (*sqlx.DB, error) {
sslParams, err := registerTLSConfig(cfg)
params, err := registerTLSConfig(cfg)
if err != nil {
return nil, err
}

for k, v := range cfg.ConnectAttributes {
params.Set(k, v)
}
host, port, err := net.SplitHostPort(cfg.ConnectAddr)
if err != nil {
return nil, fmt.Errorf("invalid connect address, it must be in host:port format, %v, err: %v", cfg.ConnectAddr, err)
}

db, err := sqlx.Connect(PluginName, buildDSN(cfg, host, port, sslParams))
db, err := sqlx.Connect(PluginName, buildDSN(cfg, host, port, params))
if err != nil {
return nil, err
}
Expand All @@ -108,7 +110,7 @@ func (d *plugin) createSingleDBConn(cfg *config.SQL) (*sqlx.DB, error) {
return db, nil
}

func buildDSN(cfg *config.SQL, host string, port string, sslParams url.Values) string {
func buildDSN(cfg *config.SQL, host string, port string, params url.Values) string {
dbName := cfg.DatabaseName
//NOTE: postgres doesn't allow to connect with empty dbName, the admin dbName is "postgres"
if dbName == "" {
Expand All @@ -117,7 +119,7 @@ func buildDSN(cfg *config.SQL, host string, port string, sslParams url.Values) s

credentialString := generateCredentialString(cfg.User, cfg.Password)
dsn := fmt.Sprintf(dsnFmt, credentialString, host, port, dbName)
if attrs := sslParams.Encode(); attrs != "" {
if attrs := params.Encode(); attrs != "" {
dsn += "?" + attrs
}
return dsn
Expand Down

0 comments on commit 2957a70

Please sign in to comment.