tlm-login-server/internal/app/db.go

36 lines
654 B
Go
Raw Normal View History

2023-12-06 23:25:34 +01:00
package app
import (
"code.lila.network/lauralani/tlm-login-server/internal/config"
2023-12-06 23:25:34 +01:00
"code.lila.network/lauralani/tlm-login-server/internal/models"
"github.com/glebarez/sqlite"
//"gorm.io/driver/sqlite" with CGo
"gorm.io/gorm"
"log/slog"
"os"
)
func InitializeDB() error {
var err error
var db *gorm.DB
db, err = gorm.Open(sqlite.Open(config.SQLitePath), &gorm.Config{})
2023-12-06 23:25:34 +01:00
if err != nil {
slog.Error("Can't open DB", "path", config.SQLitePath)
2023-12-06 23:25:34 +01:00
os.Exit(1)
}
models.DB = db
err = models.DB.AutoMigrate(&models.User{}, &models.EmailConfirmation{})
2023-12-06 23:25:34 +01:00
if err != nil {
slog.Error("Can't do DB Migration")
2023-12-06 23:25:34 +01:00
return err
}
return nil
}