Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests #234

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions pkg/db/db_session/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"gorm.io/gorm/logger"
"k8s.io/klog/v2"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"
"github.com/openshift-online/maestro/pkg/config"
"github.com/openshift-online/maestro/pkg/db"
)
Expand Down Expand Up @@ -65,7 +67,7 @@ func (f *Test) Init(config *config.DatabaseConfig) {

func initDatabase(config *config.DatabaseConfig, migrate func(db2 *gorm.DB) error) error {
// - Connect to `template1` DB
dbx, g2, cleanup := connect("template1", config)
dbx, g2, cleanup := connect(config)
defer cleanup()

for _, err := dbx.Exec(`select 1`); err != nil; {
Expand All @@ -78,7 +80,7 @@ func initDatabase(config *config.DatabaseConfig, migrate func(db2 *gorm.DB) erro

func resetDB(config *config.DatabaseConfig) error {
// Reconnect to the default `postgres` database, so we can drop the existing db and recreate it
dbx, _, cleanup := connect("postgres", config)
dbx, _, cleanup := connect(config)
defer cleanup()

// Drop `all` connections to both `template1` and AMS DB, so it can be dropped and created
Expand All @@ -103,27 +105,24 @@ func resetDB(config *config.DatabaseConfig) error {
}

// connect to database specified by `name` and return connections + cleanup function
func connect(name string, config *config.DatabaseConfig) (*sql.DB, *gorm.DB, func()) {
func connect(config *config.DatabaseConfig) (*sql.DB, *gorm.DB, func()) {
var (
dbx *sql.DB
g2 *gorm.DB
err error
)

dbx, err = sql.Open(config.Dialect, config.ConnectionStringWithName(name, config.SSLMode != disable))
connConfig, err := pgx.ParseConfig(config.ConnectionString(config.SSLMode != disable))
if err != nil {
dbx, err = sql.Open(config.Dialect, config.ConnectionStringWithName(name, false))
if err != nil {
panic(fmt.Sprintf(
"SQL failed to connect to %s database %s with connection string: %s\nError: %s",
config.Dialect,
name,
config.LogSafeConnectionStringWithName(name, config.SSLMode != disable),
err.Error(),
))
}
panic(fmt.Sprintf(
"GORM failed to parse the connection string: %s\nError: %s",
config.LogSafeConnectionString(config.SSLMode != disable),
err.Error(),
))
}

dbx = stdlib.OpenDB(*connConfig, stdlib.OptionBeforeConnect(setPassword(config)))

// Connect GORM to use the same connection
conf := &gorm.Config{
PrepareStmt: false,
Expand Down Expand Up @@ -174,7 +173,7 @@ func connectFactory(config *config.DatabaseConfig) (*sql.DB, *gorm.DB) {
dbx *sql.DB
g2 *gorm.DB
)
dbx, g2, _ = connect(config.Name, config)
dbx, g2, _ = connect(config)
dbx.SetMaxOpenConns(config.MaxOpenConnections)

return dbx, g2
Expand Down