Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_wal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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