# Generated by Django 4.2.23 on 2025-06-30 21:23

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('dab_rbac', '0007_remote_permissions_removals'),
    ]

    operations = [
        # After data migration this should be safe to make non-null again
        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.', on_delete=models.deletion.CASCADE, to='dab_rbac.dabcontenttype'),
        ),
        # Restore the unique constraint dropped for technical reasons
        migrations.AlterUniqueTogether(
            name='dabpermission',
            unique_together={('content_type', 'codename')},
        ),
        # Django does not pick this up automatically because postgres follows the field rename, and Django does not know
        migrations.AddConstraint(
            model_name='objectrole',
            constraint=models.UniqueConstraint(fields=('object_id', 'content_type', 'role_definition'), name='one_object_role_per_object_and_role'),
        ),
        # Same subtle postgres issue but with evaluation models
        migrations.AddConstraint(
            model_name='roleevaluation',
            constraint=models.UniqueConstraint(fields=('object_id', 'content_type_id', 'codename', 'role'), name='one_entry_per_object_permission_and_role'),
        ),
        migrations.AddConstraint(
            model_name='roleevaluationuuid',
            constraint=models.UniqueConstraint(fields=('object_id', 'content_type_id', 'codename', 'role'), name='one_entry_per_object_permission_and_role_uuid'),
        ),
        migrations.AlterField(
            model_name='dabpermission',
            name='content_type',
            field=models.ForeignKey(help_text='The content type this permission will apply to.', on_delete=models.deletion.CASCADE, to='dab_rbac.dabcontenttype', verbose_name='content type'),
        ),
        # Add back indexes for evaluations after new fields are in place
        # this is the thing that is only needed for sqlite3
        migrations.AddIndex(
            model_name='roleevaluation',
            index=models.Index(fields=['role', 'content_type_id', 'object_id'], name='dab_rbac_ro_role_id_604bc4_idx'),
        ),
        migrations.AddIndex(
            model_name='roleevaluation',
            index=models.Index(fields=['role', 'content_type_id', 'codename'], name='dab_rbac_ro_role_id_8b9faf_idx'),
        ),
        migrations.AddIndex(
            model_name='roleevaluationuuid',
            index=models.Index(fields=['role', 'content_type_id', 'object_id'], name='dab_rbac_ro_role_id_237936_idx'),
        ),
        migrations.AddIndex(
            model_name='roleevaluationuuid',
            index=models.Index(fields=['role', 'content_type_id', 'codename'], name='dab_rbac_ro_role_id_4fe905_idx'),
        ),
        # This is an additional, app-specific, constraint
        # goes beyond what the standard ContentType imposes
        migrations.AlterUniqueTogether(
            name='dabcontenttype',
            unique_together={('service', 'model')},
        ),
        # Add related names
        migrations.AlterField(
            model_name='dabcontenttype',
            name='parent_content_type',
            field=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, related_name='child_content_types', to='dab_rbac.dabcontenttype'),
        ),
        migrations.AlterField(
            model_name='dabpermission',
            name='content_type',
            field=models.ForeignKey(help_text='The content type this permission will apply to.', on_delete=models.deletion.CASCADE, related_name='dab_permissions', to='dab_rbac.dabcontenttype', verbose_name='content type'),
        ),
        # Add ordering on all models for consistent view pagination
        migrations.AlterModelOptions(
            name='dabcontenttype',
            options={'ordering': ['id']},
        ),
        migrations.AlterModelOptions(
            name='roleevaluation',
            options={'ordering': ['id'], 'verbose_name_plural': 'role_object_permissions'},
        ),
        migrations.AlterModelOptions(
            name='roleevaluationuuid',
            options={'ordering': ['id'], 'verbose_name_plural': 'role_object_permissions'},
        ),
    ]
