"""Add latency columns to rule results

Revision ID: 8dd0c9ed869c
Revises: d2e6156f6c12
Create Date: 2024-03-25 19:54:08.605036

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "8dd0c9ed869c"
down_revision = "d2e6156f6c12"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###

    # Make columns nullable at first to zer0 them out.
    op.add_column(
        "prompt_rule_results",
        sa.Column("latency_ms", sa.Integer(), nullable=True),
    )
    op.add_column(
        "response_rule_results",
        sa.Column("latency_ms", sa.Integer(), nullable=True),
    )

    op.execute("UPDATE prompt_rule_results SET latency_ms=0 WHERE latency_ms IS null")
    op.execute("UPDATE response_rule_results SET latency_ms=0 WHERE latency_ms IS null")

    op.alter_column(
        "prompt_rule_results",
        sa.Column("latency_ms", sa.Integer(), nullable=False),
    )
    op.alter_column(
        "response_rule_results",
        sa.Column("latency_ms", sa.Integer(), nullable=False),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("response_rule_results", "latency_ms")
    op.drop_column("prompt_rule_results", "latency_ms")
    # ### end Alembic commands ###
