From 4d24bf6b34c0d9ed6e6831f68f625bee7f447b85 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 5 Mar 2024 18:14:55 -0800 Subject: [PATCH] Don't explain an explain even in the demo, refs #2293 --- docs/plugin_hooks.rst | 6 ++++-- tests/plugins/my_plugin.py | 8 +++----- tests/test_plugins.py | 7 ++++++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/plugin_hooks.rst b/docs/plugin_hooks.rst index 62062bfd..ecc8f058 100644 --- a/docs/plugin_hooks.rst +++ b/docs/plugin_hooks.rst @@ -1557,7 +1557,10 @@ This example adds a new query action linking to a page for explaining a query: @hookimpl - def query_actions(datasette, database, sql): + def query_actions(datasette, database, query_name, sql): + # Don't explain an explain + if sql.lower().startswith("explain"): + return return [ { "href": datasette.urls.database(database) @@ -1571,7 +1574,6 @@ This example adds a new query action linking to a page for explaining a query: }, ] - .. _plugin_hook_database_actions: database_actions(datasette, actor, database, request) diff --git a/tests/plugins/my_plugin.py b/tests/plugins/my_plugin.py index 650cc57d..01324213 100644 --- a/tests/plugins/my_plugin.py +++ b/tests/plugins/my_plugin.py @@ -393,11 +393,9 @@ def table_actions(datasette, database, table, actor): @hookimpl def query_actions(datasette, database, query_name, sql): - args = { - "sql": sql, - } - if query_name: - args["query_name"] = query_name + # Don't explain an explain + if sql.lower().startswith("explain"): + return return [ { "href": datasette.urls.database(database) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 4620af51..d1da16fa 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -954,6 +954,8 @@ async def test_hook_table_actions(ds_client, table_or_view): "/fixtures/pragma_cache_size", "/fixtures?sql=explain+PRAGMA+cache_size%3B", ), + # Don't attempt to explain an explain + ("/fixtures?sql=explain+select+1", None), ), ) async def test_hook_query_actions(ds_client, path, expected_url): @@ -967,7 +969,10 @@ async def test_hook_query_actions(ds_client, path, expected_url): response = await ds_client.get(path) assert response.status_code == 200 links = get_table_actions_links(response.text) - assert links == [{"label": "Explain this query", "href": expected_url}] + if expected_url is None: + assert links == [] + else: + assert links == [{"label": "Explain this query", "href": expected_url}] @pytest.mark.asyncio