"""Add magento_category_cache table.

Revision ID: 0017_add_magento_category_cache
Revises: 0016_add_magento_attribute_cache
Create Date: 2026-03-04

"""

from alembic import op
import sqlalchemy as sa


revision = "0017_add_magento_category_cache"
down_revision = "0016_add_magento_attribute_cache"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "magento_category_cache",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("connection_id", sa.Integer(), sa.ForeignKey("magento_connections.id"), nullable=False),
        sa.Column("magento_category_id", sa.Integer(), nullable=False),
        sa.Column("parent_id", sa.Integer(), nullable=True),
        sa.Column("name", sa.Text(), nullable=False),
        sa.Column("path", sa.Text(), nullable=False),
        sa.Column("path_names", sa.Text(), nullable=True),
        sa.Column("level", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("fetched_at", sa.DateTime(timezone=True), nullable=False),
        sa.UniqueConstraint("connection_id", "magento_category_id", name="uq_magento_category_cache_conn_catid"),
    )
    op.create_index(
        "ix_magento_category_cache_conn_path_names",
        "magento_category_cache",
        ["connection_id", "path_names"],
    )
    op.create_index(
        "ix_magento_category_cache_conn_name",
        "magento_category_cache",
        ["connection_id", "name"],
    )


def downgrade() -> None:
    op.drop_table("magento_category_cache")
