"""Add suit and rank to Card

Revision ID: 0d93e6c0e637
Revises: 22fe93e4e5a3
Create Date: 2019-08-25 12:36:23.019613

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base

from deuces import Card as PokerCard


# revision identifiers, used by Alembic.
revision = '0d93e6c0e637'
down_revision = '22fe93e4e5a3'
branch_labels = None
depends_on = None

Base = declarative_base()


class Card(Base):
    __tablename__ = 'cards'
    id = sa.Column(sa.Integer, primary_key=True)
    code = sa.Column(sa.Integer)
    rank = sa.Column(sa.String(length=1), nullable=True)
    suit = sa.Column(sa.String(length=1), nullable=True)


def upgrade():
    bind = op.get_bind()
    session = orm.Session(bind=bind)

    op.add_column('cards', sa.Column('rank', sa.String(length=1), nullable=True))
    op.add_column('cards', sa.Column('suit', sa.String(length=1), nullable=True))

    for card in session.query(Card):
        rank, suit = PokerCard.int_to_str(card.code)
        card.rank = rank
        card.suit = suit

    session.commit()

    op.alter_column('cards', 'rank', nullable=False)
    op.alter_column('cards', 'suit', nullable=False)


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('cards', 'suit')
    op.drop_column('cards', 'rank')
    # ### end Alembic commands ###
