fix(db): enable WAL also for Wasm driver

related #620
This commit is contained in:
sentriz
2025-12-05 11:49:04 +01:00
parent c8cc0ddb4c
commit b67c1c14d2
2 changed files with 12 additions and 20 deletions

No files matched your search

+1 -1
View File
@@ -138,7 +138,7 @@ func main() {
log.Fatalf("couldn't create covers cache path: %v\n", err) log.Fatalf("couldn't create covers cache path: %v\n", err)
} }
dbc, err := db.New(*confDBPath, db.DefaultOptions(), *confLogDB) dbc, err := db.New(*confDBPath, *confLogDB)
if err != nil { if err != nil {
log.Fatalf("error opening database: %v\n", err) log.Fatalf("error opening database: %v\n", err)
} }
+11 -19
View File
@@ -17,46 +17,38 @@ import (
"go.senan.xyz/gonic/server/ctrlsubsonic/specid" "go.senan.xyz/gonic/server/ctrlsubsonic/specid"
) )
func DefaultOptions() url.Values {
return url.Values{
// with this, the db sleeps for a little while when locked. can prevent
// a SQLITE_BUSY. see https://www.sqlite.org/c3ref/busy_timeout.html
"_busy_timeout": {"30000"},
"_journal_mode": {"WAL"},
"_foreign_keys": {"true"},
}
}
func mockOptions() url.Values {
return url.Values{
"_foreign_keys": {"true"},
}
}
type DB struct { type DB struct {
*gorm.DB *gorm.DB
} }
func New(path string, options url.Values, logQueries bool) (*DB, error) { func New(path string, logQueries bool) (*DB, error) {
url := url.URL{ url := url.URL{
Scheme: "file", Scheme: "file",
Opaque: path, Opaque: path,
} }
url.RawQuery = options.Encode()
db, err := gorm.Open("sqlite3", url.String()) db, err := gorm.Open("sqlite3", url.String())
if err != nil { if err != nil {
return nil, fmt.Errorf("with gorm: %w", err) return nil, fmt.Errorf("with gorm: %w", err)
} }
db.SetLogger(log.New(os.Stdout, "gorm ", 0)) db.SetLogger(log.New(os.Stdout, "gorm ", 0))
if logQueries { if logQueries {
db.LogMode(true) db.LogMode(true)
} }
db.DB().SetMaxOpenConns(4) db.DB().SetMaxOpenConns(4)
// https://www.sqlite.org/pragma.html
db.Exec("PRAGMA busy_timeout = 30000")
db.Exec("PRAGMA journal_mode = WAL")
db.Exec("PRAGMA foreign_keys = ON")
return &DB{DB: db}, nil return &DB{DB: db}, nil
} }
func NewMock() (*DB, error) { func NewMock() (*DB, error) {
d, err := New(":memory:", mockOptions(), false) d, err := New(":memory:", false)
if err != nil { if err != nil {
return nil, err return nil, err
} }