diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index aacdc893..6a0c0234 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 23ca144e..44d86398 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