"""add-is-draft-column-application-table

Revision ID: 524194732683
Revises: f581ec5971eb
Create Date: 2025-01-17 14:17:56.829145

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '524194732683'
down_revision = 'f581ec5971eb'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('application', sa.Column('is_draft', sa.Boolean(), nullable=True, server_default='false'))
    op.create_index(op.f('ix_application_is_draft'), 'application', ['is_draft'], unique=False)
    # Update is_draft column in application table to true for active draft applications
    update_query = sa.text("""UPDATE public.application
        SET is_draft = true
        WHERE id IN (SELECT application_id FROM public.draft WHERE status='1');""")
    # Execute the SQL statement
    op.execute(update_query)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_application_is_draft'), table_name='application')
    op.drop_column('application', 'is_draft')
    # ### end Alembic commands ###
