Skip to content

Commit

Permalink
Enhance database initialization in db.go (#2645)
Browse files Browse the repository at this point in the history
- 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>
izzzzzi and Zakhar Izmaylov authored Jan 21, 2025
1 parent 7b7eb98 commit 66fe841
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions database/db.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 66fe841

Please sign in to comment.