"""Add split pot support

Revision ID: 1e77ac97ec6e
Revises: 2fc8308bab99
Create Date: 2019-08-18 06:25:27.862262

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


# revision identifiers, used by Alembic.
revision = '1e77ac97ec6e'
down_revision = '2fc8308bab99'
branch_labels = None
depends_on = None


def upgrade():
    # ### Added manually by Matt ###
    # Create enum class for use because Alembic won't do it automatically
    pot_state = postgresql.ENUM("VOID", "CLOSED", "OPEN", "SPLIT",
                                name="potstate")
    pot_state.create(op.get_bind())

    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('pots', sa.Column('parent_id', sa.Integer(), nullable=True))
    op.add_column('pots', sa.Column('state', sa.Enum('VOID', 'CLOSED', 'OPEN', 'SPLIT', name='potstate'), nullable=False))
    op.add_column('pots', sa.Column('winner_id', sa.Integer(), nullable=True))
    op.create_foreign_key('pots_parent_id_fkey', 'pots', 'pots', ['parent_id'], ['id'])
    op.create_foreign_key('pots_winner_id_fkey', 'pots', 'players', ['winner_id'], ['id'])
    op.create_check_constraint('pots_check', 'pots', '(state != \'CLOSED\' AND winner_id IS NULL) OR (state = \'CLOSED\' and winner_id IS NOT NULL)')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('pots_check', 'pots', type_='check')
    op.drop_constraint('pots_winner_id_fkey', 'pots', type_='foreignkey')
    op.drop_constraint('pots_parent_id_fkey', 'pots', type_='foreignkey')
    op.drop_column('pots', 'winner_id')
    op.drop_column('pots', 'state')
    op.drop_column('pots', 'parent_id')
    # ### end Alembic commands ###
