"""drop_unique_constraint

Revision ID: 6b2c3d4e5f6g
Revises: 3e29c99b24ae
Create Date: 2026-01-28 16:40:00.000000

"""
from typing import Sequence, Union

from alembic import op
from sqlalchemy import text


# revision identifiers, used by Alembic.
revision: str = '6b2c3d4e5f6g'
down_revision: Union[str, None] = '3e29c99b24ae'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Drop unique constraint on extracted_records if it exists.

    The constraint was causing issues with DETAILED pipeline chunks
    that have the same (document_id, sheet_name, row_number) values.
    Removing it simplifies the data model.

    Uses raw SQL with IF EXISTS to safely drop constraints that may not exist.
    """
    # Use raw SQL with IF EXISTS for safe constraint dropping
    op.execute(text(
        "ALTER TABLE extracted_records "
        "DROP CONSTRAINT IF EXISTS uq_extracted_records_document_sheet_row"
    ))
    op.execute(text(
        "ALTER TABLE extracted_records "
        "DROP CONSTRAINT IF EXISTS uq_extracted_records_document_sheet_table_row"
    ))


def downgrade() -> None:
    """No downgrade - constraint removal is intentional."""
    pass
