Skip to content

Commit

Permalink
Dev (#32)
Browse files Browse the repository at this point in the history
* Adding a raise for unique constraint failed on readset when ingesting run processing

* Moving the raise for unique constraint failed on readset when ingesting run processing when flushing

* Checking which readset(s) in causing the unique constraint failure
  • Loading branch information
paulstretenowich authored Feb 7, 2024
1 parent c507442 commit abacbbb
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions project_tracking/db_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, message=None, table=None, attribute=None, query=None):
if message:
self.message = message
else:
self.message = f"'{table}' with '{attribute}' '{query}' doesn't exist on database"
self.message = f"'{table}' with '{attribute}' '{query}' doesn't exist in the database"

class RequestError(Error):
"""RequestError"""
Expand All @@ -70,6 +70,34 @@ def __init__(self, message=None, argument=None):
else:
self.message = f"For current request '{argument}' is required"

class UniqueConstraintError(Error):
"""UniqueConstraintError"""
def __init__(self, message=None, entity=None, attribute=None, value=None):
super().__init__(message)
if message:
self.message = message
else:
self.message = f"'{entity}' with '{attribute}' '{value}' already exists in the database and '{attribute}' has to be unique"

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]:
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


def name_to_id(model_class, name, session=None):
"""
Converting a given name into its id
Expand Down Expand Up @@ -604,7 +632,6 @@ def ingest_run_processing(project_id: str, ingest_data, session=None):
kit_expiration_date=kit_expiration_date,
session=session
)
# Let this fails if readset already exists.
readset = Readset(
name=readset_json[vb.READSET_NAME],
lane=LaneEnum(readset_json[vb.READSET_LANE]),
Expand Down Expand Up @@ -665,7 +692,12 @@ def ingest_run_processing(project_id: str, ingest_data, session=None):
)

session.add(readset)
session.flush()
try:
session.flush()
except exc.IntegrityError as error:
session.rollback()
message = unique_constraint_error(session, "run_processing", ingest_data)
raise UniqueConstraintError(message=message) from error

operation_id = operation.id
job_id = job.id
Expand Down

0 comments on commit abacbbb

Please sign in to comment.