Skip to content

fix: prevent duplicate enrolment report creation during sync#517

Merged
voneiden merged 1 commit into
mainfrom
seer/fix/enrolment-report-integrity-error
Jun 25, 2026
Merged

fix: prevent duplicate enrolment report creation during sync#517
voneiden merged 1 commit into
mainfrom
seer/fix/enrolment-report-integrity-error

Conversation

@sentry

@sentry sentry Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

This PR addresses an IntegrityError: duplicate key value violates unique constraint "reports_enrolmentreport_enrolment_id_key" that occurs during the sync_enrolment_reports operation.

Problem:
The sync_enrolment_reports function, specifically the create_missing method, attempts to insert new EnrolmentReport instances 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 second INSERT operation for the same enrolment_id then fails due to the unique constraint on the _enrolment (which maps to enrolment_id) field.

Solution:
To resolve this, ignore_conflicts=True has been added to the bulk_create call within the create_missing method. This modification instructs the database to silently skip any INSERT operations that would result in a unique constraint violation. This ensures that if an EnrolmentReport for a given enrolment_id already exists, no error is raised, and the existing record is preserved, effectively handling the race condition without requiring complex locking mechanisms or ON CONFLICT clauses.

Fixes KULTUS-API-7

Summary by CodeRabbit

  • Bug Fixes
    • Improved report generation reliability by skipping duplicate or conflicting entries when creating missing enrolment reports.
    • Reduced the chance of failures during bulk creation, helping reports complete more consistently.

@sentry sentry Bot requested a review from a team as a code owner June 23, 2026 12:14
@azure-pipelines

Copy link
Copy Markdown

KULTUS-API branch is deployed to platta: https://kultus-pr517.api.dev.hel.ninja 🚀🚀🚀

@voneiden voneiden force-pushed the seer/fix/enrolment-report-integrity-error branch from 2ee2941 to 5a9d33d Compare June 24, 2026 08:29
@voneiden voneiden changed the title fix: Prevent duplicate enrolment report creation during sync fix: prevent duplicate enrolment report creation during sync Jun 24, 2026
@sonarqubecloud

Copy link
Copy Markdown

@voneiden

Copy link
Copy Markdown
Contributor

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 97af8331-38d0-43bc-8ba3-476c0c4762f8

📥 Commits

Reviewing files that changed from the base of the PR and between cd3d76b and 5a9d33d.

📒 Files selected for processing (1)
  • reports/models.py

📝 Walkthrough

Walkthrough

EnrolmentReportManager.create_missing in reports/models.py is updated to pass ignore_conflicts=True to bulk_create, so duplicate or conflicting rows are silently skipped instead of raising a database error.

Changes

EnrolmentReport conflict handling

Layer / File(s) Summary
bulk_create ignore_conflicts flag
reports/models.py
ignore_conflicts=True is added to the bulk_create call in create_missing, causing conflicting rows to be silently skipped during bulk insertion.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

A bunny once tripped on a duplicate row,
And crashed through the warren with errors in tow.
Now ignore_conflicts hops gently past,
No more loud exceptions, peace found at last!
🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main fix: preventing duplicate EnrolmentReport creation during sync.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch seer/fix/enrolment-report-integrity-error

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@azure-pipelines

Copy link
Copy Markdown

KULTUS-API branch is deployed to platta: https://kultus-pr517.api.dev.hel.ninja 🚀🚀🚀

@mhieta mhieta left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@voneiden voneiden merged commit 7dfa0db into main Jun 25, 2026
33 checks passed
@voneiden voneiden deleted the seer/fix/enrolment-report-integrity-error branch June 25, 2026 07:01
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.

2 participants