31 lines
524 B
Go
31 lines
524 B
Go
|
package db
|
||
|
|
||
|
import (
|
||
|
"github.com/spf13/viper"
|
||
|
"gorm.io/gorm"
|
||
|
"log"
|
||
|
)
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
}
|