# Generated by Django 3.1.2 on 2020-11-05 12:16

from django.db import migrations


def convert_null_to_empty(apps, schema_editor):
    Barrier = apps.get_model("barriers", "Barrier")
    HistoricalBarrier = apps.get_model("barriers", "HistoricalBarrier")
    PublicBarrier = apps.get_model("barriers", "PublicBarrier")
    HistoricalPublicBarrier = apps.get_model("barriers", "HistoricalPublicBarrier")
    BarrierCommodity = apps.get_model("barriers", "BarrierCommodity")

    barrier_fields = (
        "archived_explanation",
        "archived_reason",
        "next_steps_summary",
        "other_source",
        "priority_summary",
        "product",
        "source",
        "status_summary",
        "sub_status",
        "sub_status_other",
        "summary",
        "title",
        "trading_bloc",
        "unarchived_reason",
    )

    for field in barrier_fields:
        Barrier.objects.filter(**{f"{field}__isnull": True}).update(**{field: ""})
        HistoricalBarrier.objects.filter(**{f"{field}__isnull": True}).update(
            **{field: ""}
        )

    BarrierCommodity.objects.filter(trading_bloc__isnull=True).update(trading_bloc="")

    public_barrier_fields = (
        "_summary",
        "_title",
        "archived_reason",
        "internal_summary_at_update",
        "internal_title_at_update",
        "trading_bloc",
        "unarchived_reason",
    )

    for field in public_barrier_fields:
        PublicBarrier.objects.filter(**{f"{field}__isnull": True}).update(**{field: ""})
        HistoricalPublicBarrier.objects.filter(**{f"{field}__isnull": True}).update(
            **{field: ""}
        )


class Migration(migrations.Migration):

    dependencies = [
        ("barriers", "0094_rename_barrier_model"),
    ]

    operations = [
        migrations.RunPython(
            convert_null_to_empty, reverse_code=migrations.RunPython.noop
        ),
    ]
