Skip to content

Commit

Permalink
fix(backend): set default db name
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Urban committed Oct 11, 2023
1 parent 8527ed7 commit 7b327c7
Showing 1 changed file with 12 additions and 41 deletions.
53 changes: 12 additions & 41 deletions backend/internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,13 @@ func InitializeDatabase() (*gorm.DB, error) {
var dsn string
var db *gorm.DB

switch dbType {
case "mysql":
dbHost, err := util.GetEnvOrError("DB_HOST")
if err != nil {
return nil, err
}

dbPort, err := util.GetEnvOrError("DB_PORT")
if err != nil {
return nil, err
}

dbUser, err := util.GetEnvOrError("DB_USER")
if err != nil {
return nil, err
}

dbPassword, err := util.GetEnvOrError("DB_PASSWORD")
if err != nil {
return nil, err
}

dbName, err := util.GetEnvOrDefault("DB_NAME", "kubevoyage")
if err != nil {
return nil, err
}

dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPassword, dbHost, dbPort, dbName)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
dbName, err := util.GetEnvOrDefault("DB_NAME", "kubevoyage")
if err != nil {
return nil, err
}

case "postgres":
switch dbType {
case "mysql", "postgres":
dbHost, err := util.GetEnvOrError("DB_HOST")
if err != nil {
return nil, err
Expand All @@ -74,20 +50,15 @@ func InitializeDatabase() (*gorm.DB, error) {
return nil, err
}

dbName, err := util.GetEnvOrDefault("DB_NAME", "kubevoyage")
if err != nil {
return nil, err
if dbType == "mysql" {
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPassword, dbHost, dbPort, dbName)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
} else {
dsn = fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=disable", dbHost, dbPort, dbUser, dbName, dbPassword)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
}

dsn = fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=disable", dbHost, dbPort, dbUser, dbName, dbPassword)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})

case "sqlite":
dbName, err := util.GetEnvOrDefault("DB_NAME", "kubevoyage")
if err != nil {
return nil, err
}

dsn = dbName // For SQLite, dbName would be the path to the .db file
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{})

Expand Down

0 comments on commit 7b327c7

Please sign in to comment.