fix: prevent duplicate enrolment report creation during sync#517
Conversation
|
KULTUS-API branch is deployed to platta: https://kultus-pr517.api.dev.hel.ninja 🚀🚀🚀 |
2ee2941 to
5a9d33d
Compare
|
|
@coderabbitai review |
✅ Action performedReview finished.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
ChangesEnrolmentReport conflict handling
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
KULTUS-API branch is deployed to platta: https://kultus-pr517.api.dev.hel.ninja 🚀🚀🚀 |



This PR addresses an
IntegrityError: duplicate key value violates unique constraint "reports_enrolmentreport_enrolment_id_key"that occurs during thesync_enrolment_reportsoperation.Problem:
The
sync_enrolment_reportsfunction, specifically thecreate_missingmethod, attempts to insert newEnrolmentReportinstances into the database. In scenarios where multiple sync operations run concurrently (e.g., a cron job and a manual trigger, or across different application instances), a race condition can occur. Both operations might identify the same enrolment as 'missing' and attempt to create a report for it. The secondINSERToperation for the sameenrolment_idthen fails due to the unique constraint on the_enrolment(which maps toenrolment_id) field.Solution:
To resolve this,
ignore_conflicts=Truehas been added to thebulk_createcall within thecreate_missingmethod. This modification instructs the database to silently skip anyINSERToperations that would result in a unique constraint violation. This ensures that if anEnrolmentReportfor a givenenrolment_idalready exists, no error is raised, and the existing record is preserved, effectively handling the race condition without requiring complex locking mechanisms orON CONFLICTclauses.Fixes KULTUS-API-7
Summary by CodeRabbit