# Generated by Django 3.2.13 on 2022-05-31 09:53

from django.db import migrations
from django.db.models.functions import Collate

from olympia import amo


def fix_missing_denied_guids_for_blocked_addons(apps, schema_editor):
    Addon = apps.get_model('addons', 'Addon')
    Block = apps.get_model('blocklist', 'Block')
    DeniedGuid = apps.get_model('addons', 'DeniedGuid')
    missing_denied_guids = (
        Addon.unfiltered.filter(
            guid__in=Block.objects.filter(min_version='0', max_version='*').values(
                guidc=Collate('guid', 'utf8mb4_unicode_ci')
            ),
            status=amo.STATUS_DELETED,
        )
        .exclude(guid__in=DeniedGuid.objects.values('guid'))
        .values_list('guid', flat=True)
        .distinct()
    )
    DeniedGuid.objects.bulk_create(
        [DeniedGuid(guid=guid) for guid in missing_denied_guids]
    )


class Migration(migrations.Migration):

    dependencies = [
        ('addons', '0038_auto_20220530_1639'),
    ]

    operations = [
        migrations.RunPython(fix_missing_denied_guids_for_blocked_addons),
    ]

Add migration to insert missing DeniedGuids from blocked add-ons (#19308)