# Generated by Django 4.2.24 on 2026-01-06 04:07
# Modified to safely handle permission tables and update Django state

from django.db import migrations


class Migration(migrations.Migration):
    """
    Remove guardian permission tables for DocumentRelationship.

    DocumentRelationship now uses inherited permissions from source_document +
    target_document + corpus (matching annotation Relationship model) rather
    than direct guardian permissions.

    Uses SeparateDatabaseAndState to:
    1. Drop tables via raw SQL (safe with IF EXISTS for envs where tables don't exist)
    2. Update Django's migration state to remove the model definitions
    """

    dependencies = [
        ("documents", "0028_add_corpus_index_to_documentrelationship"),
    ]

    operations = [
        # Handle the permission tables - drop from DB and remove from Django state
        migrations.SeparateDatabaseAndState(
            database_operations=[
                # Drop tables (may not exist in all envs - beta feature)
                migrations.RunSQL(
                    sql="DROP TABLE IF EXISTS documents_documentrelationshipuserobjectpermission CASCADE;",
                    reverse_sql=migrations.RunSQL.noop,
                ),
                migrations.RunSQL(
                    sql="DROP TABLE IF EXISTS documents_documentrelationshipgroupobjectpermission CASCADE;",
                    reverse_sql=migrations.RunSQL.noop,
                ),
            ],
            state_operations=[
                # Remove models from Django's state
                migrations.DeleteModel(
                    name="DocumentRelationshipGroupObjectPermission",
                ),
                migrations.DeleteModel(
                    name="DocumentRelationshipUserObjectPermission",
                ),
            ],
        ),
        # Update DocumentRelationship Meta options (remove permissions)
        migrations.AlterModelOptions(
            name="documentrelationship",
            options={},
        ),
    ]
