"""v0 1 3 mysql agent update

Revision ID: 06a1f79bce8b
Revises: 54351e123cf0
Create Date: 2026-01-29 16:19:18.332351

"""
from typing import Sequence, Union

from alembic import op
from openjiuwen_studio.core.database.migration_utils import table_exists, column_exists, index_exists
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
from sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision: str = '06a1f79bce8b'
down_revision: Union[str, Sequence[str], None] = '54351e123cf0'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    if not column_exists('plugin', 'desc_mk'):
        op.add_column('plugin', sa.Column('desc_mk', sa.Text(), nullable=True))
    if not column_exists('plugin', 'inputs'):
        op.add_column('plugin', sa.Column('inputs', sa.JSON(), nullable=True))
    
    if not column_exists('plugin_publish', 'desc_mk'):
        op.add_column('plugin_publish', sa.Column('desc_mk', sa.Text(), nullable=True))
    if not column_exists('plugin_publish', 'inputs'):
        op.add_column('plugin_publish', sa.Column('inputs', sa.JSON(), nullable=True))
    
    if not column_exists('tool', 'available'):
        op.add_column('tool', sa.Column('available', sa.Boolean(), nullable=False))

    # 设置现有 tool 记录的 available 字段为 True
    if table_exists('tool') and column_exists('tool', 'available'):
        op.execute("UPDATE tool SET available = 1")

    if column_exists('user', 'session_key'):
        op.alter_column('user', 'session_key',
                   existing_type=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=256),
                   type_=sa.String(length=512),
                   existing_nullable=False)
    
    if not column_exists('workflow', 'object_key'):
        op.add_column('workflow', sa.Column('object_key', sa.String(length=512), nullable=True))
    if column_exists('workflow', 'schema'):
        op.alter_column('workflow', 'schema',
                   existing_type=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   type_=mysql.LONGTEXT(),
                   existing_nullable=True)
    
    if not column_exists('workflow_publish', 'object_key'):
        op.add_column('workflow_publish', sa.Column('object_key', sa.String(length=512), nullable=True))
    if column_exists('workflow_publish', 'schema'):
        op.alter_column('workflow_publish', 'schema',
                   existing_type=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   type_=mysql.LONGTEXT(),
                   existing_nullable=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    if column_exists('workflow_publish', 'schema'):
        op.alter_column('workflow_publish', 'schema',
                   existing_type=mysql.LONGTEXT(),
                   type_=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   existing_nullable=True)
    if column_exists('workflow_publish', 'object_key'):
        op.drop_column('workflow_publish', 'object_key')
    
    if column_exists('workflow', 'schema'):
        op.alter_column('workflow', 'schema',
                   existing_type=mysql.LONGTEXT(),
                   type_=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   existing_nullable=True)
    if column_exists('workflow', 'object_key'):
        op.drop_column('workflow', 'object_key')
    
    if column_exists('user', 'session_key'):
        op.alter_column('user', 'session_key',
                   existing_type=sa.String(length=512),
                   type_=mysql.VARCHAR(collation='utf8mb4_unicode_ci', length=256),
                   existing_nullable=False)
    
    if column_exists('tool', 'available'):
        op.drop_column('tool', 'available')
    
    if column_exists('plugin_publish', 'inputs'):
        op.drop_column('plugin_publish', 'inputs')
    if column_exists('plugin_publish', 'desc_mk'):
        op.drop_column('plugin_publish', 'desc_mk')
    
    if column_exists('plugin', 'inputs'):
        op.drop_column('plugin', 'inputs')
    if column_exists('plugin', 'desc_mk'):
        op.drop_column('plugin', 'desc_mk')
    # ### end Alembic commands ###
