Skip to content

Commit

Permalink
Improve compatibility with Aiven DB (#1413)
Browse files Browse the repository at this point in the history
* Try using a set of default database names when initializing admin connection

Aiven DB is a PostgreSQL compatible DB, but comes with a different default DB name
  • Loading branch information
wxing1292 committed Mar 26, 2021
1 parent 1ff79f2 commit c34d9bb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
54 changes: 40 additions & 14 deletions common/persistence/sql/sqlplugin/postgresql/plugin.go
Expand Up @@ -25,11 +25,12 @@
package postgresql

import (
"errors"
"fmt"
"strings"

"github.com/iancoleman/strcase"
"github.com/jmoiron/sqlx"
"go.temporal.io/api/serviceerror"

"go.temporal.io/server/common/config"
"go.temporal.io/server/common/persistence/sql"
Expand All @@ -43,7 +44,12 @@ const (
dsnFmt = "postgres://%v:%v@%v/%v?%v"
)

var errTLSNotImplemented = errors.New("tls for postgresql has not been implemented")
var (
defaultDatabaseNames = []string{
"postgres", // normal PostgreSQL default DB name
"defaultdb", // special behavior for Aiven: #1389
}
)

type plugin struct{}

Expand Down Expand Up @@ -85,8 +91,11 @@ func (d *plugin) CreateAdminDB(
// 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
// the tables in the database
func (d *plugin) createDBConnection(cfg *config.SQL, r resolver.ServiceResolver) (*sqlx.DB, error) {
db, err := sqlx.Connect(PluginName, buildDSN(cfg, r))
func (d *plugin) createDBConnection(
cfg *config.SQL,
r resolver.ServiceResolver,
) (*sqlx.DB, error) {
db, err := d.tryConnect(cfg, r)
if err != nil {
return nil, err
}
Expand All @@ -105,24 +114,41 @@ func (d *plugin) createDBConnection(cfg *config.SQL, r resolver.ServiceResolver)
return db, nil
}

func buildDSN(cfg *config.SQL, r resolver.ServiceResolver) string {
func (d *plugin) tryConnect(
cfg *config.SQL,
r resolver.ServiceResolver,
) (*sqlx.DB, error) {
if cfg.DatabaseName != "" {
return sqlx.Connect(PluginName, buildDSN(cfg, r))
}

// database name not provided
// try defaults
defer func() { cfg.DatabaseName = "" }()
for _, databaseName := range defaultDatabaseNames {
cfg.DatabaseName = databaseName
if sqlxDB, err := sqlx.Connect(PluginName, buildDSN(cfg, r)); err == nil {
return sqlxDB, nil
}
}
return nil, serviceerror.NewInternal(
fmt.Sprintf("unable to connect to DB, tried default DB names: %v", strings.Join(defaultDatabaseNames, ",")),
)
}

func buildDSN(
cfg *config.SQL,
r resolver.ServiceResolver,
) string {
tlsAttrs := buildDSNAttr(cfg).Encode()
resolvedAddr := r.Resolve(cfg.ConnectAddr)[0]
dsn := fmt.Sprintf(
dsnFmt,
cfg.User,
cfg.Password,
resolvedAddr,
databaseName(cfg.DatabaseName),
cfg.DatabaseName,
tlsAttrs,
)
return dsn
}

func databaseName(dbName string) string {
//NOTE: postgres doesn't allow to connect with empty dbName, the admin dbName is "postgres"
if dbName == "" {
return "postgres"
}
return dbName
}
2 changes: 1 addition & 1 deletion tools/sql/clitest/connTest.go
Expand Up @@ -79,7 +79,7 @@ func (s *SQLConnTestSuite) SetupSuite() {
conn, err := newTestConn("", s.host, s.port, s.pluginName)
if err != nil {
logger := log.NewDefaultLogger()
logger.Fatal("error creating sql conn, ", tag.Error(err))
logger.Fatal("error creating sql conn", tag.Error(err))
}
s.SetupSuiteBase(conn)
}
Expand Down

0 comments on commit c34d9bb

Please sign in to comment.