Add exception handling for DB transactions#228
Open
harsh-kumar-patwa wants to merge 2 commits intoThe-OpenROAD-Project:masterfrom
Open
Add exception handling for DB transactions#228harsh-kumar-patwa wants to merge 2 commits intoThe-OpenROAD-Project:masterfrom
harsh-kumar-patwa wants to merge 2 commits intoThe-OpenROAD-Project:masterfrom
Conversation
1257bb6 to
342da80
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves resilience around SQLAlchemy session failures by ensuring failed write operations roll back transactions and by adding API-layer handling for database exceptions, including a fallback for chat history persistence.
Changes:
- Add
try/except+db.rollback()around DB write operations incrud.py(and log before re-raising). - Catch
SQLAlchemyErrorin conversation CRUD endpoints and convert them into explicit HTTP 500 responses. - Add tests asserting
rollback()is invoked whencommit()fails in each write CRUD function.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| backend/src/database/crud.py | Wraps write operations with rollback + logging on SQLAlchemyError. |
| backend/src/api/routers/conversations.py | Adds DB exception handling for CRUD endpoints and introduces db_persist fallback logic for chat persistence. |
| backend/tests/test_database_crud.py | Adds unit tests to verify rollback behavior on commit failures. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Harsh Kumar <harshkumar3446@gmail.com>
342da80 to
cac4c4d
Compare
Signed-off-by: Harsh Kumar <harshkumar3446@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Right now, if a database commit or delete fails in any of the CRUD functions, there's no rollback — the SQLAlchemy session is left in a broken state. The API endpoints also let these errors bubble up as raw 500s, and the chat endpoints crash instead of falling back to in-memory history.
This PR fixes that.
Changes
crud.py— wrapped all 5 write functions (create_conversation,update_conversation_title,delete_conversation,create_message,delete_message) with try/except so they calldb.rollback()on failure before re-raisingconversations.py— CRUD endpoints now catch DB errors and return proper HTTP 500 responses. Chat endpoints use adb_persistflag so if the DB call fails, they fall back to in-memory history instead of crashing.get_history_stralso handles DB read failures gracefully.test_database_crud.py— added 5 tests to verify rollback is called when a commit failsContext
Resolves #185
All existing tests pass (54/54), no lint issues.