machinelock-manager/db/main.go
2024-12-27 23:47:52 +01:00

42 lines
753 B
Go

package db
import (
"log/slog"
"git.ctdo.de/ctdo/machinelock-manager/config"
"github.com/glebarez/sqlite"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var DB *gorm.DB
func init() {
var err error
switch config.DbType {
case "sqlite":
DB, err = gorm.Open(sqlite.Open(config.Db), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
case "psql":
DB, err = gorm.Open(postgres.Open(config.Db), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
}
if err != nil {
slog.Error("failed to connect database", "error", err.Error())
panic(0)
}
migrate()
}
func migrate() {
err := DB.AutoMigrate(
&Token{},
&Machine{},
)
if err != nil {
slog.Error(err.Error())
panic(0)
}
}