"""empty message

Revision ID: roc_dingo_0028_modify_sbm_log_status_retrieved
Revises: roc_dingo_0027_modify_view_rpw_events_per_day
Create Date: 2023-09-22 08:50:28.193974

"""

from pathlib import Path

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

# revision identifiers, used by Alembic.
revision = "roc_dingo_0028_modify_sbm_log_status_retrieved"
down_revision = "roc_dingo_0027_modify_view_rpw_events_per_day"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column(
        "invalid_packet_log",
        "apid",
        existing_type=sa.SMALLINT(),
        type_=sa.INTEGER(),
        existing_nullable=True,
        schema="pipeline",
    )
    op.add_column(
        "sbm_log",
        sa.Column("retrieved", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        schema="pipeline",
    )
    ## Following commented command generated by Alembic does not work
    # op.alter_column('sbm_log', 'status',
    #           existing_type=postgresql.ENUM('Available', 'Requested', 'Downlinked', 'Deleted', 'Missed', 'Unknown', name='sbm_status_type'),
    #           type_=postgresql.JSONB(astext_type=sa.Text()),
    #           nullable=True,
    #           schema='pipeline')
    # This command works
    filename = "SQL/roc_dingo_0028__upgrade.sql"
    sql_file = Path(__file__).parent / filename
    with open(sql_file, "r") as sql_content:
        sql_cmd = sql_content.read()
        op.execute(sql_cmd)

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    ## Following commented command generated by Alembic does not work
    # op.alter_column('sbm_log', 'status',
    #           existing_type=postgresql.JSONB(astext_type=sa.Text()),
    #           type_=postgresql.ENUM('Available', 'Requested', 'Downlinked', 'Deleted', 'Missed', 'Unknown', name='sbm_status_type'),
    #           nullable=False,
    #           schema='pipeline')
    # This command works
    filename = "SQL/roc_dingo_0028__downgrade.sql"
    sql_file = Path(__file__).parent / filename
    with open(sql_file, "r") as sql_content:
        sql_cmd = sql_content.read()
        op.execute(sql_cmd)

    op.drop_column("sbm_log", "retrieved", schema="pipeline")
    op.alter_column(
        "invalid_packet_log",
        "apid",
        existing_type=sa.INTEGER(),
        type_=sa.SMALLINT(),
        existing_nullable=True,
        schema="pipeline",
    )
    # ### end Alembic commands ###
