Skip to content

Commit

Permalink
bug workaround in history table
Browse files Browse the repository at this point in the history
  • Loading branch information
khoroshevskyi committed Jul 8, 2024
1 parent a63e982 commit 7b469fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion pepdbagent/modules/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,12 @@ def _update_samples(
old_samples = session.scalars(select(Samples).where(Samples.project_id == project_id))

old_samples_mapping: dict = {sample.guid: sample for sample in old_samples}

# old_child_parent_id needed because of the parent_guid is sometimes set to none in sqlalchemy mapping :( bug
old_child_parent_id: Dict[str, str] = {
child: mapping.parent_guid for child, mapping in old_samples_mapping.items()
}

old_samples_ids_set: set = set(old_samples_mapping.keys())
new_samples_ids_list: list = [
new_sample[PEPHUB_SAMPLE_ID_KEY]
Expand Down Expand Up @@ -751,14 +757,16 @@ def _update_samples(
old_samples_mapping[current_id].sample = sample_value
old_samples_mapping[current_id].sample_name = sample_value[sample_name_key]

# !bug workaround: if project was deleted and sometimes old_samples_mapping[current_id].parent_guid
# and it can cause an error in history. For this we have `old_child_parent_id` dict
if old_samples_mapping[current_id].parent_guid != parent_id:
if history_sa_model:
if current_history:
current_history.parent_guid = parent_id
else:
current_history = HistorySamples(
guid=old_samples_mapping[current_id].guid,
parent_guid=old_samples_mapping[current_id].parent_guid,
parent_guid=old_child_parent_id[current_id],
sample_json=old_samples_mapping[current_id].sample,
change_type=UpdateTypes.UPDATE,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_project_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_get_add_history_all_project(self, namespace, name, sample_name):
prj_init = agent.project.get(namespace, name, tag="default", raw=False)
prj = agent.project.get(namespace, name, tag="default", with_id=True)

prj["_sample_dict"][0]["sample_name"] = "new_sample_name"
# prj["_sample_dict"][0]["sample_name"] = "new_sample_name"

del prj["_sample_dict"][1]
del prj["_sample_dict"][2]
Expand Down

0 comments on commit 7b469fe

Please sign in to comment.