feat: improve sqlite database reliability
All checks were successful
ci/woodpecker/push/ci Pipeline was successful
ci/woodpecker/tag/release Pipeline was successful

This commit is contained in:
Felipe M 2025-06-13 09:27:06 +02:00
parent 8fa74fd046
commit 1e0bc86b21
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8

View file

@ -35,6 +35,11 @@ func New(dbPath string) (*Database, error) {
return nil, err
}
// Configure SQLite for better reliability
if err := configureSQLite(db); err != nil {
return nil, err
}
// Initialize database
if err := initDatabase(db); err != nil {
return nil, err
@ -794,6 +799,32 @@ func initDatabase(db *sql.DB) error {
return nil
}
// Configure SQLite for better reliability
func configureSQLite(db *sql.DB) error {
pragmas := []string{
// Enable Write-Ahead Logging for better concurrency and crash recovery
"PRAGMA journal_mode = WAL",
// Set 5-second timeout when database is locked by another connection
"PRAGMA busy_timeout = 5000",
// Balance between safety and performance for disk writes
"PRAGMA synchronous = NORMAL",
// Set large cache size (1GB) for better read performance
"PRAGMA cache_size = 1000000000",
// Enable foreign key constraint enforcement
"PRAGMA foreign_keys = true",
// Store temporary tables and indices in memory for speed
"PRAGMA temp_store = memory",
}
for _, pragma := range pragmas {
if _, err := db.Exec(pragma); err != nil {
return fmt.Errorf("failed to execute %s: %w", pragma, err)
}
}
return nil
}
// CacheGet retrieves a value from the cache
func (d *Database) CacheGet(key string) (string, error) {
query := `