"""fix_sessions_schema_mismatch

Revision ID: f1a2b3c4d5e6
Revises: e51c9a16ee16
Create Date: 2025-12-10 10:30:00.000000

Fix schema mismatch between SQLAlchemy model and MySQL database:
1. Remove session_token column from tool_ocr_sessions (not in model)
2. Remove is_active column from tool_ocr_sessions (not in model)
3. Note: processing_track in tool_ocr_tasks is nullable so it won't cause issues

These columns were added to the database manually or by another migration
but are not present in the current SQLAlchemy models.
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql


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


def upgrade() -> None:
    """
    Remove columns from tool_ocr_sessions that exist in DB but not in model.
    This fixes the "Field 'session_token' doesn't have a default value" error.
    """
    # Remove session_token column (exists in DB, not in model)
    op.drop_column('tool_ocr_sessions', 'session_token')

    # Remove is_active column (exists in DB, not in model)
    op.drop_column('tool_ocr_sessions', 'is_active')


def downgrade() -> None:
    """
    Re-add the columns if needed to rollback.
    """
    # Re-add is_active column
    op.add_column('tool_ocr_sessions',
        sa.Column('is_active', mysql.TINYINT(), nullable=False, server_default='1',
                  comment='Whether the session is active'))

    # Re-add session_token column
    op.add_column('tool_ocr_sessions',
        sa.Column('session_token', sa.String(length=255), nullable=False,
                  comment='Session token for authentication'))
