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)]