Skip to content

Commit

Permalink
Use pop store to ping the db instead of making a new connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hobson committed Nov 7, 2022
1 parent e91e2a7 commit 9f0e0e4
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pkg/cli/dbconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/rds"
pop "github.com/gobuffalo/pop/v6"
"github.com/jmoiron/sqlx"
"github.com/luna-duclos/instrumentedsql"
"github.com/pkg/errors"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -391,6 +390,10 @@ func InitDatabase(v *viper.Viper, creds *credentials.Credentials, logger *zap.Lo
return connection, nil
}

type pinger interface {
Ping() error
}

// testConnection tests the connection to determine successful ping
func testConnection(dbConnDetails *pop.ConnectionDetails, useIam bool, logger *zap.Logger) error {
// Copy connection info as we don't want to alter connection info
Expand Down Expand Up @@ -433,16 +436,15 @@ func testConnection(dbConnDetails *pop.ConnectionDetails, useIam bool, logger *z
return err
}

// Check the connection
db, err := sqlx.Open(connection.Dialect.Details().Dialect, connection.Dialect.URL())
if err != nil {
logger.Warn("Failed to open DB by driver name", zap.Error(err))
return err
dbWithPinger, ok := connection.Store.(pinger)
if !ok {
logger.Error("Failed to convert to pinger interface")
return errors.New("Failed to convert to pinger interface")
}

// Make the db ping
logger.Info("Starting database ping....")
err = db.Ping()
err = dbWithPinger.Ping()
if err != nil {
logger.Warn("Failed to ping DB connection", zap.Error(err))
return err
Expand Down

0 comments on commit 9f0e0e4

Please sign in to comment.