Allow for empty postgres db password
@@ -28,6 +28,7 @@ import (
"errors"
"fmt"
"net"
+ "strings"
"github.com/iancoleman/strcase"
"github.com/jmoiron/sqlx"
@@ -38,8 +39,8 @@ import (
)
const (
+ // PluginName is the name of the plugin
PluginName = "postgres"
- dataSourceNamePostgres = "user=%v password=%v host=%v port=%v dbname=%v sslmode=disable "
)
var errTLSNotImplemented = errors.New("tls for postgres has not been implemented")
@@ -72,6 +73,23 @@ func (d *plugin) CreateAdminDB(cfg *config.SQL) (sqlplugin.AdminDB, error) {
return db, nil
}
+func composeConnectionString(user, password, host, port, dbName string) string {
+ composeSegment := func(paramName string, paramValue string) string {
+ paramValue = strings.TrimSpace(paramValue)
+ if paramValue != "" {
+ return fmt.Sprintf("%s=%s ", paramName, paramValue)
+ }
+ return ""
+ }
+
+ return composeSegment("user", user) +
+ composeSegment("password", password) +
+ composeSegment("host", host) +
+ composeSegment("port", port) +
+ composeSegment("dbname", dbName) +
+ composeSegment("sslmode", "disable")
+}
+
// CreateDBConnection creates a returns a reference to a logical connection to the
// underlying SQL database. The returned object is to tied to a single
// SQL database and the object can be used to perform CRUD operations on
@@ -92,8 +110,8 @@ func (d *plugin) createDBConnection(cfg *config.SQL) (*sqlx.DB, error) {
if dbName == "" {
dbName = "postgres"
}
- db, err := sqlx.Connect(PluginName, fmt.Sprintf(dataSourceNamePostgres, cfg.User, cfg.Password, host, port, dbName))
+ db, err := sqlx.Connect(PluginName, composeConnectionString(cfg.User, cfg.Password, host, port, dbName))
if err != nil {
return nil, err
}
