From 7818e8b9d15a3d50c16f080dc7fe4b5e8eb3d241 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 7 Mar 2024 00:03:42 -0500 Subject: [PATCH] Hide tables starting with an _, refs #2104 --- datasette/database.py | 1 + docs/pages.rst | 15 +++++++++++++++ tests/test_api.py | 15 +++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/datasette/database.py b/datasette/database.py index 4e590d3a..ffe94ea7 100644 --- a/datasette/database.py +++ b/datasette/database.py @@ -469,6 +469,7 @@ class Database: and ( sql like '%VIRTUAL TABLE%USING FTS%' ) or name in ('sqlite_stat1', 'sqlite_stat2', 'sqlite_stat3', 'sqlite_stat4') + or name like '\\_%' escape '\\' """ ) ).rows diff --git a/docs/pages.rst b/docs/pages.rst index 2ce05428..1c3e2c1e 100644 --- a/docs/pages.rst +++ b/docs/pages.rst @@ -40,6 +40,21 @@ The JSON version of this page provides programmatic access to the underlying dat * `fivethirtyeight.datasettes.com/fivethirtyeight.json `_ * `global-power-plants.datasettes.com/global-power-plants.json `_ +.. _DatabaseView_hidden: + +Hidden tables +------------- + +Some tables listed on the database page are treated as hidden. Hidden tables are not completely invisible - they can be accessed through the "hidden tables" link at the bottom of the page. They are hidden because they represent low-level implementation details which are generally not useful to end-users of Datasette. + +The following tables are hidden by default: + +- Any table with a name that starts with an underscore - this is a Datasette convention to help plugins easily hide their own internal tables. +- Tables that have been configured as ``"hidden": true`` using :ref:`metadata_hiding_tables`. +- ``*_fts`` tables that implement SQLite full-text search indexes. +- Tables relating to the inner workings of the SpatiaLite SQLite extension. +- ``sqlite_stat`` tables used to store statistics used by the query optimizer. + .. _TableView: Table diff --git a/tests/test_api.py b/tests/test_api.py index 7a25b55e..4ad55d72 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1018,6 +1018,21 @@ async def test_hidden_sqlite_stat1_table(): ) +@pytest.mark.asyncio +async def test_hide_tables_starting_with_underscore(): + ds = Datasette() + db = ds.add_memory_database("test_hide_tables_starting_with_underscore") + await db.execute_write("create table normal (id integer primary key, name text)") + await db.execute_write("create table _hidden (id integer primary key, name text)") + data = ( + await ds.client.get( + "/test_hide_tables_starting_with_underscore.json?_show_hidden=1" + ) + ).json() + tables = [(t["name"], t["hidden"]) for t in data["tables"]] + assert tables == [("normal", False), ("_hidden", True)] + + @pytest.mark.asyncio @pytest.mark.parametrize("db_name", ("foo", r"fo%o", "f~/c.d")) async def test_tilde_encoded_database_names(db_name):