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

Added a DEV environment variable to use local .db file #98

Merged
merged 2 commits into from
Jun 9, 2023
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
61 changes: 25 additions & 36 deletions utils/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,13 @@ import (

// _ "github.com/jinzhu/gorm/dialects/mysql" // MySQL Dialect
_ "github.com/jinzhu/gorm/dialects/postgres"
// _ "github.com/jinzhu/gorm/dialects/sqlite" // sqlite for dev
_ "github.com/jinzhu/gorm/dialects/sqlite" // sqlite for dev
)

// InitialMigration Initialize migration
func InitialMigration() {
DatabaseUsername := os.Getenv("DATABASE_USERNAME")
DatabasePassword := os.Getenv("DATABASE_PASSWORD")
DatabaseName := os.Getenv("DATABASE_NAME")
DatabaseHost := os.Getenv("DATABASE_HOST")
DatabasePort := os.Getenv("DATABASE_PORT")

newURI := "host=" + DatabaseHost + " port=" + DatabasePort + " user=" + DatabaseUsername + " dbname=" + DatabaseName + " sslmode=disable password=" + DatabasePassword
db, err := gorm.Open("postgres", newURI)
if err != nil {
log.Err(err).Msg("Database Error")
panic(err)
}

// // temporary SQLite for ease of development
// db, err := gorm.Open("sqlite3", "kwoc.db")
// if err != nil {
// log.Err(err).Msg("Database Error")
// panic("failed to connect database")
// }
// defer db.Close()
db := GetDB()
defer db.Close()

db.AutoMigrate(&models.Mentor{})
db.AutoMigrate(&models.Student{})
Expand All @@ -44,26 +26,33 @@ func InitialMigration() {

// GetDB Get Database
func GetDB() *gorm.DB {
DatabaseUsername := os.Getenv("DATABASE_USERNAME")
DatabasePassword := os.Getenv("DATABASE_PASSWORD")
DatabaseName := os.Getenv("DATABASE_NAME")
DatabaseHost := os.Getenv("DATABASE_HOST")
DatabasePort := os.Getenv("DATABASE_PORT")
isDev := os.Getenv("DEV") == "true"

var dbDialect string
var dbURI string

if !isDev {
DatabaseUsername := os.Getenv("DATABASE_USERNAME")
DatabasePassword := os.Getenv("DATABASE_PASSWORD")
DatabaseName := os.Getenv("DATABASE_NAME")
DatabaseHost := os.Getenv("DATABASE_HOST")
DatabasePort := os.Getenv("DATABASE_PORT")

dbDialect = "postgres"
dbURI = "host=" + DatabaseHost + " port=" + DatabasePort + " user=" + DatabaseUsername + " dbname=" + DatabaseName + " sslmode=disable password=" + DatabasePassword

} else {
rajivharlalka marked this conversation as resolved.
Show resolved Hide resolved
// SQLite database for local development
dbDialect = "sqlite3"
dbURI = "devDB.db"
}

db, err := gorm.Open(dbDialect, dbURI)

newURI := "host=" + DatabaseHost + " port=" + DatabasePort + " user=" + DatabaseUsername + " dbname=" + DatabaseName + " sslmode=disable password=" + DatabasePassword
db, err := gorm.Open("postgres", newURI)
if err != nil {
log.Err(err).Msg("Database Error")
panic(err)
}
// TODO : DB close issue

// // temporary SQLite for ease of development
// db, err := gorm.Open("sqlite3", "kwoc.db")
// if err != nil {
// log.Err(err).Msg("Database Error")
// panic(err)
// }

return db
}