Skip to content

Commit

Permalink
Hide tables starting with an _, refs #2104
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 7, 2024
1 parent a395256 commit 7818e8b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions datasette/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ async def hidden_table_names(self):
and (
sql like '%VIRTUAL TABLE%USING FTS%'
) or name in ('sqlite_stat1', 'sqlite_stat2', 'sqlite_stat3', 'sqlite_stat4')
or name like '\\_%' escape '\\'
"""
)
).rows
Expand Down
15 changes: 15 additions & 0 deletions docs/pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ The JSON version of this page provides programmatic access to the underlying dat
* `fivethirtyeight.datasettes.com/fivethirtyeight.json <https://fivethirtyeight.datasettes.com/fivethirtyeight.json>`_
* `global-power-plants.datasettes.com/global-power-plants.json <https://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
Expand Down
15 changes: 15 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 7818e8b

Please sign in to comment.