mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Sometimes sort tables by number of relationships, closes #460
This commit is contained in:
parent
689cf9c139
commit
faf33515b2
4 changed files with 36 additions and 4 deletions
|
|
@ -30,6 +30,7 @@ from .utils import (
|
||||||
detect_spatialite,
|
detect_spatialite,
|
||||||
escape_css_string,
|
escape_css_string,
|
||||||
escape_sqlite,
|
escape_sqlite,
|
||||||
|
get_all_foreign_keys,
|
||||||
get_outbound_foreign_keys,
|
get_outbound_foreign_keys,
|
||||||
get_plugins,
|
get_plugins,
|
||||||
module_from_path,
|
module_from_path,
|
||||||
|
|
@ -261,6 +262,11 @@ class ConnectedDatabase:
|
||||||
)
|
)
|
||||||
return [r[0] for r in results.rows]
|
return [r[0] for r in results.rows]
|
||||||
|
|
||||||
|
async def get_all_foreign_keys(self):
|
||||||
|
return await self.ds.execute_against_connection_in_thread(
|
||||||
|
self.name, get_all_foreign_keys
|
||||||
|
)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
tags = []
|
tags = []
|
||||||
if self.is_mutable:
|
if self.is_mutable:
|
||||||
|
|
|
||||||
|
|
@ -29,9 +29,7 @@ class DatabaseView(BaseView):
|
||||||
table_counts = await db.table_counts(5)
|
table_counts = await db.table_counts(5)
|
||||||
views = await db.view_names()
|
views = await db.view_names()
|
||||||
hidden_table_names = set(await db.hidden_table_names())
|
hidden_table_names = set(await db.hidden_table_names())
|
||||||
all_foreign_keys = await self.ds.execute_against_connection_in_thread(
|
all_foreign_keys = await db.get_all_foreign_keys()
|
||||||
database, get_all_foreign_keys
|
|
||||||
)
|
|
||||||
|
|
||||||
metadata = (self.ds.metadata("databases") or {}).get(database, {})
|
metadata = (self.ds.metadata("databases") or {}).get(database, {})
|
||||||
self.ds.update_with_inherited_metadata(metadata)
|
self.ds.update_with_inherited_metadata(metadata)
|
||||||
|
|
|
||||||
|
|
@ -54,14 +54,27 @@ class IndexView(RenderMixin):
|
||||||
"fts_table": await self.ds.execute_against_connection_in_thread(
|
"fts_table": await self.ds.execute_against_connection_in_thread(
|
||||||
name, lambda conn: detect_fts(conn, table)
|
name, lambda conn: detect_fts(conn, table)
|
||||||
),
|
),
|
||||||
|
"num_relationships_for_sorting": 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if request.args.get("_sort") == "relationships" or not table_counts:
|
||||||
|
# We will be sorting by number of relationships, so populate that field
|
||||||
|
all_foreign_keys = await db.get_all_foreign_keys()
|
||||||
|
for table, foreign_keys in all_foreign_keys.items():
|
||||||
|
count = len(foreign_keys["incoming"] + foreign_keys["outgoing"])
|
||||||
|
tables[table]["num_relationships_for_sorting"] = count
|
||||||
|
|
||||||
hidden_tables = [t for t in tables.values() if t["hidden"]]
|
hidden_tables = [t for t in tables.values() if t["hidden"]]
|
||||||
visible_tables = [t for t in tables.values() if not t["hidden"]]
|
visible_tables = [t for t in tables.values() if not t["hidden"]]
|
||||||
|
|
||||||
tables_and_views_truncated = list(
|
tables_and_views_truncated = list(
|
||||||
sorted(
|
sorted(
|
||||||
(t for t in tables.values() if t not in hidden_tables),
|
(t for t in tables.values() if t not in hidden_tables),
|
||||||
key=lambda t: t["count"] or 0,
|
key=lambda t: (
|
||||||
|
t["num_relationships_for_sorting"],
|
||||||
|
t["count"] or 0,
|
||||||
|
t["name"],
|
||||||
|
),
|
||||||
reverse=True,
|
reverse=True,
|
||||||
)[:TRUNCATE_AT]
|
)[:TRUNCATE_AT]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,21 @@ def test_homepage(app_client):
|
||||||
assert d["views_count"] == 4
|
assert d["views_count"] == 4
|
||||||
|
|
||||||
|
|
||||||
|
def test_homepage_sort_by_relationships(app_client):
|
||||||
|
response = app_client.get("/.json?_sort=relationships")
|
||||||
|
assert response.status == 200
|
||||||
|
tables = [
|
||||||
|
t["name"] for t in response.json["fixtures"]["tables_and_views_truncated"]
|
||||||
|
]
|
||||||
|
assert [
|
||||||
|
"simple_primary_key",
|
||||||
|
"complex_foreign_keys",
|
||||||
|
"searchable_tags",
|
||||||
|
"foreign_key_references",
|
||||||
|
"facetable",
|
||||||
|
] == tables
|
||||||
|
|
||||||
|
|
||||||
def test_database_page(app_client):
|
def test_database_page(app_client):
|
||||||
response = app_client.get("/fixtures.json")
|
response = app_client.get("/fixtures.json")
|
||||||
data = response.json
|
data = response.json
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue