Skip to content

Commit

Permalink
Do not attempt optimize/wal on readonly main
Browse files Browse the repository at this point in the history
Fixes #544
  • Loading branch information
rogerbinns committed Nov 20, 2024
1 parent 418bd0e commit adebc36
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions apsw/bestpractice.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ def connection_wal(connection: apsw.Connection) -> None:
`described here <https://www.sqlite.org/wal.html>`__.
"""
try:
connection.pragma("journal_mode", "wal")
if not connection.readonly("main"):
connection.pragma("journal_mode", "wal")
except apsw.ReadOnlyError:
# journal/wal etc could still be readonly
pass


Expand Down Expand Up @@ -75,8 +77,10 @@ def connection_optimize(connection: apsw.Connection) -> None:
<https://sqlite.org/lang_analyze.html>`__.
"""
try:
connection.pragma("optimize", 0x10002)
if not connection.readonly("main"):
connection.pragma("optimize", 0x10002)
except apsw.ReadOnlyError:
# journal/wal etc could still be readonly
pass


Expand Down
3 changes: 2 additions & 1 deletion apsw/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10178,7 +10178,8 @@ def testBestPractice(self) -> None:
self.assertRaises(apsw.SQLError, con.execute, dqs)

# can't optimize or WAL readonly databases
apsw.Connection(self.db.filename, flags=apsw.SQLITE_OPEN_READONLY)
with self.assertNoLogs():
apsw.Connection(self.db.filename, flags=apsw.SQLITE_OPEN_READONLY)

def testExtTracing(self) -> None:
"apsw.ext Tracing and Resource usage"
Expand Down

0 comments on commit adebc36

Please sign in to comment.