# Generated by Django 5.0.2 on 2024-02-21 20:53

from django.db import migrations


def migrate_path(apps, schema_editor):
    CertificateAuthority = apps.get_model("django_ca", "CertificateAuthority")
    for ca in CertificateAuthority.objects.all():
        ca.key_backend_options = {"path": ca.private_key_path}
        ca.save()


def reverse_path(apps, schema_editor):
    CertificateAuthority = apps.get_model("django_ca", "CertificateAuthority")
    for ca in CertificateAuthority.objects.all():
        if ca.key_backend_path == "default" and "path" in ca.key_backend_options:
            ca.private_key_path = ca.key_backend_options["path"]
        else:
            raise ValueError(f"{ca.name}: CA does not use StoragesBackend, cannot revert this migration.")
        ca.save()


class Migration(migrations.Migration):
    dependencies = [
        ("django_ca", "0042_certificateauthority_key_backend_options_and_more"),
    ]

    operations = [
        migrations.RunPython(migrate_path, reverse_path),
    ]
