# Generated by Django 5.2.7 on 2025-10-08 17:03

from django.apps.registry import Apps
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
import pgtrigger.compiler
import pgtrigger.migrations
from django.db import migrations, models


def remove_non_default_messages(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
    Task = apps.get_model("authentik_tasks", "task")

    db_alias = schema_editor.connection.alias
    Task.objects.using(db_alias).exclude(queue_name="default").delete()


class Migration(migrations.Migration):

    dependencies = [
        ("authentik_tasks", "0003_task_eta_task_retries"),
        ("authentik_tenants", "0006_tenant_flags"),
        ("contenttypes", "0002_remove_content_type_name"),
    ]

    operations = [
        pgtrigger.migrations.RemoveTrigger(
            model_name="task",
            name="notify_enqueueing",
        ),
        migrations.RemoveIndex(
            model_name="task",
            name="authentik_t_state_bb4a31_idx",
        ),
        migrations.AddIndex(
            model_name="task",
            index=models.Index(fields=["queue_name"], name="authentik_t_queue_n_7b09fb_idx"),
        ),
        migrations.AddIndex(
            model_name="task",
            index=models.Index(
                fields=["queue_name", "state"], name="authentik_t_queue_n_7ff882_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="task",
            index=models.Index(
                fields=["message_id", "queue_name", "state", "eta"],
                name="authentik_t_message_8ee59b_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="task",
            index=models.Index(
                fields=["message_id", "state", "eta"], name="authentik_t_message_74b8d7_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="task",
            index=models.Index(
                fields=["message_id", "queue_name", "state"], name="authentik_t_message_affe69_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="task",
            index=models.Index(
                fields=["state", "mtime", "result_expiry"], name="authentik_t_state_bef902_idx"
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="task",
            trigger=pgtrigger.compiler.Trigger(
                name="notify_enqueueing",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition='WHEN (NEW."eta" IS NULL AND NEW."state" = \'queued\')',
                    constraint="CONSTRAINT",
                    func="\n                    PERFORM pg_notify(\n                        'authentik.tasks.' || NEW.queue_name || '.enqueue',\n                        NEW.message_id::text\n                    );\n                    RETURN NEW;\n                ",
                    hash="da36b9a6344d384e5cf4de889a06bf578eaa10ef",
                    operation="INSERT OR UPDATE",
                    pgid="pgtrigger_notify_enqueueing_0bc94",
                    table="authentik_tasks_task",
                    timing="DEFERRABLE INITIALLY DEFERRED",
                    when="AFTER",
                ),
            ),
        ),
        migrations.RunPython(remove_non_default_messages),
    ]
