fix: sqlite concurrency defaults

This commit is contained in:
2026-05-26 22:21:09 +02:00
parent 30441c3e1f
commit c6090604ef
2 changed files with 16 additions and 3 deletions

View File

@@ -10,9 +10,14 @@ import (
// Open connects to a sqlite3 database with foreign keys enforced
func Open(dbFile string) (*sql.DB, error) {
db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?_foreign_keys=on", dbFile))
// busy_timeout avoids immediate SQLITE_BUSY errors under concurrent access.
// foreign_keys ensures FK constraints are enforced for this connection.
db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?_foreign_keys=on&_busy_timeout=5000", dbFile))
if err != nil {
return nil, fmt.Errorf("failed to open db: %w", err)
}
// WAL improves concurrency between readers and writers.
_, _ = db.Exec("PRAGMA journal_mode=WAL;")
_, _ = db.Exec("PRAGMA busy_timeout=5000;")
return db, nil
}