"""'Add referenced_recipe to ingredients'

Revision ID: 1d9a002d7234
Revises: e6bb583aac2d
Create Date: 2025-09-10 19:21:48.479101

"""

import sqlalchemy as sa
from alembic import op

import mealie.db.migration_types

# revision identifiers, used by Alembic.
revision = "1d9a002d7234"
down_revision: str | None = "e6bb583aac2d"
branch_labels: str | tuple[str, ...] | None = None
depends_on: str | tuple[str, ...] | None = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("recipes_ingredients", schema=None) as batch_op:
        batch_op.add_column(sa.Column("referenced_recipe_id", mealie.db.migration_types.GUID(), nullable=True))
        batch_op.create_index(
            batch_op.f("ix_recipes_ingredients_referenced_recipe_id"), ["referenced_recipe_id"], unique=False
        )
        batch_op.create_foreign_key("fk_recipe_subrecipe", "recipes", ["referenced_recipe_id"], ["id"])

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("recipes_ingredients", schema=None) as batch_op:
        batch_op.drop_constraint("fk_recipe_subrecipe", type_="foreignkey")
        batch_op.drop_index(batch_op.f("ix_recipes_ingredients_referenced_recipe_id"))
        batch_op.drop_column("referenced_recipe_id")

    # ### end Alembic commands ###
