mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-24 18:04:32 +02:00
Database() now accepts all three Python transaction handling modes and behaves identically in each. For autocommit=False connections - where the driver holds an implicit transaction open at all times - the library tracks transaction ownership itself: - A new _explicit_transaction flag distinguishes transactions opened with begin()/atomic() from the driver's implicit one, exposed as a new db.in_transaction property (conn.in_transaction is always True in this mode) - begin() claims the driver's implicit transaction instead of executing BEGIN, which the driver would reject; BEGIN/COMMIT/ ROLLBACK passed to db.execute() are routed through begin()/commit()/ rollback() - Writes outside a user transaction commit the implicit transaction immediately, preserving the library's auto-commit contract - Row-returning statements outside a transaction fetch eagerly and commit, so the implicit read transaction does not hold a shared lock that blocks writes from other connections - PRAGMA and VACUUM run in temporary driver autocommit mode (ensure_autocommit_on() now flips conn.autocommit), since PRAGMAs are silently ignored and VACUUM refused inside the implicit transaction A new pytest --sqlite-autocommit-false option runs the entire suite in this mode, wired into CI alongside --sqlite-autocommit. Tests that asserted on conn.in_transaction or wrote through db.conn without committing now use the mode-aware library API instead. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PFkrS2nuP8mo1jmT594VCd
64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
import sqlite_utils
|
|
|
|
|
|
def test_delete_rowid_table(fresh_db):
|
|
table = fresh_db["table"]
|
|
table.insert({"foo": 1}).last_pk
|
|
rowid = table.insert({"foo": 2}).last_pk
|
|
table.delete(rowid)
|
|
assert [{"foo": 1}] == list(table.rows)
|
|
|
|
|
|
def test_delete_pk_table(fresh_db):
|
|
table = fresh_db["table"]
|
|
table.insert({"id": 1}, pk="id")
|
|
table.insert({"id": 2}, pk="id")
|
|
table.delete(1)
|
|
assert [{"id": 2}] == list(table.rows)
|
|
|
|
|
|
def test_delete_where(fresh_db):
|
|
table = fresh_db["table"]
|
|
for i in range(1, 11):
|
|
table.insert({"id": i}, pk="id")
|
|
assert table.count == 10
|
|
table.delete_where("id > ?", [5])
|
|
assert table.count == 5
|
|
|
|
|
|
def test_delete_where_all(fresh_db):
|
|
table = fresh_db["table"]
|
|
for i in range(1, 11):
|
|
table.insert({"id": i}, pk="id")
|
|
assert table.count == 10
|
|
table.delete_where()
|
|
assert table.count == 0
|
|
|
|
|
|
def test_delete_where_commits(tmpdir):
|
|
path = str(tmpdir / "test.db")
|
|
db = sqlite_utils.Database(path)
|
|
db["table"].insert_all([{"id": i} for i in range(5)], pk="id")
|
|
db["table"].delete_where("id > ?", [2])
|
|
# The connection must not be left inside an open transaction,
|
|
# otherwise subsequent atomic() blocks never commit either
|
|
assert not db.in_transaction
|
|
db["table"].insert({"id": 100})
|
|
db.close()
|
|
db2 = sqlite_utils.Database(path)
|
|
assert [r["id"] for r in db2["table"].rows] == [0, 1, 2, 100]
|
|
db2.close()
|
|
|
|
|
|
def test_delete_where_analyze(fresh_db):
|
|
table = fresh_db["table"]
|
|
table.insert_all(({"id": i, "i": i} for i in range(10)), pk="id")
|
|
table.create_index(["i"], analyze=True)
|
|
assert "sqlite_stat1" in fresh_db.table_names()
|
|
assert list(fresh_db["sqlite_stat1"].rows) == [
|
|
{"tbl": "table", "idx": "idx_table_i", "stat": "10 1"}
|
|
]
|
|
table.delete_where("id > ?", [5], analyze=True)
|
|
assert list(fresh_db["sqlite_stat1"].rows) == [
|
|
{"tbl": "table", "idx": "idx_table_i", "stat": "6 1"}
|
|
]
|