Skip to content

Commit

Permalink
Fix OpenConn WAL handling (#34)
Browse files Browse the repository at this point in the history
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 <ross@zombiezen.com>
  • Loading branch information
anacrolix and zombiezen committed Jan 10, 2022
1 parent ffbe825 commit c6d6f6a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 9 additions & 3 deletions sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit c6d6f6a

Please sign in to comment.