"""dedupe attribute discovery

Revision ID: 0003_dedupe_attribute_discovery
Revises: 0002_drop_ingest_run_file_hash_unique
Create Date: 2026-01-24 00:05:00.000000
"""

from alembic import op


revision = "0003_dedupe_attribute_discovery"
down_revision = "0002_drop_ingest_run_file_hash_unique"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute(
        """
        DELETE FROM attribute_discovery a
        USING attribute_discovery b
        WHERE a.id > b.id
          AND a.attribute_code = b.attribute_code
          AND a.new_value = b.new_value;
        """
    )
    op.create_unique_constraint(
        "uq_attr_discovery_value",
        "attribute_discovery",
        ["attribute_code", "new_value"],
    )


def downgrade() -> None:
    op.drop_constraint("uq_attr_discovery_value", "attribute_discovery", type_="unique")
