"""add master_product_image.image_role

Revision ID: 0060_add_master_product_image_role
Revises: 0059_add_collection_commerce_landing_metadata
Create Date: 2026-07-21 19:10:00.000000
"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect


# revision identifiers, used by Alembic.
revision = "0060_add_master_product_image_role"
down_revision = "0059_add_collection_commerce_landing_metadata"
branch_labels = None
depends_on = None


def upgrade() -> None:
    bind = op.get_bind()
    inspector = inspect(bind)
    columns = {column["name"] for column in inspector.get_columns("master_product_image")}
    if "image_role" not in columns:
        op.add_column(
            "master_product_image",
            sa.Column("image_role", sa.String(length=64), nullable=True),
        )
        op.create_index(
            "ix_master_product_image_image_role",
            "master_product_image",
            ["image_role"],
            unique=False,
        )


def downgrade() -> None:
    bind = op.get_bind()
    inspector = inspect(bind)
    columns = {column["name"] for column in inspector.get_columns("master_product_image")}
    indexes = {index["name"] for index in inspector.get_indexes("master_product_image")}
    if "ix_master_product_image_image_role" in indexes:
        op.drop_index("ix_master_product_image_image_role", table_name="master_product_image")
    if "image_role" in columns:
        op.drop_column("master_product_image", "image_role")
