Skip to content

fix: make session deletion transactional#429

Open
hariom888 wants to merge 1 commit into
Canopus-Labs:mainfrom
hariom888:fix/373-atomic-session-deletion
Open

fix: make session deletion transactional#429
hariom888 wants to merge 1 commit into
Canopus-Labs:mainfrom
hariom888:fix/373-atomic-session-deletion

Conversation

@hariom888

Copy link
Copy Markdown
Contributor

Summary

Fixes #373.

This PR makes deleteSession atomic by wrapping the deletion of both the session and its associated questions inside a MongoDB transaction.

Previously, Question.deleteMany() and Session.deleteOne() were executed as two independent operations. If a failure occurred after deleting the questions but before deleting the session, the database could be left in an inconsistent state where the session still existed but its associated questions had already been removed.

Changes

  • Wrapped the session lookup, question deletion, and session deletion in a MongoDB transaction.
  • Executed all database operations using the same transaction session.
  • Ensured that either all deletions are committed together or all changes are rolled back if any operation fails.
  • Preserved the existing API response format and error handling.

Why this Fix

Using a MongoDB transaction guarantees atomicity across both collections.

If every operation succeeds:

  • The session is deleted.
  • All associated questions are deleted.

If any operation fails:

  • The transaction is rolled back.
  • Neither the session nor its questions are removed.

This prevents partial deletes and keeps the database in a consistent state.

Testing

  • Verified successful deletion removes both the session and its associated questions.
  • Simulated failures during session deletion and confirmed the transaction rolls back all changes.
  • Verified existing API behavior and responses remain unchanged.

Impact

This change is backward compatible and improves data consistency by preventing partial deletion scenarios and orphaned records during session deletion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: deleteSession is non-transactional — partial failure orphans questions or leaves sessions undeleted

1 participant