from django.db import migrations


def drop_events(apps, schema_editor):
    Event = apps.get_model('logging', 'Event')
    Event.objects.filter(event_type='openstack_security_group_rule_pulled').delete()


class Migration(migrations.Migration):

    dependencies = [
        ('logging', '0007_drop_alerts'),
    ]

    operations = [migrations.RunPython(drop_events)]

@@ -32,338 +32,632 @@
 s%0A%0A%0A
-def drop_events(apps, schema_editor
+class Migration(migrations.Migration
 ):%0A
+%0A
     
-Event = apps.get_model('logging', 'Event
+dependencies = %5B%0A        ('logging', '0007_drop_alerts
 ')
+,
 %0A    
-Event.objects.filter(event_type='openstack_security_group_rule_pulled').delete()%0A%0A%0Aclass Migration(migrations.Migration):%0A%0A    dependencies = %5B%0A        ('logging', '0007_drop_alerts'),%0A
+%5D%0A%0A    # Run SQL instead of Run Python is used to avoid OOM error%0A    # See also: https://docs.djangoproject.com/en/3.1/ref/models/querysets/#django.db.models.query.QuerySet.delete%0A    operations = %5B%0A        migrations.RunSQL(%0A            %22DELETE FROM logging_feed WHERE event_id in (SELECT id from logging_event WHERE event_type='openstack_security_group_rule_pulled')%22%0A   
     
-%5D%0A
+ ),
 %0A    
-operations = %5Bmigrations.RunPython(drop_events)
+    migrations.RunSQL(%0A            %22DELETE FROM logging_event WHERE event_type='openstack_security_group_rule_pulled'%22%0A        ),%0A    
 %5D%0A

Fix migration script to avoid OOM error.