# Generated by Django 3.2.2 on 2021-05-14 12:08

from django.db import migrations


def assign_psp_reference_values(apps, schema_editor):
    Payment = apps.get_model("payment", "Payment")
    payments = []
    for payment in Payment.objects.filter(is_active=True).iterator():
        txn = (
            payment.transactions.filter(searchable_key__isnull=False)
            .exclude(searchable_key="")
            .first()
        )
        if txn:
            payment.psp_reference = txn.searchable_key
            payments.append(payment)

    if payments:
        Payment.objects.bulk_update(payments, ["psp_reference"])


class Migration(migrations.Migration):

    dependencies = [
        ("payment", "0026_payment_psp_reference"),
    ]

    operations = [
        migrations.RunPython(assign_psp_reference_values, migrations.RunPython.noop),
    ]
