
# Generated by Django 4.2.23 on 2025-09-26 21:03

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ("documents", "0019_add_pdf_file_hash"),
    ]

    operations = [
        # Index for DocumentUserObjectPermission table to speed up permission checks
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_doc_user_perm_content_user "
            "ON documents_documentuserobjectpermission(content_object_id, user_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_doc_user_perm_content_user;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_doc_user_perm_user_perm "
            "ON documents_documentuserobjectpermission(user_id, permission_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_doc_user_perm_user_perm;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_doc_user_perm_composite "
            "ON documents_documentuserobjectpermission(content_object_id, user_id, permission_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_doc_user_perm_composite;",
        ),
        # Index for Document table to optimize common filters
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_document_creator_public "
            "ON documents_document(creator_id, is_public);",
            reverse_sql="DROP INDEX IF EXISTS idx_document_creator_public;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_document_public "
            "ON documents_document(is_public) WHERE is_public = TRUE;",
            reverse_sql="DROP INDEX IF EXISTS idx_document_public;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_document_created_desc "
            "ON documents_document(created DESC);",
            reverse_sql="DROP INDEX IF EXISTS idx_document_created_desc;",
        ),
        # Index for corpus relationship (common filter) - ManyToMany table
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_corpus_documents "
            "ON corpuses_corpus_documents(document_id, corpus_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_corpus_documents;",
        ),
    ]
