Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions app/migrations/0002_conference_unique_conference_id_app.py
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),
]
17 changes: 17 additions & 0 deletions app/migrations/0003_conference_unique_together.py
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')},
),
]
1 change: 1 addition & 0 deletions app/models/conference.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Conference(BaseModel):

class Meta:
db_table = 'conference'
unique_together = (('conference_id', 'app'),)
Copy link
Copy Markdown
Contributor Author

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 constraints with UniqueConstraint over the older unique_together (which is soft-deprecated since Django 4.2). Since you're on Django 2.2, both work — UniqueConstraint was introduced in 2.2. Not a blocker, just flagging for when you upgrade:

class Meta:
    db_table = 'conference'
    constraints = [
        models.UniqueConstraint(fields=['conference_id', 'app'], name='unique_conference_per_app')
    ]


cache_keys = (
sorted(('id',)),
Expand Down