refact: Add db.DB wrapper and refact all test fixtures to use it. (#1535)
* refact: Changes required for gorm v2 * refact: Add db.DB wrapper and refact all test fixtures to use it Stop leaking the gorm abstraction within our unit tests. * refact: Migrate direct calls to pq to pgx driver The refactor will remove the remaining direct dependencies on the pq driver which has been deprecated in favor of the pgx driver. It includes the addition of common.SqlOpen which is a wrapper around sql.Open, which is needed to deal with the driver name changing from "postgres" to "pgx" but in our code base the dialect and driver are synonyms.
This commit is contained in:
77 files changed
+441
-321
No files matched your search
@@ -94,7 +94,7 @@ func (b *Server) CreateDevDatabase(ctx context.Context, opt ...Option) error {
|
||||
b.Info["dev database container"] = strings.TrimPrefix(container, "/")
|
||||
}
|
||||
|
||||
if err := b.ConnectToDatabase(dialect); err != nil {
|
||||
if err := b.ConnectToDatabase(ctx, dialect); err != nil {
|
||||
if c != nil {
|
||||
err = multierror.Append(err, c())
|
||||
}
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db/common"
|
||||
_ "github.com/jackc/pgx/v4/stdlib"
|
||||
"github.com/ory/dockertest/v3"
|
||||
)
|
||||
|
||||
@@ -37,7 +38,7 @@ func startDbInDockerSupported(dialect string, opt ...Option) (cleanup func() err
|
||||
}
|
||||
|
||||
switch dialect {
|
||||
case "postgres":
|
||||
case "postgres", "pgx":
|
||||
switch {
|
||||
case os.Getenv("BOUNDARY_TESTING_PG_URL") != "":
|
||||
url = os.Getenv("BOUNDARY_TESTING_PG_URL")
|
||||
@@ -68,7 +69,7 @@ func startDbInDockerSupported(dialect string, opt ...Option) (cleanup func() err
|
||||
}
|
||||
|
||||
if err := pool.Retry(func() error {
|
||||
db, err := sql.Open(dialect, url)
|
||||
db, err := common.SqlOpen(dialect, url)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening %s dev container: %w", dialect, err)
|
||||
}
|
||||
|
||||
@@ -39,8 +39,6 @@ import (
|
||||
"github.com/hashicorp/go-secure-stdlib/strutil"
|
||||
"github.com/mitchellh/cli"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -120,7 +118,7 @@ type Server struct {
|
||||
DatabaseMaxOpenConnections int
|
||||
DevDatabaseCleanupFunc func() error
|
||||
|
||||
Database *gorm.DB
|
||||
Database *db.DB
|
||||
|
||||
// StatusGracePeriodDuration represents the period of time (as a
|
||||
// duration) that the controller will wait before marking
|
||||
@@ -548,38 +546,22 @@ func (b *Server) RunShutdownFuncs() error {
|
||||
return mErr.ErrorOrNil()
|
||||
}
|
||||
|
||||
func (b *Server) ConnectToDatabase(dialect string) error {
|
||||
func (b *Server) ConnectToDatabase(ctx context.Context, dialect string) error {
|
||||
dbType, err := db.StringToDbType(dialect)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create db object with dialect %s: %w", dialect, err)
|
||||
}
|
||||
dbase, err := db.Open(dbType, b.DatabaseUrl)
|
||||
opts := []db.Option{
|
||||
db.WithMaxOpenConnections(b.DatabaseMaxOpenConnections),
|
||||
}
|
||||
if os.Getenv("BOUNDARY_DISABLE_GORM_FORMATTER") == "" {
|
||||
opts = append(opts, db.WithGormFormatter(b.Logger))
|
||||
}
|
||||
dbase, err := db.Open(dbType, b.DatabaseUrl, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create db object with dialect %s: %w", dialect, err)
|
||||
}
|
||||
if b.DatabaseMaxOpenConnections == 1 {
|
||||
return fmt.Errorf("unable to create db object with dialect %s: %s", dialect, "max_open_connections must be unlimited by setting 0 or at least 2")
|
||||
} else {
|
||||
underlyingDB, err := dbase.DB()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable retreive db: %w", err)
|
||||
}
|
||||
underlyingDB.SetMaxOpenConns(b.DatabaseMaxOpenConnections)
|
||||
}
|
||||
b.Database = dbase
|
||||
if os.Getenv("BOUNDARY_DISABLE_GORM_FORMATTER") == "" {
|
||||
newLogger := logger.New(
|
||||
db.GetGormLogger(b.Logger),
|
||||
logger.Config{
|
||||
LogLevel: logger.Error, // Log level
|
||||
Colorful: false, // Disable color
|
||||
},
|
||||
)
|
||||
dbase = dbase.Session(&gorm.Session{Logger: newLogger})
|
||||
}
|
||||
|
||||
b.Database = dbase
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -618,13 +600,11 @@ func (b *Server) CreateGlobalKmsKeys(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Server) DestroyDevDatabase() error {
|
||||
func (b *Server) DestroyDevDatabase(ctx context.Context) error {
|
||||
if b.Database != nil {
|
||||
underlyingDB, err := b.Database.DB()
|
||||
if err != nil {
|
||||
if err := b.Database.Close(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
underlyingDB.Close()
|
||||
}
|
||||
if b.DevDatabaseCleanupFunc != nil {
|
||||
return b.DevDatabaseCleanupFunc()
|
||||
|
||||
Reference in new issue
Block a user