Enhance database initialization in db.go (#2645)

- Updated GORM configuration to skip default transactions and prepare statements.
- Modified the database connection string to include caching and journal mode settings.
- Executed several PRAGMA statements to optimize SQLite performance and enable foreign key support.

These changes improve database handling and performance in the application.

Co-authored-by: Zakhar Izmaylov <ptdev@kedruss.ru>
This commit is contained in:
Zahar Izmailov 2025-01-21 04:59:30 +03:00 committed by GitHub
parent 7b7eb98acb
commit 66fe84181b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -82,9 +82,31 @@ func InitDB(dbPath string) error {
}
c := &gorm.Config{
Logger: gormLogger,
Logger: gormLogger,
SkipDefaultTransaction: true,
PrepareStmt: true,
}
db, err = gorm.Open(sqlite.Open(dbPath), c)
dsn := dbPath + "?cache=shared&_journal_mode=WAL&_synchronous=NORMAL"
db, err = gorm.Open(sqlite.Open(dsn), c)
if err != nil {
return err
}
sqlDB, err := db.DB()
if err != nil {
return err
}
_, err = sqlDB.Exec("PRAGMA cache_size = -64000;")
if err != nil {
return err
}
_, err = sqlDB.Exec("PRAGMA temp_store = MEMORY;")
if err != nil {
return err
}
_, err = sqlDB.Exec("PRAGMA foreign_keys = ON;")
if err != nil {
return err
}