Skip to content

Commit

Permalink
New introspection endpoint: /-/databases - closes #470
Browse files Browse the repository at this point in the history
Refs #419 and #465
  • Loading branch information
simonw committed May 16, 2019
1 parent 21b57cd commit 0dad111
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
17 changes: 17 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,19 @@ def register_custom_units(self):
for unit in self.metadata("custom_units") or []:
ureg.define(unit)

def connected_databases(self):
return [
{
"name": d.name,
"path": d.path,
"size": d.size,
"is_mutable": d.is_mutable,
"is_memory": d.is_memory,
"hash": d.hash,
}
for d in self.databases.values()
]

def versions(self):
conn = sqlite3.connect(":memory:")
self.prepare_connection(conn)
Expand Down Expand Up @@ -810,6 +823,10 @@ async def handle_request(self, request, write_callback, stream_callback):
JsonDataView.as_view(self, "config.json", lambda: self._config),
r"/-/config<as_format:(\.json)?$>",
)
app.add_route(
JsonDataView.as_view(self, "databases.json", self.connected_databases),
r"/-/databases<as_format:(\.json)?$>",
)
app.add_route(
DatabaseDownload.as_view(self), r"/<db_name:[^/]+?><as_db:(\.db)$>"
)
Expand Down
16 changes: 16 additions & 0 deletions docs/introspection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,19 @@ Shows the :ref:`config` options for this instance of Datasette. `Config example
"max_returned_rows": 1000,
"sql_time_limit_ms": 1000
}

/-/databases
------------

Shows currently attached databases. `Databases example <https://latest.datasette.io/-/config>`_::

[
{
"hash": null,
"is_memory": false,
"is_mutable": true,
"name": "fixtures",
"path": "fixtures.db",
"size": 225280
}
]
7 changes: 7 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ def app_client_two_attached_databases():
)


@pytest.fixture(scope="session")
def app_client_two_attached_databases_one_immutable():
yield from make_app_client(
is_immutable=True, extra_databases={"extra_database.db": EXTRA_DATABASE_SQL}
)


@pytest.fixture(scope="session")
def app_client_with_memory():
yield from make_app_client(memory=True)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
app_client_shorter_time_limit,
app_client_larger_cache_size,
app_client_returned_rows_matches_page_size,
app_client_two_attached_databases_one_immutable,
app_client_with_cors,
app_client_with_dot,
generate_compound_rows,
Expand Down Expand Up @@ -1058,6 +1059,22 @@ def test_unit_filters(app_client):
assert data["rows"][0][0] == 2


def test_databases_json(app_client_two_attached_databases_one_immutable):
response = app_client_two_attached_databases_one_immutable.get("/-/databases.json")
databases = response.json
assert 2 == len(databases)
extra_database, fixtures_database = databases
assert "extra_database" == extra_database["name"]
assert None == extra_database["hash"]
assert True == extra_database["is_mutable"]
assert False == extra_database["is_memory"]

assert "fixtures" == fixtures_database["name"]
assert fixtures_database["hash"] is not None
assert False == fixtures_database["is_mutable"]
assert False == fixtures_database["is_memory"]


def test_metadata_json(app_client):
response = app_client.get("/-/metadata.json")
assert METADATA == response.json
Expand Down

0 comments on commit 0dad111

Please sign in to comment.