Gorm silent, except in debug mode

This commit is contained in:
Pablu23
2024-05-30 23:58:18 +02:00
parent 766da5aeb2
commit e5e7c4eb54
5 changed files with 36 additions and 11 deletions

View File

@@ -2,30 +2,46 @@ package database
import (
_ "embed"
_ "github.com/mattn/go-sqlite3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type Manager struct {
ConnectionString string
Db *gorm.DB
CreateIfNotExists bool
ConnectionString string
Db *gorm.DB
CreateIfNotExists bool
ActivateGormLogger bool
}
func NewDatabase(connectionString string, createIfNotExists bool) Manager {
func NewDatabase(connectionString string, createIfNotExists bool, activateGormLogger bool) Manager {
return Manager{
ConnectionString: connectionString,
Db: nil,
CreateIfNotExists: createIfNotExists,
ConnectionString: connectionString,
Db: nil,
CreateIfNotExists: createIfNotExists,
ActivateGormLogger: activateGormLogger,
}
}
func (dbMgr *Manager) Open() error {
db, err := gorm.Open(sqlite.Open(dbMgr.ConnectionString), &gorm.Config{})
if err != nil {
return err
var db *gorm.DB
var err error
if dbMgr.ActivateGormLogger {
db, err = gorm.Open(sqlite.Open(dbMgr.ConnectionString), &gorm.Config{})
if err != nil {
return err
}
} else {
db, err = gorm.Open(sqlite.Open(dbMgr.ConnectionString), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
return err
}
}
dbMgr.Db = db
if dbMgr.CreateIfNotExists {
err = dbMgr.createDatabaseIfNotExists()