"""add_unique_constraint_txn_literal

Adds a partial unique index on transactions.txn_literal WHERE txn_literal IS NOT NULL.

The index was previously applied manually to the database as:
  CREATE UNIQUE INDEX idx_transactions_txn_literal_unique
  ON transactions(txn_literal) WHERE txn_literal IS NOT NULL;

This migration records that constraint in Alembic's version history and uses
CREATE UNIQUE INDEX IF NOT EXISTS so it is safe to run against a database that
already has the index.

Revision ID: b2c3d4e5f6a7
Revises: a1b2c3d4e5f8
Create Date: 2026-03-04 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'b2c3d4e5f6a7'
down_revision: Union[str, Sequence[str], None] = 'a1b2c3d4e5f8'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Create the partial unique index only if it does not already exist.
    # Using IF NOT EXISTS makes this migration idempotent against databases
    # where the index was applied manually before this migration was written.
    op.execute(
        """
        CREATE UNIQUE INDEX IF NOT EXISTS idx_transactions_txn_literal_unique
        ON transactions(txn_literal)
        WHERE txn_literal IS NOT NULL
        """
    )


def downgrade() -> None:
    op.execute(
        "DROP INDEX IF EXISTS idx_transactions_txn_literal_unique"
    )
