# pylint: disable=invalid-name
"""SampleSizeOptions table

Revision ID: cec7ecc73bd8
Revises: 496ee3db6da8
Create Date: 2022-04-25 22:57:54.528446+00:00

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "cec7ecc73bd8"
down_revision = "496ee3db6da8"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "sample_size_options",
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.Column("election_id", sa.String(length=200), nullable=False),
        sa.Column("round_num", sa.Integer(), nullable=False),
        sa.Column("task_id", sa.String(length=200), nullable=True),
        sa.Column("sample_size_options", sa.JSON(), nullable=True),
        sa.ForeignKeyConstraint(
            ["election_id"],
            ["election.id"],
            name=op.f("sample_size_options_election_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["task_id"],
            ["background_task.id"],
            name=op.f("sample_size_options_task_id_fkey"),
            ondelete="set null",
        ),
        sa.PrimaryKeyConstraint(
            "election_id", "round_num", name=op.f("sample_size_options_pkey")
        ),
    )

    op.execute(
        """
        INSERT INTO sample_size_options
            (created_at, updated_at, election_id, round_num, task_id, sample_size_options)
        SELECT created_at, updated_at, id, 1, sample_size_options_task_id, sample_size_options
        FROM election
        """
    )

    op.drop_constraint(
        "election_sample_size_options_task_id_fkey", "election", type_="foreignkey"
    )
    op.drop_column("election", "sample_size_options")
    op.drop_column("election", "sample_size_options_task_id")


def downgrade():  # pragma: no cover
    pass
