Skip to content

Commit a0fab5b

Browse files
authored
Merge pull request #936 from hubmapconsortium/Derek-Furst/avoid-unecessary-linkeages
removed 2 outdated comments about linking to ancestors
2 parents 0562f36 + f447d7d commit a0fab5b

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/schema/provenance_schema.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ ENTITIES:
462462
exposed: false
463463
indexed: false
464464
description: "The uuids of source entities from which this new entity is derived. Used to pass source entity ids in on POST or PUT calls used to create the linkages."
465-
# Note: link_dataset_to_direct_ancestors() will always delete all the old linkages first
466465
after_create_trigger: link_dataset_to_direct_ancestors
467466
after_update_trigger: link_dataset_to_direct_ancestors
468467
direct_ancestors:
@@ -1062,7 +1061,6 @@ ENTITIES:
10621061
exposed: false
10631062
indexed: false
10641063
description: "The uuid of source entity from which this new entity is derived from. Used on creation or edit to create an action and relationship to the ancestor. The direct ancestor must be a Donor or Sample. If the direct ancestor is a Donor, the sample must be of type organ."
1065-
# Note: link_sample_to_direct_ancestor() will always delete all the old linkages first
10661064
after_create_trigger: link_sample_to_direct_ancestor
10671065
after_update_trigger: link_sample_to_direct_ancestor
10681066
before_property_update_validators:

src/schema/schema_triggers.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -913,18 +913,22 @@ def link_dataset_to_direct_ancestors(property_key, normalized_type, request, use
913913
activity_uuid = activity_data_dict['uuid']
914914
create_activity = True
915915

916-
if new_ancestors:
917-
916+
if new_ancestors:
917+
logger.info(f"Linking the following new ancestors: {', '.join(new_ancestors)}")
918918
try:
919919
schema_neo4j_queries.add_new_ancestors_to_existing_activity(schema_manager.get_neo4j_driver_instance(), list(new_ancestors), activity_uuid, create_activity, activity_data_dict, dataset_uuid)
920920
except TransactionError:
921921
raise
922922

923923
if ancestors_to_unlink:
924+
logger.info(f"Unlinking the following ancestors: {', '.join(ancestors_to_unlink)}")
924925
try:
925926
schema_neo4j_queries.delete_ancestor_linkages_tx(schema_manager.get_neo4j_driver_instance(), dataset_uuid, list(ancestors_to_unlink))
926927
except TransactionError:
927928
raise
929+
930+
if not(ancestors_to_unlink or new_ancestors):
931+
logger.info("No new ancestors linked, nor old ancestors unlinked")
928932

929933

930934
"""
@@ -1929,13 +1933,19 @@ def link_sample_to_direct_ancestor(property_key, normalized_type, request, user_
19291933
create_activity = False
19301934
activity_data_dict = {}
19311935
sample_uuid = existing_data_dict['uuid']
1936+
new_ancestors = None
1937+
ancestors_to_unlink = None
19321938

19331939
# Build a list of direct ancestor uuids
19341940
# Only one uuid in the list in this case
19351941
direct_ancestor_uuids = [new_data_dict['direct_ancestor_uuid']]
19361942
existing_sample_ancestor_uuids = schema_neo4j_queries.get_sample_direct_ancestor(schema_manager.get_neo4j_driver_instance(), sample_uuid, "uuid")
1937-
new_ancestors = set(direct_ancestor_uuids)-set(existing_sample_ancestor_uuids)
1938-
ancestors_to_unlink = set(existing_sample_ancestor_uuids)-set(direct_ancestor_uuids)
1943+
if isinstance(existing_sample_ancestor_uuids, list):
1944+
existing_sample_ancestor_uuids = existing_sample_ancestor_uuids[0]
1945+
if existing_sample_ancestor_uuids and existing_sample_ancestor_uuids != direct_ancestor_uuids:
1946+
new_ancestors = direct_ancestor_uuids
1947+
if not existing_sample_ancestor_uuids:
1948+
new_ancestors = direct_ancestor_uuids
19391949
activity_uuid = schema_neo4j_queries.get_parent_activity_uuid_from_entity(schema_manager.get_neo4j_driver_instance(), sample_uuid)
19401950

19411951
if not activity_uuid:
@@ -1944,17 +1954,22 @@ def link_sample_to_direct_ancestor(property_key, normalized_type, request, user_
19441954
create_activity = True
19451955

19461956
if new_ancestors:
1947-
1957+
ancestors_to_unlink = existing_sample_ancestor_uuids
1958+
logger.info(f"Linking the following new ancestors: {new_ancestors}")
19481959
try:
1949-
schema_neo4j_queries.add_new_ancestors_to_existing_activity(schema_manager.get_neo4j_driver_instance(), list(new_ancestors), activity_uuid, create_activity, activity_data_dict, sample_uuid)
1960+
schema_neo4j_queries.add_new_ancestors_to_existing_activity(schema_manager.get_neo4j_driver_instance(), new_ancestors, activity_uuid, create_activity, activity_data_dict, sample_uuid)
19501961
except TransactionError:
19511962
raise
1952-
1963+
19531964
if ancestors_to_unlink:
1965+
logger.info(f"Unlinking the following ancestor: {ancestors_to_unlink}")
19541966
try:
1955-
schema_neo4j_queries.delete_ancestor_linkages_tx(schema_manager.get_neo4j_driver_instance(), sample_uuid, list(ancestors_to_unlink))
1967+
schema_neo4j_queries.delete_ancestor_linkages_tx(schema_manager.get_neo4j_driver_instance(), sample_uuid, [ancestors_to_unlink])
19561968
except TransactionError:
19571969
raise
1970+
1971+
if not(ancestors_to_unlink or new_ancestors):
1972+
logger.info("No new ancestors linked, nor old ancestors unlinked")
19581973

19591974
"""
19601975
TriggerTypeEnum.BEFORE_CREATE and TriggerTypeEnum.BEFORE_UPDATE

0 commit comments

Comments
 (0)