"""Scope domain_term_dictionary uniqueness per node.

Replaces the global UNIQUE constraint on ``term_original`` with a composite
UNIQUE on ``(term_original, node_id)`` so the same term can exist in
different nodes of the hierarchy without conflict.

Revision ID: 4d5e6f7a8b9c
Revises: 3c4d5e6f7a8b
Create Date: 2026-04-17 10:00:00.000000

"""
from typing import Sequence, Union

from alembic import op


revision: str = "4d5e6f7a8b9c"
down_revision: Union[str, None] = "3c4d5e6f7a8b"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.execute(
        "ALTER TABLE domain_term_dictionary "
        "DROP CONSTRAINT IF EXISTS domain_term_dictionary_term_original_key"
    )
    op.create_unique_constraint(
        "uq_term_original_node",
        "domain_term_dictionary",
        ["term_original", "node_id"],
    )


def downgrade() -> None:
    op.execute(
        "ALTER TABLE domain_term_dictionary "
        "DROP CONSTRAINT IF EXISTS uq_term_original_node"
    )
    op.create_unique_constraint(
        "domain_term_dictionary_term_original_key",
        "domain_term_dictionary",
        ["term_original"],
    )
