From f91869500b0cb2ebeb785ee13464cad9aefba801 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 6 Feb 2026 16:57:18 -0600 Subject: Fetch a single resource ID: delete data from one resource at a time. --- gn_auth/auth/authorisation/data/phenotypes.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gn_auth/auth/authorisation/data/phenotypes.py b/gn_auth/auth/authorisation/data/phenotypes.py index d335314..91f761a 100644 --- a/gn_auth/auth/authorisation/data/phenotypes.py +++ b/gn_auth/auth/authorisation/data/phenotypes.py @@ -206,6 +206,23 @@ def fetch_data_link_ids( return tuple(uuid.UUID(row["data_link_id"]) for row in cursor.fetchall()) +def fetch_resource_id(cursor: authdb.DbCursor, + data_link_ids: tuple[uuid.UUID, ...]) -> uuid.UUID: + """Retrieve the ID of the resource where the data is linked to. + + RAISES: InvalidResourceError in the case where more the data_link_ids belong + to more than one resource.""" + _paramstr = ", ".join(["?"] * len(data_link_ids)) + cursor.execute( + "SELECT DISTINCT(resource_id) FROM phenotype_resources " + f"WHERE data_link_id IN ({_paramstr})", + tuple(str(_id) for _id in data_link_ids)) + _ids = tuple(uuid.UUID(row['resource_id']) for row in cursor.fetchall()) + if len(_ids) != 1: + raise InvalidResourceError("...") + return _ids[0] + + def delete_linked_data( cursor: authdb.DbCursor, data_link_ids: tuple[uuid.UUID, ...] @@ -248,6 +265,7 @@ def delete_linked_phenotypes_data( # TODO: Use background job, for huge number of xref_ids data_link_ids = fetch_data_link_ids( cursor, species_id, population_id, dataset_id, xref_ids) + resource_id = fetch_resource_id(cursor, data_link_ids) _resources_ids = unlink_from_resources(cursor, data_link_ids) delete_resources(cursor, _resources_ids) _deleted = delete_linked_data(cursor, data_link_ids) -- cgit 1.4.1