From e36c014290abe9583f4134464bc3f8e602c7b846 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Fri, 12 Jan 2024 11:18:39 -0500 Subject: [PATCH] fix(database): allow filtering by UUID (#26469) --- superset/databases/api.py | 1 + tests/unit_tests/databases/api_test.py | 40 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/superset/databases/api.py b/superset/databases/api.py index 8de84a16af6dc..62fb72943749f 100644 --- a/superset/databases/api.py +++ b/superset/databases/api.py @@ -205,6 +205,7 @@ class DatabaseRestApi(BaseSupersetModelRestApi): "changed_by", "database_name", "expose_in_sqllab", + "uuid", ] search_filters = {"allow_file_upload": [DatabaseUploadEnabledFilter]} allowed_rel_fields = {"changed_by", "created_by"} diff --git a/tests/unit_tests/databases/api_test.py b/tests/unit_tests/databases/api_test.py index 28ca123ec66a0..cf3e64c306210 100644 --- a/tests/unit_tests/databases/api_test.py +++ b/tests/unit_tests/databases/api_test.py @@ -29,6 +29,46 @@ from sqlalchemy.orm.session import Session +def test_filter_by_uuid( + session: Session, + client: Any, + full_api_access: None, +) -> None: + """ + Test that we can filter databases by UUID. + + Note: this functionality is not used by the Superset UI, but is needed by 3rd + party tools that use the Superset API. If this tests breaks, please make sure + that the functionality is properly deprecated between major versions with + enough warning so that tools can be adapted. + """ + from superset.databases.api import DatabaseRestApi + from superset.models.core import Database + + DatabaseRestApi.datamodel.session = session + + # create table for databases + Database.metadata.create_all(session.get_bind()) # pylint: disable=no-member + session.add( + Database( + database_name="my_db", + sqlalchemy_uri="sqlite://", + uuid=UUID("7c1b7880-a59d-47cd-8bf1-f1eb8d2863cb"), + ) + ) + session.commit() + + response = client.get( + "/api/v1/database/?q=(filters:!((col:uuid,opr:eq,value:" + "%277c1b7880-a59d-47cd-8bf1-f1eb8d2863cb%27)))" + ) + assert response.status_code == 200 + + payload = response.json + assert len(payload["result"]) == 1 + assert payload["result"][0]["uuid"] == "7c1b7880-a59d-47cd-8bf1-f1eb8d2863cb" + + def test_post_with_uuid( session: Session, client: Any,