2024-02-22 10:18:59 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2024-02-22 20:52:37 +01:00
|
|
|
"log"
|
|
|
|
|
2024-02-22 10:18:59 +01:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
var conn *gorm.DB
|
|
|
|
|
|
|
|
// ConnectDB finds the correct database type and connects to it, then stores the pointer
|
|
|
|
// to the database handler in a variable
|
|
|
|
func ConnectDB() {
|
|
|
|
dbType := viper.GetString("db.type")
|
|
|
|
switch dbType {
|
|
|
|
case "postgres":
|
|
|
|
conn = connectPostgres()
|
|
|
|
default:
|
|
|
|
conn = connectSQLite()
|
|
|
|
}
|
|
|
|
|
|
|
|
err := conn.AutoMigrate(&User{})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = conn.AutoMigrate(&Group{})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|