"""fix extend mysql text column size limit

Revision ID: 54be202e347e
Revises: 26f51e9528f1
Create Date: 2026-02-04 23:31:22.259889

"""
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 = '54be202e347e'
down_revision: Union[str, Sequence[str], None] = '26f51e9528f1'
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 column_exists('agent_execution', 'fail_reason'):
        op.alter_column('agent_execution', 'fail_reason',
                   existing_type=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   type_=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   existing_comment='the reason for failure',
                   existing_nullable=True)
    
    if not column_exists('agent_publish', 'model_id'):
        op.add_column('agent_publish', sa.Column('model_id', sa.Integer(), nullable=True))
    if not column_exists('agent_publish', 'agent_model_config'):
        op.add_column('agent_publish', sa.Column('agent_model_config', sa.JSON(), nullable=True))
    
    if column_exists('trace_detail', 'input'):
        op.alter_column('trace_detail', 'input',
                   existing_type=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   type_=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   existing_comment='输入数据',
                   existing_nullable=True)
    if column_exists('trace_detail', 'output'):
        op.alter_column('trace_detail', 'output',
                   existing_type=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   type_=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   existing_comment='输出数据',
                   existing_nullable=True)
    if column_exists('trace_summary', 'fail_reason'):
        op.alter_column('trace_summary', 'fail_reason',
                   existing_type=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   type_=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   existing_comment='失败原因',
                   existing_nullable=True)
    if column_exists('workflow_execution', 'fail_reason'):
        op.alter_column('workflow_execution', 'fail_reason',
                   existing_type=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   type_=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   existing_comment='the reason for failure',
                   existing_nullable=True)
    if column_exists('workflow_execution_details', 'meta_data'):
        op.alter_column('workflow_execution_details', 'meta_data',
                   existing_type=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   type_=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   existing_nullable=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    if column_exists('workflow_execution_details', 'meta_data'):
        op.alter_column('workflow_execution_details', 'meta_data',
                   existing_type=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   type_=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   existing_nullable=True)
    if column_exists('workflow_execution', 'fail_reason'):
        op.alter_column('workflow_execution', 'fail_reason',
                   existing_type=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   type_=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   existing_comment='the reason for failure',
                   existing_nullable=True)
    if column_exists('trace_summary', 'fail_reason'):
        op.alter_column('trace_summary', 'fail_reason',
                   existing_type=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   type_=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   existing_comment='失败原因',
                   existing_nullable=True)
    if column_exists('trace_detail', 'output'):
        op.alter_column('trace_detail', 'output',
                   existing_type=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   type_=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   existing_comment='输出数据',
                   existing_nullable=True)
    if column_exists('trace_detail', 'input'):
        op.alter_column('trace_detail', 'input',
                   existing_type=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   type_=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   existing_comment='输入数据',
                   existing_nullable=True)
    
    if column_exists('agent_publish', 'agent_model_config'):
        op.drop_column('agent_publish', 'agent_model_config')
    if column_exists('agent_publish', 'model_id'):
        op.drop_column('agent_publish', 'model_id')
    if column_exists('agent_execution', 'fail_reason'):
        op.alter_column('agent_execution', 'fail_reason',
                   existing_type=sa.Text().with_variant(mysql.LONGTEXT(), 'mysql'),
                   type_=mysql.TEXT(collation='utf8mb4_unicode_ci'),
                   existing_comment='the reason for failure',
                   existing_nullable=True)
    # ### end Alembic commands ###
