# Generated by Django 2.2.20 on 2021-04-27 18:12

from django.db import migrations


class Migration(migrations.Migration):
    """
    Clean records in the `CERTIFICATES_GENERATEDCERTIFICATE` table that are in the downloadable state but also have
    old errors still part of their certificate record.

    As part of this migration we are also altering the Managers for the GeneratedCertificate model. We need the ability
    to access the `objects` attribute for the cleanup.
    """
    def cleanup_certificate_records(apps, schema_editor):
        GeneratedCertificate = apps.get_model('certificates', 'GeneratedCertificate')

        GeneratedCertificate.objects.filter(status='downloadable').exclude(error_reason='').update(error_reason='')

    dependencies = [
        ('certificates', '0024_delete_allowlistgenerationconfiguration'),
    ]

    operations = [
        migrations.AlterModelManagers(
            name='generatedcertificate',
            managers=[
            ],
        ),
        migrations.RunPython(cleanup_certificate_records, reverse_code=migrations.RunPython.noop)
    ]
