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 all commits
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 @@ -41,7 +43,7 @@ func NewTestFactory(config *config.DatabaseConfig) *Test {

// Init will:
// - initialize a template1 DB with migrations
// - rebuild AMS DB from template1
// - rebuild Maestro DB from template1
// - return a new connection factory
// Go includes database connection pooling in the platform. Gorm uses the same and provides a method to
// clone a connection via New(), which is safe for use by concurrent Goroutines.
Expand Down Expand Up @@ -81,15 +83,15 @@ func resetDB(config *config.DatabaseConfig) error {
dbx, _, cleanup := connect("postgres", config)
defer cleanup()

// Drop `all` connections to both `template1` and AMS DB, so it can be dropped and created
// Drop `all` connections to both `template1` and Maestro DB, so it can be dropped and created
if err := dropConnections(dbx, "template1"); err != nil {
return err
}
if err := dropConnections(dbx, config.Name); err != nil {
return err
}

// Rebuild AMS DB
// Rebuild Maestro DB
query := fmt.Sprintf("DROP DATABASE IF EXISTS %s", config.Name)
if _, err := dbx.Exec(query); err != nil {
return fmt.Errorf("SQL failed to DROP database %s: %s", config.Name, err.Error())
Expand All @@ -98,7 +100,7 @@ func resetDB(config *config.DatabaseConfig) error {
if _, err := dbx.Exec(query); err != nil {
return fmt.Errorf("SQL failed to CREATE database %s: %s", config.Name, err.Error())
}
// As `template1` had all migrations, so now AMS DB has them too!
// As `template1` had all migrations, so now Maestro DB has them too!
return nil
}

Expand All @@ -110,20 +112,17 @@ func connect(name string, config *config.DatabaseConfig) (*sql.DB, *gorm.DB, fun
err error
)

dbx, err = sql.Open(config.Dialect, config.ConnectionStringWithName(name, config.SSLMode != disable))
connConfig, err := pgx.ParseConfig(config.ConnectionStringWithName(name, 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.LogSafeConnectionStringWithName(name, 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
Loading