"""update_sqlite_agent_v0.1.3_20260126

Revision ID: 031b34b4dd30
Revises: f458c7fb17a5
Create Date: 2026-01-26 20:55:22.420372

"""
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


# revision identifiers, used by Alembic.
revision: str = '031b34b4dd30'
down_revision: Union[str, Sequence[str], None] = 'f458c7fb17a5'
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! ###
    with op.batch_alter_table('plugin', schema=None) as batch_op:
        if not column_exists('plugin', 'desc_mk'):
            batch_op.add_column(sa.Column('desc_mk', sa.Text(), nullable=True))
        if not column_exists('plugin', 'inputs'):
            batch_op.add_column(sa.Column('inputs', sa.JSON(), nullable=True))

    with op.batch_alter_table('plugin_publish', schema=None) as batch_op:
        if not column_exists('plugin_publish', 'desc_mk'):
            batch_op.add_column(sa.Column('desc_mk', sa.Text(), nullable=True))
        if not column_exists('plugin_publish', 'inputs'):
            batch_op.add_column(sa.Column('inputs', sa.JSON(), nullable=True))

    # 设置现有 tool 记录的 available 字段为默认为 True
    with op.batch_alter_table('tool', schema=None) as batch_op:
        if not column_exists('tool', 'available'):
            batch_op.add_column(sa.Column('available', sa.Boolean(),
            nullable=False, server_default=sa.text('1')))

    # 设置现有 tool 记录的 available 字段为 True
    op.execute("UPDATE tool SET available = 1")

    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.alter_column('session_key',
               existing_type=sa.VARCHAR(length=256),
               type_=sa.String(length=512),
               existing_nullable=False)

    with op.batch_alter_table('workflow', schema=None) as batch_op:
        if not column_exists('workflow', 'object_key'):
            batch_op.add_column(sa.Column(
            'object_key', sa.String(length=512), nullable=True
            ))

    with op.batch_alter_table('workflow_publish', schema=None) as batch_op:
        if not column_exists('workflow_publish', 'object_key'):
            batch_op.add_column(sa.Column(
            'object_key', sa.String(length=512), nullable=True
            ))

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('workflow_publish', schema=None) as batch_op:
        if column_exists('workflow_publish', 'object_key'):
            batch_op.drop_column('object_key')

    with op.batch_alter_table('workflow', schema=None) as batch_op:
        if column_exists('workflow', 'object_key'):
            batch_op.drop_column('object_key')

    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.alter_column('session_key',
               existing_type=sa.String(length=512),
               type_=sa.VARCHAR(length=256),
               existing_nullable=False)

    with op.batch_alter_table('tool', schema=None) as batch_op:
        if column_exists('tool', 'available'):
            batch_op.drop_column('available')

    with op.batch_alter_table('plugin_publish', schema=None) as batch_op:
        if column_exists('plugin_publish', 'inputs'):
            batch_op.drop_column('inputs')
        if column_exists('plugin_publish', 'desc_mk'):
            batch_op.drop_column('desc_mk')

    with op.batch_alter_table('plugin', schema=None) as batch_op:
        if column_exists('plugin', 'inputs'):
            batch_op.drop_column('inputs')
        if column_exists('plugin', 'desc_mk'):
            batch_op.drop_column('desc_mk')

    # ### end Alembic commands ###