Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table_config instead of table_metadata #2257

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ def __init__(
# Move any "plugins" settings from metadata to config - updates them in place
metadata = metadata or {}
config = config or {}
move_plugins(metadata, config)
metadata, config = move_plugins(metadata, config)
# Now migrate any known table configuration settings over as well
move_table_config(metadata, config)
metadata, config = move_table_config(metadata, config)

self._metadata_local = metadata or {}
self.sqlite_extensions = []
Expand Down
13 changes: 10 additions & 3 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from contextlib import contextmanager
import click
from collections import OrderedDict, namedtuple, Counter
import copy
import base64
import hashlib
import inspect
Expand All @@ -17,7 +18,7 @@
import types
import secrets
import shutil
from typing import Iterable
from typing import Iterable, Tuple
import urllib
import yaml
from .shutil_backport import copytree
Expand Down Expand Up @@ -1301,11 +1302,13 @@
d.pop(key, None)


def move_plugins(source: dict, destination: dict):
def move_plugins(source: dict, destination: dict) -> Tuple[dict, dict]:
"""
Move 'plugins' keys from source to destination dictionary. Creates hierarchy in destination if needed.
After moving, recursively remove any keys in the source that are left empty.
"""
source = copy.deepcopy(source)
destination = copy.deepcopy(destination)
Comment on lines +1310 to +1311
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit worried about the performance overhead of this - not on Datasette itself (it only starts up once) but on the Datasette test suite which might call this hundreds of times.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to ignore this problem in the interest of getting this release done.


def recursive_move(src, dest, path=None):
if path is None:
Expand All @@ -1329,6 +1332,7 @@

recursive_move(source, destination)
prune_empty_dicts(source)
return source, destination


_table_config_keys = (
Expand All @@ -1351,10 +1355,12 @@
Move all known table configuration keys from metadata to config.
"""
if "databases" not in metadata:
return
return metadata, config
metadata = copy.deepcopy(metadata)
config = copy.deepcopy(config)
for database_name, database in metadata["databases"].items():
if "tables" not in database:
continue

Check warning on line 1363 in datasette/utils/__init__.py

View check run for this annotation

Codecov / codecov/patch

datasette/utils/__init__.py#L1363

Added line #L1363 was not covered by tests
for table_name, table in database["tables"].items():
for key in _table_config_keys:
if key in table:
Expand All @@ -1366,6 +1372,7 @@
key
)
prune_empty_dicts(metadata)
return metadata, config


def redact_keys(original: dict, key_patterns: Iterable) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ def test_databases_json(app_client_two_attached_databases_one_immutable):
@pytest.mark.asyncio
async def test_metadata_json(ds_client):
response = await ds_client.get("/-/metadata.json")
assert response.json() == METADATA
assert response.json() == ds_client.ds.metadata()


@pytest.mark.asyncio
Expand Down
2 changes: 1 addition & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ async def test_metadata_json_html(ds_client):
response = await ds_client.get("/-/metadata")
assert response.status_code == 200
pre = Soup(response.content, "html.parser").find("pre")
assert METADATA == json.loads(pre.text)
assert ds_client.ds.metadata() == json.loads(pre.text)


@pytest.mark.asyncio
Expand Down
Loading