"""Keep one active collection registry row per path slug.

Revision ID: 0068_unique_active_collection_path_slug
Revises: 0067_add_v2_foundation_tables
Create Date: 2026-08-19 18:30:00.000000
"""

from __future__ import annotations

from alembic import op

revision = "0068_unique_active_collection_path_slug"
down_revision = "0067_add_v2_foundation_tables"
branch_labels = None
depends_on = None

INDEX_NAME = "uq_master_collection_registry_active_path_slug"


def upgrade() -> None:
    op.execute(
        f"""
        CREATE UNIQUE INDEX IF NOT EXISTS {INDEX_NAME}
        ON master_collection_registry (path_slug)
        WHERE is_active IS TRUE
          AND path_slug IS NOT NULL
          AND btrim(path_slug) <> ''
        """
    )


def downgrade() -> None:
    op.execute(f"DROP INDEX IF EXISTS {INDEX_NAME}")
