From dc22aae9e8db9660199e71f1745dc68cd2abc0ef Mon Sep 17 00:00:00 2001 From: garrettladley <ladley.g@northeastern.edu> Date: Sat, 3 Feb 2024 20:17:42 -0500 Subject: [PATCH] testing root db --- .github/workflows/backend.yml | 2 +- backend/src/database/db.go | 19 ++++++++++++++----- backend/tests/api/helpers.go | 32 +++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index b370f8a1e..e7b560af7 100644 --- a/.github/workflows/backend.yml +++ b/.github/workflows/backend.yml @@ -99,6 +99,6 @@ jobs: - name: Migrate DB run: cd ./backend/src && go run main.go --only-migrate - name: Run Tests with Coverage - run: cd ./backend/ && go test -benchmem -race -coverprofile=coverage.txt ./... + run: cd ./backend/ && go test -bench=. -benchmem -race -coverprofile=coverage.txt ./... - name: Print Coverage run: cd ./backend/ && go tool cover -func=coverage.txt diff --git a/backend/src/database/db.go b/backend/src/database/db.go index 003a27988..f0d7d5717 100644 --- a/backend/src/database/db.go +++ b/backend/src/database/db.go @@ -10,7 +10,20 @@ import ( ) func ConfigureDB(settings config.Settings) (*gorm.DB, error) { - db, err := gorm.Open(postgres.Open(settings.Database.WithDb()), &gorm.Config{ + db, err := EstablishConn(settings.Database.WithDb()) + if err != nil { + return nil, err + } + + if err := MigrateDB(settings, db); err != nil { + return nil, err + } + + return db, nil +} + +func EstablishConn(dsn string) (*gorm.DB, error) { + db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Info), SkipDefaultTransaction: true, TranslateError: true, @@ -24,10 +37,6 @@ func ConfigureDB(settings config.Settings) (*gorm.DB, error) { return nil, err } - if err := MigrateDB(settings, db); err != nil { - return nil, err - } - return db, nil } diff --git a/backend/tests/api/helpers.go b/backend/tests/api/helpers.go index 5681d41cd..10cf2177c 100644 --- a/backend/tests/api/helpers.go +++ b/backend/tests/api/helpers.go @@ -10,6 +10,7 @@ import ( "net/http/httptest" "path/filepath" "strings" + "sync" "testing" "github.com/GenerateNU/sac/backend/src/auth" @@ -23,7 +24,6 @@ import ( "github.com/gofiber/fiber/v2" "github.com/huandu/go-assert" - gormPostgres "gorm.io/driver/postgres" "gorm.io/gorm" ) @@ -219,20 +219,30 @@ func generateRandomDBName() string { return fmt.Sprintf("%s%s", prefix, string(result)) } -func configureDatabase(config config.Settings) (*gorm.DB, error) { - dsnWithoutDB := config.Database.WithoutDb() - dbWithoutDB, err := gorm.Open(gormPostgres.Open(dsnWithoutDB), &gorm.Config{SkipDefaultTransaction: true, TranslateError: true}) - if err != nil { - return nil, err - } +var ( + rootConn *gorm.DB + once sync.Once +) + +func RootConn(dbConfig config.DatabaseSettings) { + once.Do(func() { + var err error + rootConn, err = database.EstablishConn(dbConfig.WithDb()) + if err != nil { + panic(err) + } + }) +} + +func configureDatabase(settings config.Settings) (*gorm.DB, error) { + RootConn(settings.Database) - err = dbWithoutDB.Exec(fmt.Sprintf("CREATE DATABASE %s;", config.Database.DatabaseName)).Error + err := rootConn.Exec(fmt.Sprintf("CREATE DATABASE %s", settings.Database.DatabaseName)).Error if err != nil { return nil, err } - dsnWithDB := config.Database.WithDb() - dbWithDB, err := gorm.Open(gormPostgres.Open(dsnWithDB), &gorm.Config{SkipDefaultTransaction: true, TranslateError: true}) + dbWithDB, err := database.EstablishConn(settings.Database.WithDb()) if err != nil { return nil, err } @@ -241,7 +251,7 @@ func configureDatabase(config config.Settings) (*gorm.DB, error) { if err != nil { return nil, err } - err = database.MigrateDB(config, dbWithDB) + err = database.MigrateDB(settings, dbWithDB) if err != nil { return nil, err }