Disable app-side auto-checkpointing so Litestream is the sole checkpoint owner, and configure Litestream to use only non-blocking checkpoints. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
34 lines
798 B
Go
34 lines
798 B
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
func Open(dbPath string) (*sql.DB, error) {
|
|
dir := filepath.Dir(dbPath)
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
return nil, fmt.Errorf("creating db directory: %w", err)
|
|
}
|
|
|
|
dsn := fmt.Sprintf("file:%s?_pragma=journal_mode(WAL)&_pragma=synchronous(NORMAL)&_pragma=foreign_keys(ON)&_pragma=busy_timeout(30000)&_pragma=mmap_size(268435456)&_pragma=cache_size(-65536)&_pragma=wal_autocheckpoint(0)", dbPath)
|
|
|
|
db, err := sql.Open("sqlite", dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("opening database: %w", err)
|
|
}
|
|
|
|
db.SetMaxOpenConns(1)
|
|
db.SetMaxIdleConns(1)
|
|
|
|
if err := db.Ping(); err != nil {
|
|
_ = db.Close()
|
|
return nil, fmt.Errorf("pinging database: %w", err)
|
|
}
|
|
|
|
return db, nil
|
|
}
|