From 66c628bfd4acb9e4b9fc3c50576fd916e7acc281 Mon Sep 17 00:00:00 2001 From: nickmwangemi Date: Wed, 17 Jan 2024 12:30:45 +0300 Subject: [PATCH] Add data migration --- peachjam/admin.py | 2 +- ...nd_delete_old_order_outcome_field_data.py} | 20 +++++++++++++------ peachjam/models/judgment.py | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) rename peachjam/migrations/{0115_change_order_outcome_field_to_many_to_many.py => 0115_copy_and_delete_old_order_outcome_field_data.py} (55%) diff --git a/peachjam/admin.py b/peachjam/admin.py index a3303943e..c222663f7 100644 --- a/peachjam/admin.py +++ b/peachjam/admin.py @@ -730,7 +730,7 @@ class JudgmentAdmin(ImportExportMixin, DocumentAdmin): CaseNumberAdmin, JudgmentRelationshipStackedInline, ] + DocumentAdmin.inlines - filter_horizontal = ("judges", "attorneys", "order_outcome") + filter_horizontal = ("judges", "attorneys", "order_outcomes") list_filter = (*DocumentAdmin.list_filter, "court") fieldsets = copy.deepcopy(DocumentAdmin.fieldsets) diff --git a/peachjam/migrations/0115_change_order_outcome_field_to_many_to_many.py b/peachjam/migrations/0115_copy_and_delete_old_order_outcome_field_data.py similarity index 55% rename from peachjam/migrations/0115_change_order_outcome_field_to_many_to_many.py rename to peachjam/migrations/0115_copy_and_delete_old_order_outcome_field_data.py index 81f8d388a..7e80f734a 100644 --- a/peachjam/migrations/0115_change_order_outcome_field_to_many_to_many.py +++ b/peachjam/migrations/0115_copy_and_delete_old_order_outcome_field_data.py @@ -1,22 +1,30 @@ -# Generated by Django 3.2.19 on 2024-01-16 13:40 +# Generated by Django 3.2.19 on 2024-01-17 09:20 from django.db import migrations, models -class Migration(migrations.Migration): +def copy_data(apps, schema_editor): + Judgment = apps.get_model("peachjam", "Judgment") + + for judgment in Judgment.objects.all().iterator(chunk_size=100): + judgment.order_outcomes.add(judgment.order_outcome) + judgment.save() + +class Migration(migrations.Migration): dependencies = [ ("peachjam", "0114_alter_court_country"), ] operations = [ - migrations.RemoveField( + migrations.AddField( model_name="judgment", - name="order_outcome", + name="order_outcomes", + field=models.ManyToManyField(blank=True, to="peachjam.OrderOutcome"), ), - migrations.AddField( + migrations.RunPython(copy_data), + migrations.RemoveField( model_name="judgment", name="order_outcome", - field=models.ManyToManyField(blank=True, to="peachjam.OrderOutcome"), ), ] diff --git a/peachjam/models/judgment.py b/peachjam/models/judgment.py index 35d53726d..c4e999d74 100644 --- a/peachjam/models/judgment.py +++ b/peachjam/models/judgment.py @@ -202,7 +202,7 @@ class Judgment(CoreDocument): attorneys = models.ManyToManyField( Attorney, blank=True, verbose_name=_("attorneys") ) - order_outcome = models.ManyToManyField( + order_outcomes = models.ManyToManyField( OrderOutcome, blank=True, )