Rename ensure_autocommit_off() to ensure_autocommit_on(), closes #705

The ensure_autocommit_off() context manager was misleadingly named: it
sets isolation_level = None on the underlying sqlite3 connection, which
puts the driver INTO autocommit mode (no implicit transactions) - the
opposite of what the name and docstring claimed.

The behavior was always correct for its call sites, which need
autocommit to run PRAGMA statements like journal_mode=wal. This renames
the method to ensure_autocommit_on() with an accurate docstring, updates
the three internal call sites, adds tests, and documents the rename in
the changelog and the 3.x to 4.0 upgrading guide. The old name is
removed outright since 4.0 permits breaking changes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Simon Willison 2026-07-05 21:50:20 -07:00
commit 6df6da1a8e
4 changed files with 27 additions and 7 deletions

View file

@ -610,16 +610,22 @@ class Database:
self.conn.execute("ROLLBACK")
@contextlib.contextmanager
def ensure_autocommit_off(self) -> Generator[None, None, None]:
def ensure_autocommit_on(self) -> Generator[None, None, None]:
"""
Ensure autocommit is off for this database connection.
Ensure the connection is in driver-level autocommit mode for the
duration of a block of code.
This temporarily sets ``isolation_level = None`` on the underlying
``sqlite3`` connection, so the driver does not open implicit
transactions. This is useful for statements such as
``PRAGMA journal_mode=wal`` which cannot run inside a transaction.
Example usage::
with db.ensure_autocommit_off():
with db.ensure_autocommit_on():
# do stuff here
This will reset to the previous autocommit state at the end of the block.
The previous ``isolation_level`` is restored at the end of the block.
"""
old_isolation_level = self.conn.isolation_level
try:
@ -783,7 +789,7 @@ class Database:
if self.conn.in_transaction:
cursor = self.conn.execute(sql, *args)
else:
with self.ensure_autocommit_off():
with self.ensure_autocommit_on():
cursor = self.conn.execute(sql, *args)
if cursor.description is None:
raise ValueError(message)
@ -1085,7 +1091,7 @@ class Database:
"""
if self.journal_mode != "wal":
self._ensure_no_open_transaction("enable_wal()")
with self.ensure_autocommit_off():
with self.ensure_autocommit_on():
self.execute("PRAGMA journal_mode=wal;")
def disable_wal(self) -> None:
@ -1097,7 +1103,7 @@ class Database:
"""
if self.journal_mode != "delete":
self._ensure_no_open_transaction("disable_wal()")
with self.ensure_autocommit_off():
with self.ensure_autocommit_on():
self.execute("PRAGMA journal_mode=delete;")
def _ensure_no_open_transaction(self, operation: str) -> None: