Skip to content

Commit

Permalink
Checking which readset(s) in causing the unique constraint failure
Browse files Browse the repository at this point in the history
  • Loading branch information
paulstretenowich committed Feb 6, 2024
1 parent 9c3d349 commit 0e725c3
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions project_tracking/db_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,22 @@ def __init__(self, message=None, entity=None, attribute=None, value=None):
else:
self.message = f"'{entity}' with '{attribute}' '{value}' already exists in the database and '{attribute}' has to be unique"

def unique_constraint_error(json_format, ingest_data):
def unique_constraint_error(session, json_format, ingest_data):
"""
When unique constraint errors, checks which entity is causing the error and returns it/them
"""
ret = []
if json_format == "run_processing":
for patient_json in ingest_data[vb.PATIENT]:
for sample_json in patient_json[vb.SAMPLE]:
for readset_json in sample_json[vb.READSET]:
ret.append(f"'Readset' with 'name' '{readset_json[vb.READSET_NAME]}' already exists in the database and 'name' has to be unique")
readset_name = readset_json[vb.READSET_NAME]
stmt = (
select(Readset)
.where(Readset.name.is_(readset_name))
)
if session.scalars(stmt).unique().all():
ret.append(f"'Readset' with 'name' '{readset_name}' already exists in the database and 'name' has to be unique")
return ret


Expand Down Expand Up @@ -687,7 +696,7 @@ def ingest_run_processing(project_id: str, ingest_data, session=None):
session.flush()
except exc.IntegrityError as error:
session.rollback()
message = unique_constraint_error("run_processing", ingest_data)
message = unique_constraint_error(session, "run_processing", ingest_data)
raise UniqueConstraintError(message=message) from error

operation_id = operation.id
Expand Down

0 comments on commit 0e725c3

Please sign in to comment.