"""Make activity revisable and remove validation status

Revision ID: <KEY>
Revises: <KEY>0
Create Date: 2020-03-02 11:38:27.175264

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "<KEY>"
down_revision = "<KEY>"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "activity", sa.Column("cancelled_at", sa.DateTime(), nullable=True)
    )
    op.add_column(
        "activity",
        sa.Column(
            "context",
            sa.Enum(
                "conflicting_with_history",
                "no_activity_switch",
                "driver_switch",
                "unauthorized_submitter",
                name="activitycontext",
                native_enum=False,
            ),
            nullable=True,
        ),
    )
    op.execute(
        """
        UPDATE activity SET context = 
            CASE WHEN validation_status in ('pending', 'rejected', 'validated') THEN null ELSE validation_status END
        """
    )
    op.add_column(
        "activity", sa.Column("revised_at", sa.DateTime(), nullable=True)
    )
    op.add_column(
        "activity", sa.Column("revised_by_id", sa.Integer(), nullable=True)
    )
    op.create_index(
        op.f("ix_activity_revised_by_id"),
        "activity",
        ["revised_by_id"],
        unique=False,
    )
    op.create_foreign_key(
        None, "activity", "activity", ["revised_by_id"], ["id"]
    )
    op.drop_column("activity", "validation_status")
    op.add_column(
        "comment",
        sa.Column(
            "context",
            sa.Enum(
                "unauthorized_submitter",
                name="eventbasecontext",
                native_enum=False,
            ),
            nullable=True,
        ),
    )
    op.execute(
        """
        UPDATE comment SET context = 
            CASE WHEN validation_status in ('pending', 'rejected', 'validated') THEN null ELSE validation_status END
        """
    )
    op.drop_column("comment", "validation_status")
    op.add_column(
        "expenditure",
        sa.Column(
            "context",
            sa.Enum(
                "unauthorized_submitter",
                name="eventbasecontext",
                native_enum=False,
            ),
            nullable=True,
        ),
    )
    op.execute(
        """
        UPDATE expenditure SET context = 
            CASE WHEN validation_status in ('pending', 'rejected', 'validated') THEN null ELSE validation_status END
        """
    )
    op.drop_column("expenditure", "validation_status")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "expenditure",
        sa.Column(
            "validation_status",
            sa.VARCHAR(length=22),
            autoincrement=False,
            nullable=False,
        ),
    )
    op.execute(
        """
        UPDATE expenditure SET validation_status = 
            CASE WHEN context is not null THEN context ELSE 'pending' END
        """
    )
    op.drop_column("expenditure", "context")
    op.add_column(
        "comment",
        sa.Column(
            "validation_status",
            sa.VARCHAR(length=22),
            autoincrement=False,
            nullable=False,
        ),
    )
    op.execute(
        """
        UPDATE comment SET validation_status = 
            CASE WHEN context is not null THEN context ELSE 'pending' END
        """
    )
    op.drop_column("comment", "context")
    op.add_column(
        "activity",
        sa.Column(
            "validation_status",
            sa.VARCHAR(length=24),
            autoincrement=False,
            nullable=False,
        ),
    )
    op.execute(
        """
        UPDATE activity SET validation_status = 
            CASE WHEN context is not null THEN context ELSE 'pending' END
        """
    )
    op.drop_constraint(None, "activity", type_="foreignkey")
    op.drop_index(op.f("ix_activity_revised_by_id"), table_name="activity")
    op.drop_column("activity", "revised_by_id")
    op.drop_column("activity", "revised_at")
    op.drop_column("activity", "context")
    op.drop_column("activity", "cancelled_at")
    # ### end Alembic commands ###

"""Make activity revisable and remove validation status

Revision ID: <KEY>
Revises: <KEY>0
Create Date: 2020-03-02 11:38:27.175264

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "<KEY>"
down_revision = "<KEY>"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "activity", sa.Column("cancelled_at", sa.DateTime(), nullable=True)
    )
    op.add_column(
        "activity",
        sa.Column(
            "context",
            sa.Enum(
                "conflicting_with_history",
                "no_activity_switch",
                "driver_switch",
                "unauthorized_submitter",
                name="activitycontext",
                native_enum=False,
            ),
            nullable=True,
        ),
    )
    op.execute(
        """
        UPDATE activity SET context = 
            CASE WHEN validation_status in ('pending', 'rejected', 'validated') THEN null ELSE validation_status END
        """
    )
    op.add_column(
        "activity", sa.Column("revised_at", sa.DateTime(), nullable=True)
    )
    op.add_column(
        "activity", sa.Column("revised_by_id", sa.Integer(), nullable=True)
    )
    op.create_index(
        op.f("ix_activity_revised_by_id"),
        "activity",
        ["revised_by_id"],
        unique=False,
    )
    op.create_foreign_key(
        None, "activity", "activity", ["revised_by_id"], ["id"]
    )
    op.drop_column("activity", "validation_status")
    op.add_column(
        "comment",
        sa.Column(
            "context",
            sa.Enum(
                "unauthorized_submitter",
                name="eventbasecontext",
                native_enum=False,
            ),
            nullable=True,
        ),
    )
    op.execute(
        """
        UPDATE comment SET context = 
            CASE WHEN validation_status in ('pending', 'rejected', 'validated') THEN null ELSE validation_status END
        """
    )
    op.drop_column("comment", "validation_status")
    op.add_column(
        "expenditure",
        sa.Column(
            "context",
            sa.Enum(
                "unauthorized_submitter",
                name="eventbasecontext",
                native_enum=False,
            ),
            nullable=True,
        ),
    )
    op.execute(
        """
        UPDATE expenditure SET context = 
            CASE WHEN validation_status in ('pending', 'rejected', 'validated') THEN null ELSE validation_status END
        """
    )
    op.drop_column("expenditure", "validation_status")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "expenditure",
        sa.Column(
            "validation_status",
            sa.VARCHAR(length=22),
            autoincrement=False,
            nullable=False,
        ),
    )
    op.execute(
        """
        UPDATE expenditure SET validation_status = 
            CASE WHEN context is not null THEN context ELSE 'pending' END
        """
    )
    op.drop_column("expenditure", "context")
    op.add_column(
        "comment",
        sa.Column(
            "validation_status",
            sa.VARCHAR(length=22),
            autoincrement=False,
            nullable=False,
        ),
    )
    op.execute(
        """
        UPDATE comment SET validation_status = 
            CASE WHEN context is not null THEN context ELSE 'pending' END
        """
    )
    op.drop_column("comment", "context")
    op.add_column(
        "activity",
        sa.Column(
            "validation_status",
            sa.VARCHAR(length=24),
            autoincrement=False,
            nullable=False,
        ),
    )
    op.execute(
        """
        UPDATE activity SET validation_status = 
            CASE WHEN context is not null THEN context ELSE 'pending' END
        """
    )
    op.drop_constraint(None, "activity", type_="foreignkey")
    op.drop_index(op.f("ix_activity_revised_by_id"), table_name="activity")
    op.drop_column("activity", "revised_by_id")
    op.drop_column("activity", "revised_at")
    op.drop_column("activity", "context")
    op.drop_column("activity", "cancelled_at")
    # ### end Alembic commands ###

Make activity revisable and remove validation status  Revision ID: <KEY> Revises: <KEY>0 Create Date: 2020-03-02 11:38:27.175264 # revision identifiers, used by Alembic. # ### commands auto generated by Alembic - please adjust! ### UPDATE activity SET context =              CASE WHEN validation_status in ('pending', 'rejected', 'validated') THEN null ELSE validation_status END UPDATE comment SET context =              CASE WHEN validation_status in ('pending', 'rejected', 'validated') THEN null ELSE validation_status END UPDATE expenditure SET context =              CASE WHEN validation_status in ('pending', 'rejected', 'validated') THEN null ELSE validation_status END # ### end Alembic commands ### # ### commands auto generated by Alembic - please adjust! ### UPDATE expenditure SET validation_status =              CASE WHEN context is not null THEN context ELSE 'pending' END UPDATE comment SET validation_status =              CASE WHEN context is not null THEN context ELSE 'pending' END UPDATE activity SET validation_status =              CASE WHEN context is not null THEN context ELSE 'pending' END # ### end Alembic commands ###