Skip to content

Commit c6d6f6a

Browse files
anacrolixzombiezen
andauthored
Fix OpenConn WAL handling (zombiezen#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 <[email protected]>
1 parent ffbe825 commit c6d6f6a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5151
but report success.
5252
- `sqlitemigration` will no longer skip applying the repeatable migration
5353
if the final migration is empty.
54+
- `OpenConn` now sets a busy handler before enabling WAL
55+
(thanks @anacrolix!).
5456

5557
## [0.8.0][] - 2021-11-07
5658

sqlite.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,21 @@ func OpenConn(path string, flags ...OpenFlags) (*Conn, error) {
113113
}
114114

115115
if openFlags&OpenWAL != 0 {
116+
// Set timeout for enabling WAL.
117+
// See https://github.com/crawshaw/sqlite/pull/113 for details.
118+
// TODO(maybe): Pass in Context to OpenConn?
119+
c.SetBusyTimeout(10 * time.Second)
120+
116121
stmt, _, err := c.PrepareTransient("PRAGMA journal_mode=wal;")
117122
if err != nil {
118123
c.Close()
119124
return nil, fmt.Errorf("sqlite: open %q: %w", path, err)
120125
}
121-
defer stmt.Finalize()
122-
if _, err := stmt.Step(); err != nil {
126+
_, err = stmt.Step()
127+
stmt.Finalize()
128+
if err != nil {
123129
c.Close()
124-
return nil, fmt.Errorf("sqlite: open %q: %w", path, err)
130+
return nil, fmt.Errorf("sqlite: open %q: enable wal: %w", path, err)
125131
}
126132
}
127133

0 commit comments

Comments
 (0)