"""202603051033

Revision ID: b4af97639217
Revises: 4bf27c66ae63
Create Date: 2026-03-05 10:36:06.282227

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'b4af97639217'
down_revision: Union[str, None] = '4bf27c66ae63'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Add columns as nullable first to avoid table locks
    op.add_column('model_api_keys', sa.Column('capability', sa.ARRAY(sa.String()), nullable=True, comment="模型能力列表（如['vision', 'audio', 'video']）"))
    op.add_column('model_api_keys', sa.Column('is_omni', sa.Boolean(), nullable=True, comment='是否为Omni模型（使用特殊API调用）'))
    
    op.add_column('model_bases', sa.Column('capability', sa.ARRAY(sa.String()), nullable=True, comment="模型能力列表（如['vision', 'audio', 'video']）"))
    op.add_column('model_bases', sa.Column('is_omni', sa.Boolean(), nullable=True, comment='是否为Omni模型（使用特殊API调用）'))
    
    op.add_column('model_configs', sa.Column('capability', sa.ARRAY(sa.String()), nullable=True, comment="模型能力列表（如['vision', 'audio', 'video']）"))
    op.add_column('model_configs', sa.Column('is_omni', sa.Boolean(), nullable=True, comment='是否为Omni模型（使用特殊API调用）'))
    
    # Update existing rows with default values
    op.execute("UPDATE model_api_keys SET capability = '{}' WHERE capability IS NULL")
    op.execute("UPDATE model_api_keys SET is_omni = false WHERE is_omni IS NULL")
    
    op.execute("UPDATE model_bases SET capability = '{}' WHERE capability IS NULL")
    op.execute("UPDATE model_bases SET is_omni = false WHERE is_omni IS NULL")
    
    op.execute("UPDATE model_configs SET capability = '{}' WHERE capability IS NULL")
    op.execute("UPDATE model_configs SET is_omni = false WHERE is_omni IS NULL")
    
    # Now make columns NOT NULL
    op.alter_column('model_api_keys', 'capability', nullable=False)
    op.alter_column('model_api_keys', 'is_omni', nullable=False)
    
    op.alter_column('model_bases', 'capability', nullable=False)
    op.alter_column('model_bases', 'is_omni', nullable=False)
    
    op.alter_column('model_configs', 'capability', nullable=False)
    op.alter_column('model_configs', 'is_omni', nullable=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('model_configs', 'is_omni')
    op.drop_column('model_configs', 'capability')
    op.drop_column('model_bases', 'is_omni')
    op.drop_column('model_bases', 'capability')
    op.drop_column('model_api_keys', 'is_omni')
    op.drop_column('model_api_keys', 'capability')
    # ### end Alembic commands ###
