From 6bb0c77934b0ac87f545325ad716d3f25c565267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 21 Oct 2021 12:35:29 +0300 Subject: [PATCH] Replace backfillchannels with a data migration --- .../management/commands/backfillchannels.py | 24 ------------------- hc/api/migrations/0082_fill_last_notify.py | 24 +++++++++++++++++++ 2 files changed, 24 insertions(+), 24 deletions(-) delete mode 100644 hc/api/management/commands/backfillchannels.py create mode 100644 hc/api/migrations/0082_fill_last_notify.py diff --git a/hc/api/management/commands/backfillchannels.py b/hc/api/management/commands/backfillchannels.py deleted file mode 100644 index bec56968..00000000 --- a/hc/api/management/commands/backfillchannels.py +++ /dev/null @@ -1,24 +0,0 @@ -from django.core.management.base import BaseCommand - -from hc.api.models import Channel, Notification - - -class Command(BaseCommand): - help = "Backfill Channel.last_notify and Channel.last_error" - - def handle(self, *args, **options): - total = 0 - - for channel in Channel.objects.all(): - q = Channel.objects.filter(id=channel.id) - - try: - n = Notification.objects.filter(channel=channel).latest() - q.update(last_notify=n.created, last_error=n.error) - total += 1 - except Notification.DoesNotExist: - if channel.last_error: - q.update(last_error="") - total += 1 - - return "Done! Updated %d channels." % total diff --git a/hc/api/migrations/0082_fill_last_notify.py b/hc/api/migrations/0082_fill_last_notify.py new file mode 100644 index 00000000..ddebafc7 --- /dev/null +++ b/hc/api/migrations/0082_fill_last_notify.py @@ -0,0 +1,24 @@ +# Generated by Django 3.2.8 on 2021-10-21 09:30 + +from django.db import migrations + + +def fill_last_notify(apps, schema_editor): + Channel = apps.get_model("api", "Channel") + Notification = apps.get_model("api", "Notification") + for channel in Channel.objects.filter(last_notify=None): + try: + n = Notification.objects.filter(channel=channel).latest() + channel.last_notify = n.created + channel.save() + except Notification.DoesNotExist: + pass + + +class Migration(migrations.Migration): + + dependencies = [ + ("api", "0081_channel_last_notify"), + ] + + operations = [migrations.RunPython(fill_last_notify, migrations.RunPython.noop)]