Skip to content

Commit

Permalink
refactor(orm): use test db for APP_ENV=test
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Oct 4, 2020
1 parent a5171a5 commit 1d38256
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions orm/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package orm
import (
"log"
"os"
"strings"
"sync"

"github.com/adhocore/urlsh/model"
Expand All @@ -19,17 +20,19 @@ func pgConnect() *gorm.DB {
log.Fatal("Database configuration DSN missing, Pass in APP_DB_DSN env")
}

logLevel := logger.Warn
if os.Getenv("APP_ENV") == "prod" {
logLevel, env := logger.Warn, os.Getenv("APP_ENV")
if env == "prod" {
logLevel = logger.Silent
} else if env == "test" {
dsn = strings.Replace(dsn, "dbname=", "dbname=test_", 1)
}

db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logLevel),
})

if err != nil {
log.Fatal("Cannot connect to database with given DSN")
log.Fatalf("database error: %v", err)
}

_ = db.AutoMigrate(&model.Keyword{}, &model.Url{})
Expand Down

0 comments on commit 1d38256

Please sign in to comment.