Skip to content

Commit 27eb19e

Browse files
author
Test User
committed
Fix #705: Clarify ensure_autocommit_off() naming and documentation
The function name ensure_autocommit_off() was confusing because it sets isolation_level = None, which actually ENABLES autocommit mode in SQLite, not disables it. Changes: - Add new ensure_autocommit_on() method with correct name and docstring - Keep ensure_autocommit_off() as deprecated alias for backwards compatibility - Update internal usages (enable_wal(), disable_wal()) to use new method - Add warnings import for deprecation notice
1 parent fce0eb4 commit 27eb19e

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

sqlite_utils/db.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
Tuple,
4141
)
4242
import uuid
43+
import warnings
4344
from sqlite_utils.plugins import pm
4445

4546
try:
@@ -401,13 +402,16 @@ def close(self) -> None:
401402
self.conn.close()
402403

403404
@contextlib.contextmanager
404-
def ensure_autocommit_off(self) -> Generator[None, None, None]:
405+
def ensure_autocommit_on(self) -> Generator[None, None, None]:
405406
"""
406-
Ensure autocommit is off for this database connection.
407+
Ensure autocommit is on for this database connection.
408+
409+
In SQLite's Python module, ``isolation_level = None`` enables autocommit mode,
410+
where each statement is committed immediately.
407411
408412
Example usage::
409413
410-
with db.ensure_autocommit_off():
414+
with db.ensure_autocommit_on():
411415
# do stuff here
412416
413417
This will reset to the previous autocommit state at the end of the block.
@@ -419,6 +423,21 @@ def ensure_autocommit_off(self) -> Generator[None, None, None]:
419423
finally:
420424
self.conn.isolation_level = old_isolation_level
421425

426+
@contextlib.contextmanager
427+
def ensure_autocommit_off(self) -> Generator[None, None, None]:
428+
"""
429+
Deprecated alias for :meth:`ensure_autocommit_on`.
430+
431+
This method name is confusing - ``isolation_level = None`` actually enables
432+
autocommit mode in SQLite, not disables it. Use :meth:`ensure_autocommit_on` instead.
433+
"""
434+
warnings.warn(
435+
"ensure_autocommit_off() is deprecated, use ensure_autocommit_on() instead",
436+
DeprecationWarning,
437+
)
438+
with self.ensure_autocommit_on():
439+
yield
440+
422441
@contextlib.contextmanager
423442
def tracer(
424443
self, tracer: Optional[Callable[[str, Optional[Sequence]], None]] = None
@@ -781,13 +800,13 @@ def enable_wal(self) -> None:
781800
Sets ``journal_mode`` to ``'wal'`` to enable Write-Ahead Log mode.
782801
"""
783802
if self.journal_mode != "wal":
784-
with self.ensure_autocommit_off():
803+
with self.ensure_autocommit_on():
785804
self.execute("PRAGMA journal_mode=wal;")
786805

787806
def disable_wal(self) -> None:
788807
"Sets ``journal_mode`` back to ``'delete'`` to disable Write-Ahead Log mode."
789808
if self.journal_mode != "delete":
790-
with self.ensure_autocommit_off():
809+
with self.ensure_autocommit_on():
791810
self.execute("PRAGMA journal_mode=delete;")
792811

793812
def _ensure_counts_table(self) -> None:

0 commit comments

Comments
 (0)