Skip to content

Commit

Permalink
testing root db
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 4, 2024
1 parent c178ee6 commit dc22aae
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 14 additions & 5 deletions backend/src/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down
32 changes: 21 additions & 11 deletions backend/tests/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"

"github.com/GenerateNU/sac/backend/src/auth"
Expand All @@ -23,7 +24,6 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/huandu/go-assert"
gormPostgres "gorm.io/driver/postgres"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit dc22aae

Please sign in to comment.