"""product_groups add parent_id

Revision ID: 014_product_groups_parent_id
Revises: 4952d846795f
Create Date: 2026-05-17
"""

from alembic import op
import sqlalchemy as sa

revision = "014_product_groups_parent_id"
down_revision = "4952d846795f"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "product_groups",
        sa.Column("parent_id", sa.Integer(), nullable=True, comment="顶级组为 NULL，子组指向父组"),
    )
    op.create_foreign_key(
        "fk_product_groups_parent_id",
        "product_groups",
        "product_groups",
        ["parent_id"],
        ["id"],
        ondelete="CASCADE",
    )
    op.create_index("ix_product_groups_parent_id", "product_groups", ["parent_id"])


def downgrade() -> None:
    op.drop_index("ix_product_groups_parent_id", table_name="product_groups")
    op.drop_constraint("fk_product_groups_parent_id", "product_groups", type_="foreignkey")
    op.drop_column("product_groups", "parent_id")
