2023-12-06 23:25:34 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2023-12-15 15:42:29 +01:00
|
|
|
"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
|
|
|
|
|
2023-12-15 15:42:29 +01:00
|
|
|
db, err = gorm.Open(sqlite.Open(config.SQLitePath), &gorm.Config{})
|
2023-12-06 23:25:34 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2023-12-15 15:42:29 +01:00
|
|
|
slog.Error("Can't open DB", "path", config.SQLitePath)
|
2023-12-06 23:25:34 +01:00
|
|
|
os.Exit(1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
models.DB = db
|
|
|
|
|
2023-12-18 07:48:25 +01:00
|
|
|
err = models.DB.AutoMigrate(&models.User{}, &models.EmailConfirmation{})
|
2023-12-06 23:25:34 +01:00
|
|
|
if err != nil {
|
2023-12-18 07:48:25 +01:00
|
|
|
slog.Error("Can't do DB Migration")
|
2023-12-06 23:25:34 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|