Browse Source

Replace backfillchannels with a data migration

master
Pēteris Caune 3 years ago
parent
commit
6bb0c77934
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
2 changed files with 24 additions and 24 deletions
  1. +0
    -24
      hc/api/management/commands/backfillchannels.py
  2. +24
    -0
      hc/api/migrations/0082_fill_last_notify.py

+ 0
- 24
hc/api/management/commands/backfillchannels.py View File

@ -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

+ 24
- 0
hc/api/migrations/0082_fill_last_notify.py View File

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

Loading…
Cancel
Save