Stop running sqlite-utils plugins on Datasette connections

Wrapping a connection in sqlite_utils.Database() runs sqlite-utils
plugins' prepare_connection hooks against it by default. Datasette's
write API views and introspection helpers now pass execute_plugins=False
(matching what utils/internal_db.py already did), so third-party
sqlite-utils plugins no longer touch Datasette's connections.

Also apply PRAGMA recursive_triggers=on in Datasette._prepare_connection
so every connection gets consistent trigger semantics - previously only
the write connection got it, as a side effect of the first sqlite-utils
based write.

Refs #2831

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N76afGMhBRQk528VF1LTpR
This commit is contained in:
Claude 2026-07-09 06:03:42 +00:00
commit 5cec9c9faa
No known key found for this signature in database
8 changed files with 75 additions and 8 deletions

View file

@ -2782,3 +2782,38 @@ async def test_insert_with_return_failing_row_is_atomic(ds_write):
await ds_write.get_database("data").execute("select count(*) from docs")
).single_value()
assert count == 0
@pytest.mark.asyncio
async def test_write_api_does_not_run_sqlite_utils_plugins(ds_write):
# https://github.com/simonw/datasette/issues/2831
# sqlite-utils plugins should not have their prepare_connection hooks
# executed against Datasette's connections
import sqlite_utils.plugins
from sqlite_utils import hookimpl
prepared = []
class TrackingPlugin:
@hookimpl
def prepare_connection(self, conn):
prepared.append(conn)
sqlite_utils.plugins.pm.register(TrackingPlugin(), name="datasette-test-tracking")
try:
token = write_token(ds_write)
response = await ds_write.client.post(
"/data/docs/-/insert",
json={"rows": [{"id": 1, "title": "one"}]},
headers=_headers(token),
)
assert response.status_code == 201
response = await ds_write.client.post(
"/data/docs/1/-/update",
json={"update": {"title": "two"}},
headers=_headers(token),
)
assert response.status_code == 200
assert prepared == []
finally:
sqlite_utils.plugins.pm.unregister(name="datasette-test-tracking")

View file

@ -1302,3 +1302,24 @@ async def test_database_close_is_idempotent(tmpdir):
# Second call should be a no-op, not raise
db.close()
ds._internal_database.close()
@pytest.mark.asyncio
async def test_recursive_triggers_enabled_on_all_connections(tmp_path):
# https://github.com/simonw/datasette/issues/2831
# Previously recursive_triggers was only enabled on the write connection,
# and only as a side effect of the first sqlite-utils based write - so
# trigger semantics could differ between connections
path = str(tmp_path / "test.db")
sqlite3.connect(path).close()
datasette = Datasette([path])
db = datasette.get_database("test")
write_value = await db.execute_write_fn(
lambda conn: conn.execute("PRAGMA recursive_triggers").fetchone()[0],
transaction=False,
)
read_value = await db.execute_fn(
lambda conn: conn.execute("PRAGMA recursive_triggers").fetchone()[0]
)
assert write_value == 1
assert read_value == 1