# Generated by Django 5.1.1 on 2024-11-29 08:31

import django.db.models.deletion
from django.db import migrations, models


def copy_root_object(apps, schema_editor):
    Uri = apps.get_model("apis_metainfo", "Uri")
    RootObject = apps.get_model("apis_metainfo", "RootObject")
    for uri in Uri.objects.all():
        if uri.root_object_id:
            root_object = RootObject.objects.get(pk=uri.root_object_id)
            uri.content_type_id = root_object.self_contenttype_id
            uri.object_id = root_object.id
            uri.save()


def reverse_copy_root_object(apps, schema_editor):
    Uri = apps.get_model("apis_metainfo", "Uri")
    for uri in Uri.objects.all():
        if uri.content_type_id and uri.object_id:
            uri.root_object_id = uri.object_id
            uri.save()


class Migration(migrations.Migration):
    dependencies = [
        ("apis_metainfo", "0014_remove_uri_domain_remove_uri_loaded_and_more"),
        ("contenttypes", "0002_remove_content_type_name"),
    ]

    operations = [
        migrations.AddField(
            model_name="uri",
            name="content_type",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                to="contenttypes.contenttype",
            ),
        ),
        migrations.AddField(
            model_name="uri",
            name="object_id",
            field=models.PositiveIntegerField(null=True),
        ),
        migrations.RunPython(copy_root_object, reverse_copy_root_object),
        migrations.RemoveField(model_name="uri", name="root_object"),
    ]
