"""add status_details column

Revision ID: add_status_details
Revises: add_instances_table
Create Date: 2024-03-19 11:00:00.000000

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import text


# revision identifiers, used by Alembic.
revision = 'add_status_details'
down_revision = 'add_instances_table'
branch_labels = None
depends_on = None


def upgrade():
    # Check if column exists before adding
    conn = op.get_bind()
    result = conn.execute(text("""
        SELECT EXISTS (
            SELECT FROM information_schema.columns 
            WHERE table_name = 'instances' AND column_name = 'status_details'
        );
    """))
    exists = result.scalar()
    
    if not exists:
        op.add_column('instances', sa.Column('status_details', sa.Text(), nullable=True))


def downgrade():
    op.drop_column('instances', 'status_details') 