Skip to content

Commit

Permalink
Don't run prepare_connection() on internal database, closes #2468
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 18, 2025
1 parent e59fd01 commit 209bdee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
# https://github.com/simonw/datasette/issues/283#issuecomment-781591015
SQLITE_LIMIT_ATTACHED = 10

INTERNAL_DB_NAME = "__INTERNAL__"

Setting = collections.namedtuple("Setting", ("name", "default", "help"))
SETTINGS = (
Setting("default_page_size", 100, "Default page size for the table view"),
Expand Down Expand Up @@ -328,7 +330,7 @@ def __init__(
self._internal_database = Database(self, memory_name=secrets.token_hex())
else:
self._internal_database = Database(self, path=internal, mode="rwc")
self._internal_database.name = "__INTERNAL__"
self._internal_database.name = INTERNAL_DB_NAME

self.cache_headers = cache_headers
self.cors = cors
Expand Down Expand Up @@ -878,7 +880,7 @@ async def get_canned_query(self, database_name, query_name, actor):
def _prepare_connection(self, conn, database):
conn.row_factory = sqlite3.Row
conn.text_factory = lambda x: str(x, "utf-8", "replace")
if self.sqlite_extensions:
if self.sqlite_extensions and database != INTERNAL_DB_NAME:
conn.enable_load_extension(True)
for extension in self.sqlite_extensions:
# "extension" is either a string path to the extension
Expand All @@ -891,7 +893,8 @@ def _prepare_connection(self, conn, database):
if self.setting("cache_size_kb"):
conn.execute(f"PRAGMA cache_size=-{self.setting('cache_size_kb')}")
# pylint: disable=no-member
pm.hook.prepare_connection(conn=conn, database=database, datasette=self)
if database != INTERNAL_DB_NAME:
pm.hook.prepare_connection(conn=conn, database=database, datasette=self)
# If self.crossdb and this is _memory, connect the first SQLITE_LIMIT_ATTACHED databases
if self.crossdb and database == "_memory":
count = 0
Expand Down
2 changes: 2 additions & 0 deletions docs/plugin_hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ arguments and can be called like this::

select random_integer(1, 10);

``prepare_connection()`` hooks are not called for Datasette's :ref:`internal database <internals_internal>`.

Examples: `datasette-jellyfish <https://datasette.io/plugins/datasette-jellyfish>`__, `datasette-jq <https://datasette.io/plugins/datasette-jq>`__, `datasette-haversine <https://datasette.io/plugins/datasette-haversine>`__, `datasette-rure <https://datasette.io/plugins/datasette-rure>`__

.. _plugin_hook_prepare_jinja2_environment:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ async def test_hook_plugin_prepare_connection_arguments(ds_client):
"database=fixtures, datasette.plugin_config(\"name-of-plugin\")={'depth': 'root'}"
] == response.json()

# Function should not be available on the internal database
db = ds_client.ds.get_internal_database()
with pytest.raises(sqlite3.OperationalError):
await db.execute("select prepare_connection_args()")


@pytest.mark.asyncio
@pytest.mark.parametrize(
Expand Down

0 comments on commit 209bdee

Please sign in to comment.