Skip to content

Commit

Permalink
Use transaction context manager
Browse files Browse the repository at this point in the history
This code is raising an InvalidRequestError.
  • Loading branch information
majamassarini committed Nov 12, 2024
1 parent f04011d commit 0deb846
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions packit_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2341,33 +2341,31 @@ def add_scan_transaction(self) -> Generator["OSHScanModel"]:
raise: IntegrityError if the scan model already exists
"""
session = singleton_session or Session()
session.begin()

try:
scan = OSHScanModel()
scan.copr_build_target = self
session.add(scan)
session.commit()
except Exception as ex:
logger.warning(f"Exception while working with database: {ex!r}")
session.rollback()
raise

try:
yield scan
except Exception as ex:
logger.warning(f"{ex!r}")
session.rollback()
raise

try:
session.add(scan)
session.commit()
except Exception as ex:
logger.warning(f"Exception while working with database: {ex!r}")
session.rollback()
raise
with sa_session_transaction() as session:
try:
scan = OSHScanModel()
scan.copr_build_target = self
session.add(scan)
session.commit()
except Exception as ex:
logger.warning(f"Exception while working with database: {ex!r}")
session.rollback()
raise

try:
yield scan
except Exception as ex:
logger.warning(f"{ex!r}")
session.rollback()
raise

try:
session.add(scan)
session.commit()
except Exception as ex:
logger.warning(f"Exception while working with database: {ex!r}")
session.rollback()
raise


class KojiBuildGroupModel(ProjectAndEventsConnector, GroupModel, Base):
Expand Down

0 comments on commit 0deb846

Please sign in to comment.