"""torrent_distribution_support

Revision ID: c0236e351476
Revises: 9884324a48e3
Create Date: 2026-04-06 13:40:00.002888

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'c0236e351476'
down_revision = '9884324a48e3'
branch_labels = None
depends_on = None

def _column_exists(table, column):
    conn = op.get_bind()
    result = conn.execute(sa.text(f"PRAGMA table_info({table})"))
    return any(row[1] == column for row in result.fetchall())


def upgrade():
    if not _column_exists("mps_shop", "torrent_enabled"):
        op.add_column(
            "mps_shop",
            sa.Column("torrent_enabled", sa.Boolean(), nullable=True, server_default=sa.false()),
        )
    if not _column_exists("mps_product", "torrent_magnet_link"):
        op.add_column(
            "mps_product",
            sa.Column("torrent_magnet_link", sa.UnicodeText(), nullable=True),
        )
    if not _column_exists("mps_product", "torrent_file_url"):
        op.add_column(
            "mps_product",
            sa.Column("torrent_file_url", sa.UnicodeText(), nullable=True),
        )


def downgrade():
    if _column_exists("mps_shop", "torrent_enabled"):
        op.drop_column("mps_shop", "torrent_enabled")
    if _column_exists("mps_product", "torrent_magnet_link"):
        op.drop_column("mps_product", "torrent_magnet_link")
    if _column_exists("mps_product", "torrent_file_url"):
        op.drop_column("mps_product", "torrent_file_url")
