"""add live menu_item_option_id to order_item_options

Revision ID: 1209c0afc71a
Revises: 4455bf565acf
Create Date: 2026-07-22 09:42:30.320532

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "1209c0afc71a"
down_revision: Union[str, None] = "4455bf565acf"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # NOTE: autogenerate also proposed op.drop_table('spatial_ref_sys')
    # (a PostGIS system table — dropping it would break PostGIS
    # entirely) and dropping refresh_tokens_token_hash_key (recurring,
    # pre-existing unique-index-vs-named-constraint drift unrelated to
    # this migration — same drift called out in 42828b3f2eea). Both
    # removed by hand. The menu_item_option_id -> menu_item_options
    # foreign key is named explicitly (autogenerate left it anonymous),
    # since an anonymous constraint can't be dropped by name in
    # downgrade(). Nullable, no backfill needed — every pre-existing
    # order_item_options row simply has no live option to point to
    # (see the OrderItemOption model docstring).
    op.add_column(
        "order_item_options", sa.Column("menu_item_option_id", sa.Uuid(), nullable=True)
    )
    op.create_foreign_key(
        "fk_order_item_options_menu_item_option_id_menu_item_options",
        "order_item_options",
        "menu_item_options",
        ["menu_item_option_id"],
        ["id"],
        ondelete="SET NULL",
    )


def downgrade() -> None:
    op.drop_constraint(
        "fk_order_item_options_menu_item_option_id_menu_item_options",
        "order_item_options",
        type_="foreignkey",
    )
    op.drop_column("order_item_options", "menu_item_option_id")
