From 82f31eeade679feba42190b03ad51e85550114de Mon Sep 17 00:00:00 2001 From: Rami Abdelrazzaq Date: Sat, 7 Mar 2026 12:33:12 -0600 Subject: [PATCH] Clarify ensure_autocommit_off() behavior Fixes simonw/sqlite-utils#705 --- sqlite_utils/db.py | 8 ++++++-- tests/test_wal.py | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index aacdc8933..6a0c0234d 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -403,14 +403,18 @@ def close(self) -> None: @contextlib.contextmanager def ensure_autocommit_off(self) -> Generator[None, None, None]: """ - Ensure autocommit is off for this database connection. + Ensure autocommit is on for this database connection. + + Despite the historical method name, this temporarily sets + ``self.conn.isolation_level = None`` so statements execute in autocommit + mode inside the block. Example usage:: with db.ensure_autocommit_off(): # do stuff here - This will reset to the previous autocommit state at the end of the block. + This will reset to the previous isolation level at the end of the block. """ old_isolation_level = self.conn.isolation_level try: diff --git a/tests/test_wal.py b/tests/test_wal.py index 23ca144ed..44d86398d 100644 --- a/tests/test_wal.py +++ b/tests/test_wal.py @@ -21,3 +21,11 @@ def test_enable_disable_wal(db_path_tmpdir): db.disable_wal() assert "delete" == db.journal_mode assert "test.db-wal" not in [f.basename for f in tmpdir.listdir()] + + +def test_ensure_autocommit_off_sets_autocommit_mode_temporarily(db_path_tmpdir): + db, path, tmpdir = db_path_tmpdir + original_isolation_level = db.conn.isolation_level + with db.ensure_autocommit_off(): + assert db.conn.isolation_level is None + assert db.conn.isolation_level == original_isolation_level