32 lines
525 B
Go
32 lines
525 B
Go
package db
|
|
|
|
import (
|
|
"log"
|
|
|
|
"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)
|
|
}
|
|
}
|