From 623331b3f4ee78e1ed2618ccfc2e18e0bd77cd36 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 5 Jul 2026 12:16:11 -0700 Subject: [PATCH] Fix for test failure against sqlean --- sqlite_utils/db.py | 12 ++++++++---- tests/test_query.py | 9 +++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index f796a63..a8f8e17 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -713,10 +713,14 @@ class Database: args: tuple = (params,) if params is not None else () if keyword == "PRAGMA": # Some PRAGMA statements refuse to run inside a transaction, so - # execute these without the savepoint guard used below. PRAGMAs - # never open an implicit transaction, so there is nothing to - # undo if this one turns out not to return rows - cursor = self.conn.execute(sql, *args) + # execute these without the savepoint guard used below. Some + # adapters open an implicit transaction before comment-prefixed + # PRAGMAs, so temporarily use driver autocommit when it is safe. + if self.conn.in_transaction: + cursor = self.conn.execute(sql, *args) + else: + with self.ensure_autocommit_off(): + cursor = self.conn.execute(sql, *args) if cursor.description is None: raise ValueError(message) keys = [d[0] for d in cursor.description] diff --git a/tests/test_query.py b/tests/test_query.py index 1d5c1f6..b9822e1 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -114,6 +114,15 @@ def test_query_comment_prefixed_pragma(tmpdir): db.close() +def test_query_comment_prefixed_pragma_inside_transaction(fresh_db): + fresh_db.begin() + assert list(fresh_db.query("-- check version\npragma user_version")) == [ + {"user_version": 0} + ] + assert fresh_db.conn.in_transaction + fresh_db.rollback() + + @pytest.mark.parametrize( "sql,expected", [