From c6d6f6a5901fad9977f884cb757b10100e34a8bf Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 10 Jan 2022 16:22:55 +1100 Subject: [PATCH] Fix OpenConn WAL handling (#34) Connection needs to be set to block on busy before WAL journal mode is set in case it hasn't already set by another connection. Journal-mode statement needs to be finalized before closing connection on error. Co-authored-by: Ross Light --- CHANGELOG.md | 2 ++ sqlite.go | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 539ad89..95c0c0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 but report success. - `sqlitemigration` will no longer skip applying the repeatable migration if the final migration is empty. +- `OpenConn` now sets a busy handler before enabling WAL + (thanks @anacrolix!). ## [0.8.0][] - 2021-11-07 diff --git a/sqlite.go b/sqlite.go index e43fa89..1d8bd48 100644 --- a/sqlite.go +++ b/sqlite.go @@ -113,15 +113,21 @@ func OpenConn(path string, flags ...OpenFlags) (*Conn, error) { } if openFlags&OpenWAL != 0 { + // Set timeout for enabling WAL. + // See https://github.com/crawshaw/sqlite/pull/113 for details. + // TODO(maybe): Pass in Context to OpenConn? + c.SetBusyTimeout(10 * time.Second) + stmt, _, err := c.PrepareTransient("PRAGMA journal_mode=wal;") if err != nil { c.Close() return nil, fmt.Errorf("sqlite: open %q: %w", path, err) } - defer stmt.Finalize() - if _, err := stmt.Step(); err != nil { + _, err = stmt.Step() + stmt.Finalize() + if err != nil { c.Close() - return nil, fmt.Errorf("sqlite: open %q: %w", path, err) + return nil, fmt.Errorf("sqlite: open %q: enable wal: %w", path, err) } }