Skip to content
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

bugfix/migration0073 #1290

Merged
merged 2 commits into from
Jan 15, 2025
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# Generated by Django 5.1.3 on 2025-01-07 13:37
from collections import defaultdict

from django.db import migrations, models

def remove_duplicate_citations(apps, schema_editor):
Citation = apps.get_model("redbox_core", "Citation")

duplicates = defaultdict(list)
for citation in Citation.objects.all():
duplicates[(citation.file_id, citation.chat_message_id)].append(citation.id)


# Step 2: Remove duplicates
for ids in duplicates.values():
if len(ids) > 1:
# Keep the first, delete the rest
# ids[0] is retained, ids[1:] are deleted
Citation.objects.filter(id__in=ids[1:]).delete()

def back_populate_new_source_files(apps, schema_editor):
ChatMessage = apps.get_model("redbox_core", "ChatMessage")
for chat_message in ChatMessage.objects.all():
Expand All @@ -16,6 +32,7 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(remove_duplicate_citations, migrations.RunPython.noop),
migrations.AddField(
model_name='chatmessage',
name='new_source_files',
Expand Down
7 changes: 5 additions & 2 deletions django_app/tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,11 @@ def test_0073_chatmessage_new_source_files(original_file, migrator):
)

Citation = old_state.apps.get_model("redbox_core", "Citation")
citation = Citation(chat_message=chat_message, file=file, source="USER UPLOADED DOCUMENT", text="hello!")
citation.save()
citation_1 = Citation(chat_message=chat_message, file=file, source="USER UPLOADED DOCUMENT", text="hello!")
citation_1.save()

citation_2 = Citation(chat_message=chat_message, file=file, source="USER UPLOADED DOCUMENT", text="good bye!")
citation_2.save()

new_state = migrator.apply_tested_migration(
("redbox_core", "0073_chatmessage_new_source_files"),
Expand Down
Loading