From 4af18681ca39d47d1f283be803c5ae2ca29ddd01 Mon Sep 17 00:00:00 2001 From: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com> Date: Mon, 13 Jul 2026 00:54:42 -0700 Subject: [PATCH] Fix VACUUM with an open transaction Database.vacuum() passed VACUUM directly to SQLite, which rejects it while a transaction is open. Commit first so the maintenance operation can run, and cover the reported transaction path with a regression test. Fixes #479 --- sqlite_utils/db.py | 1 + tests/test_create.py | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index e97b7d9cb..b1f95f6c9 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -1897,6 +1897,7 @@ def index_foreign_keys(self) -> None: def vacuum(self) -> None: "Run a SQLite ``VACUUM`` against the database." + self.commit() self.execute("VACUUM;") def analyze(self, name: Optional[str] = None) -> None: diff --git a/tests/test_create.py b/tests/test_create.py index d281eb4e8..250896d81 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -1170,6 +1170,17 @@ def test_vacuum(fresh_db): fresh_db.vacuum() +def test_vacuum_commits_open_transaction(fresh_db): + fresh_db["data"].insert({"foo": "foo"}) + fresh_db.begin() + fresh_db.execute("insert into data (foo) values ('bar')") + + fresh_db.vacuum() + + assert not fresh_db.conn.in_transaction + assert [row["foo"] for row in fresh_db["data"].rows] == ["foo", "bar"] + + def test_works_with_pathlib_path(tmpdir): path = pathlib.Path(tmpdir / "test.db") db = Database(path)