fix(db): enable WAL mode, busy_timeout and single conn to avoid SQLITE_BUSY
This commit is contained in:
@@ -25,10 +25,19 @@ func Open(path string) (*DB, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open sqlite: %w", err)
|
||||
}
|
||||
if _, err := conn.Exec(`PRAGMA journal_mode = WAL`); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, fmt.Errorf("set journal_mode: %w", err)
|
||||
}
|
||||
if _, err := conn.Exec(`PRAGMA busy_timeout = 5000`); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, fmt.Errorf("set busy_timeout: %w", err)
|
||||
}
|
||||
if _, err := conn.Exec(`PRAGMA foreign_keys = ON`); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, fmt.Errorf("enable foreign keys: %w", err)
|
||||
}
|
||||
conn.SetMaxOpenConns(1)
|
||||
return &DB{conn: conn}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user