From 9b38f42272994350a6bb58da796dcca86ee628d9 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> Date: Mon, 30 Sep 2019 15:17:03 +0200 Subject: Migrate nominations to new Nomination model Before the migration to Django, we stored meta data on a nomination, such as the `reason` and `end_reason`, in the infraction table using "note" infractions, using a special prefix for the `reason`. We have since decided to move nominations out of the infraction context by creating a special `Nomination` model. However, given the complexity of the data migration, we did not yet migrate the old nomination data to this new model. This commit migrates that data by performing a data migration. The data migration works as follows: - Query all nomination data in chronological order; - Replay all nominations and add them to the `Nomination` model; - Delete the now obsolete `Infraction` entry. In addition, this commit also adds a useful string representation for `Nomination` objects. --- ...inations_from_infraction_to_nomination_model.py | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 pydis_site/apps/api/migrations/0044_migrate_nominations_from_infraction_to_nomination_model.py (limited to 'pydis_site/apps/api/migrations') diff --git a/pydis_site/apps/api/migrations/0044_migrate_nominations_from_infraction_to_nomination_model.py b/pydis_site/apps/api/migrations/0044_migrate_nominations_from_infraction_to_nomination_model.py new file mode 100644 index 00000000..a56450c0 --- /dev/null +++ b/pydis_site/apps/api/migrations/0044_migrate_nominations_from_infraction_to_nomination_model.py @@ -0,0 +1,64 @@ +# Generated by Django 2.2.5 on 2019-09-30 12:15 +import logging + +from django.db import migrations +from django.db.models import Q + +log = logging.getLogger('nomination_migration') + + +def migrate_nominations_to_new_model(apps, schema_editor): + """ + Migrations nominations from the infraction model to the nomination model. + + This migration works by replaying the nomination history in chronological order, adding and + ending nominations as we've recorded them. + """ + Infraction = apps.get_model('api', 'Infraction') + Nomination = apps.get_model('api', 'Nomination') + + all_nominations = ( + Q(reason__startswith="Helper nomination:") | Q(reason__startswith="Unwatched (talent-pool):") + ) + + for infraction in Infraction.objects.filter(all_nominations).order_by('inserted_at'): + if infraction.reason.startswith("Helper nomination:"): + if Nomination.objects.filter(user=infraction.user, active=True).exists(): + log.error( + f"User `{infraction.user.id}` already has an active nomination, aborting." + ) + continue + nomination = Nomination( + user=infraction.user, + inserted_at=infraction.inserted_at, + reason=infraction.reason[19:], # Strip "Helper nomination: " prefix + actor=infraction.actor, + active=True, + ) + nomination.save() + infraction.delete() + elif infraction.reason.startswith("Unwatched (talent-pool):"): + if not Nomination.objects.filter(user=infraction.user, active=True).exists(): + log.error( + f"User `{infraction.user.id}` has no active nomination, can't end it!" + ) + continue + nomination = Nomination.objects.get(user=infraction.user, active=True) + nomination.end_reason = infraction.reason[25:] # Strip "Unwatched (talent-pool):" + nomination.ended_at = infraction.inserted_at + nomination.active = False + nomination.save() + infraction.delete() + else: + log.error(f"I don't understand this infraction: {infraction}") + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0043_infraction_hidden_warnings_to_notes'), + ] + + operations = [ + migrations.RunPython(migrate_nominations_to_new_model), + ] -- cgit v1.2.3 From 87105f2c31e5384428e9f44a146b60d2f95fb67b Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Fri, 11 Oct 2019 19:49:07 +0200 Subject: Pluralize properly. --- .../migrations/0044_add_plural_name_for_log_entry.py | 17 +++++++++++++++++ pydis_site/apps/api/models/log_entry.py | 5 +++++ 2 files changed, 22 insertions(+) create mode 100644 pydis_site/apps/api/migrations/0044_add_plural_name_for_log_entry.py (limited to 'pydis_site/apps/api/migrations') diff --git a/pydis_site/apps/api/migrations/0044_add_plural_name_for_log_entry.py b/pydis_site/apps/api/migrations/0044_add_plural_name_for_log_entry.py new file mode 100644 index 00000000..6f388179 --- /dev/null +++ b/pydis_site/apps/api/migrations/0044_add_plural_name_for_log_entry.py @@ -0,0 +1,17 @@ +# Generated by Django 2.2.3 on 2019-10-11 17:48 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0043_infraction_hidden_warnings_to_notes'), + ] + + operations = [ + migrations.AlterModelOptions( + name='logentry', + options={'verbose_name_plural': 'Log entries'}, + ), + ] diff --git a/pydis_site/apps/api/models/log_entry.py b/pydis_site/apps/api/models/log_entry.py index 55a022d7..cb7275f9 100644 --- a/pydis_site/apps/api/models/log_entry.py +++ b/pydis_site/apps/api/models/log_entry.py @@ -61,3 +61,8 @@ class LogEntry(ModelReprMixin, models.Model): level = self.level[:4].upper() return f'{timestamp} | {self.application} | {level} | {message}' + + class Meta: + """Customizes the default generated plural name to valid English.""" + + verbose_name_plural = 'Log entries' -- cgit v1.2.3 From 673cc3bd89056d288451085488ae6f1f67cc1e21 Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Fri, 11 Oct 2019 21:47:58 +0200 Subject: Resolve migration merge conflicts. --- .../migrations/0044_add_plural_name_for_log_entry.py | 17 ----------------- .../migrations/0045_add_plural_name_for_log_entry.py | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 17 deletions(-) delete mode 100644 pydis_site/apps/api/migrations/0044_add_plural_name_for_log_entry.py create mode 100644 pydis_site/apps/api/migrations/0045_add_plural_name_for_log_entry.py (limited to 'pydis_site/apps/api/migrations') diff --git a/pydis_site/apps/api/migrations/0044_add_plural_name_for_log_entry.py b/pydis_site/apps/api/migrations/0044_add_plural_name_for_log_entry.py deleted file mode 100644 index 6f388179..00000000 --- a/pydis_site/apps/api/migrations/0044_add_plural_name_for_log_entry.py +++ /dev/null @@ -1,17 +0,0 @@ -# Generated by Django 2.2.3 on 2019-10-11 17:48 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('api', '0043_infraction_hidden_warnings_to_notes'), - ] - - operations = [ - migrations.AlterModelOptions( - name='logentry', - options={'verbose_name_plural': 'Log entries'}, - ), - ] diff --git a/pydis_site/apps/api/migrations/0045_add_plural_name_for_log_entry.py b/pydis_site/apps/api/migrations/0045_add_plural_name_for_log_entry.py new file mode 100644 index 00000000..6b9933d8 --- /dev/null +++ b/pydis_site/apps/api/migrations/0045_add_plural_name_for_log_entry.py @@ -0,0 +1,17 @@ +# Generated by Django 2.2.3 on 2019-10-11 17:48 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0044_migrate_nominations_from_infraction_to_nomination_model'), + ] + + operations = [ + migrations.AlterModelOptions( + name='logentry', + options={'verbose_name_plural': 'Log entries'}, + ), + ] -- cgit v1.2.3