Fix db.go

This commit is contained in:
Dikiy13371 2025-10-07 23:44:05 +03:00
parent f0eca194e2
commit 97e9aca156

View file

@ -1,6 +1,7 @@
package database package database
import ( import (
"errors"
"io/fs" "io/fs"
"os" "os"
"path" "path"
@ -71,3 +72,27 @@ func SeedAdmin() error {
} }
return db.Create(&admin).Error return db.Create(&admin).Error
} }
// IsNotFound reports whether err is gorm's record-not-found.
func IsNotFound(err error) bool {
return errors.Is(err, gorm.ErrRecordNotFound)
}
// IsSQLiteDB reports whether current DB dialector is sqlite.
func IsSQLiteDB() bool {
if db == nil {
return false
}
return db.Dialector.Name() == "sqlite"
}
// Checkpoint runs WAL checkpoint for SQLite to compact the WAL file.
// No-op for non-SQLite databases.
func Checkpoint() error {
if !IsSQLiteDB() {
return nil
}
// FULL/TRUNCATE — в зависимости от нужной семантики.
// TRUNCATE чаще используется, чтобы обрезать WAL-файл.
return db.Exec("PRAGMA wal_checkpoint(TRUNCATE);").Error
}