-
Notifications
You must be signed in to change notification settings - Fork 5
fix: add unique constraint on conference to prevent duplicate split #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
agonza1
merged 3 commits into
peermetrics:master
from
Boanerges1996:fix/conference-unique-constraint
Apr 9, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
app/migrations/0002_conference_unique_conference_id_app.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Generated by Django 2.2.28 on 2026-04-09 13:49 | ||
|
|
||
| from django.db import migrations | ||
| from django.db.models import Count, Min | ||
|
|
||
|
|
||
| def merge_duplicate_conferences(apps, schema_editor): | ||
| """ | ||
| Before adding the unique constraint, merge duplicate conferences | ||
| (same conference_id + app_id). Keep the oldest one, move all | ||
| related data to it, and delete the rest. | ||
| """ | ||
| from django.db import transaction | ||
|
|
||
| Conference = apps.get_model('app', 'Conference') | ||
| GenericEvent = apps.get_model('app', 'GenericEvent') | ||
| Connection = apps.get_model('app', 'Connection') | ||
| Session = apps.get_model('app', 'Session') | ||
| Issue = apps.get_model('app', 'Issue') | ||
| Summary = apps.get_model('app', 'Summary') | ||
|
|
||
| dupes = (Conference.objects | ||
| .values('conference_id', 'app_id') | ||
| .annotate(count=Count('id'), earliest=Min('created_at')) | ||
| .filter(count__gt=1)) | ||
|
|
||
| for dupe in dupes: | ||
| with transaction.atomic(): | ||
| conferences = Conference.objects.filter( | ||
| conference_id=dupe['conference_id'], | ||
| app_id=dupe['app_id'], | ||
| ).order_by('created_at') | ||
|
|
||
| keeper = conferences.first() | ||
| duplicates = conferences.exclude(pk=keeper.pk) | ||
| keeper_has_summary = Summary.objects.filter(conference=keeper).exists() | ||
|
|
||
| for dup in duplicates: | ||
| GenericEvent.objects.filter(conference=dup).update(conference=keeper) | ||
| Connection.objects.filter(conference=dup).update(conference=keeper) | ||
| Session.objects.filter(conference=dup).update(conference=keeper) | ||
| Issue.objects.filter(conference=dup).update(conference=keeper) | ||
|
|
||
| # Summary is OneToOne — keep the first one, delete the rest | ||
| if not keeper_has_summary: | ||
| Summary.objects.filter(conference=dup).update(conference=keeper) | ||
| keeper_has_summary = True | ||
| else: | ||
| Summary.objects.filter(conference=dup).delete() | ||
|
|
||
| for participant in dup.participants.all(): | ||
| keeper.participants.add(participant) | ||
|
|
||
| dup.delete() | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('app', '0001_initial'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(merge_duplicate_conferences, migrations.RunPython.noop), | ||
| ] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Generated by Django 2.2.28 on 2026-04-09 13:49 | ||
|
|
||
| from django.db import migrations | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('app', '0002_conference_unique_conference_id_app'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterUniqueTogether( | ||
| name='conference', | ||
| unique_together={('conference_id', 'app')}, | ||
| ), | ||
| ] |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Django docs now recommend
constraintswithUniqueConstraintover the olderunique_together(which is soft-deprecated since Django 4.2). Since you're on Django 2.2, both work —UniqueConstraintwas introduced in 2.2. Not a blocker, just flagging for when you upgrade: