# Generated by Django 4.2.23 on 2025-06-30 12:48

import ansible_base.rbac.models.content_type
import ansible_base.rbac.remote
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('dab_rbac', '0003_alter_dabpermission_codename_and_more'),
    ]

    operations = [
        migrations.CreateModel(
            name='DABContentType',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                (
                    'service',
                    models.CharField(
                        default=ansible_base.rbac.remote.get_local_resource_prefix,
                        help_text='service namespace to track what service this type is for. Can have a value of shared, which indicates it is synchronized.',
                        max_length=100,
                    ),
                ),
                (
                    'app_label',
                    models.CharField(
                        help_text='Django app that the model is in. This is an internal technical detail that does not affect API use.',
                        max_length=100
                    ),
                ),
                (
                    'model',
                    models.CharField(
                        help_text='Name of the type according to the Django ORM Meta model_name convention. Comes from the python class, but lowercase with no spaces.',
                        max_length=100,
                    ),
                ),
                (
                    'parent_content_type',
                    models.ForeignKey(
                        help_text='Parent model within the RBAC system. Being assigned to a role in objects of the parent model can confer permissions to child objects.',
                        null=True,
                        on_delete=models.deletion.SET_NULL,
                        to='dab_rbac.dabcontenttype'
                    )
                )
            ],
            options={
                'unique_together': {('service', 'app_label', 'model')},
            },
            managers=[
                ('objects', ansible_base.rbac.models.content_type.DABContentTypeManager()),
            ],
        ),
        # Related, supporting, schema addition for remote RBAC
        migrations.AddField(
            model_name='objectrole',
            name='parent_reference',
            field=models.TextField(blank=True, db_index=True, help_text='The ansible_id or object_id of the parent resource.'),
        ),
        # Add new fields with a temporary name
        migrations.AddField(
            model_name='dabpermission',
            name='new_content_type',
            field=models.ForeignKey(
                help_text='The content type this permission will apply to.',
                null=True,
                on_delete=models.deletion.CASCADE,
                to='dab_rbac.dabcontenttype',
                verbose_name='content type',
                db_constraint=False,
            ),
        ),
        migrations.AddField(
            model_name='objectrole',
            name='new_content_type',
            field=models.ForeignKey(
                help_text='The content type of the subject of permission assignments. Duplicated from related RoleDefinition.',
                null=True,
                on_delete=models.deletion.CASCADE,
                to='dab_rbac.dabcontenttype',
            ),
        ),
        migrations.AddField(
            model_name='roledefinition',
            name='new_content_type',
            field=models.ForeignKey(
                default=None,
                help_text='The type of resource this can apply to; used for validation and user assistance.',
                null=True,
                on_delete=models.deletion.CASCADE,
                to='dab_rbac.dabcontenttype',
            ),
        ),
        migrations.AddField(
            model_name='roleteamassignment',
            name='new_content_type',
            field=models.ForeignKey(
                help_text='The content type this applies to.', null=True, on_delete=models.deletion.CASCADE, to='dab_rbac.dabcontenttype'
            ),
        ),
        migrations.AddField(
            model_name='roleuserassignment',
            name='new_content_type',
            field=models.ForeignKey(
                help_text='The content type this applies to.', null=True, on_delete=models.deletion.CASCADE, to='dab_rbac.dabcontenttype'
            ),
        ),
        migrations.AddField(
            model_name='roleevaluation',
            name='new_content_type_id',
            field=models.PositiveIntegerField(default=1, help_text='The related content type id.'),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='roleevaluationuuid',
            name='new_content_type_id',
            field=models.PositiveIntegerField(default=1, help_text='The related content type id.'),
            preserve_default=False,
        ),
        # Temporary adjustments to constraints due to the field renaming happening in next migration
        # this is actually for subtle postgres behaviors that Django does not know about
        # constraints follow the field when it is renamed, but that is unknown to Django
        migrations.AlterUniqueTogether(
            name='dabpermission',
            unique_together={},
        ),
        migrations.RemoveConstraint(
            model_name='objectrole',
            name='one_object_role_per_object_and_role',
        ),
        migrations.RemoveConstraint(
            model_name='roleevaluation',
            name='one_entry_per_object_permission_and_role',
        ),
        migrations.RemoveConstraint(
            model_name='roleevaluationuuid',
            name='one_entry_per_object_permission_and_role_uuid',
        ),
        # These have to be removed for sqlite3, only
        # postgres is able to handle them
        # but we will delete all data in these tables anyway, so low risk
        migrations.RemoveIndex(
            model_name='roleevaluation',
            name='dab_rbac_ro_role_id_604bc4_idx',
        ),
        migrations.RemoveIndex(
            model_name='roleevaluation',
            name='dab_rbac_ro_role_id_8b9faf_idx',
        ),
        migrations.RemoveIndex(
            model_name='roleevaluationuuid',
            name='dab_rbac_ro_role_id_237936_idx',
        ),
        migrations.RemoveIndex(
            model_name='roleevaluationuuid',
            name='dab_rbac_ro_role_id_4fe905_idx',
        ),
        # Fields unique to DAB RBAC and not generally shared with ContentType or Permission
        migrations.AddField(
            model_name='dabcontenttype',
            name='api_slug',
            field=models.CharField(default='', help_text='String to use for references to this type from other models in the API.', max_length=201),
        ),
        migrations.AddField(
            model_name='dabcontenttype',
            name='pk_field_type',
            field=models.CharField(default='integer', help_text='Database field type of the primary key field of the model, relevant for interal logic tracking permissions.', max_length=100),
        ),
        migrations.AddField(
            model_name='dabpermission',
            name='api_slug',
            field=models.CharField(default='', help_text='String to use for references to this type from other models in the API.', max_length=201),
        ),
        # Make the content_type fields nullable so the reverse migrations are possible
        migrations.AlterField(
            model_name='dabpermission',
            name='content_type',
            field=models.ForeignKey(help_text='The content type this permission will apply to.', null=True, on_delete=models.deletion.CASCADE, to='contenttypes.contenttype', verbose_name='content type'),
        ),
        migrations.AlterField(
            model_name='objectrole',
            name='content_type',
            field=models.ForeignKey(help_text='The content type of the subject of permission assignments. Duplicated from related RoleDefinition.', null=True, on_delete=models.deletion.CASCADE, to='contenttypes.contenttype'),
        ),
    ]
