"""Move defense component to Portfolio level

Revision ID: ec1ba2363191
Revises: fb22e47972a3
Create Date: 2019-02-22 14:43:49.408446

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import text


# revision identifiers, used by Alembic.
revision = 'ec1ba2363191'
down_revision = 'fb22e47972a3'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('portfolios', sa.Column('defense_component', sa.String(), nullable=True))
    conn = op.get_bind()
    conn.execute(
        text(
            """
                UPDATE portfolios
                SET defense_component = task_orders.defense_component
                FROM task_orders
                WHERE task_orders.portfolio_id = portfolios.id;
            """
        )
    )
    op.drop_column('task_orders', 'defense_component')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('task_orders', sa.Column('defense_component', sa.VARCHAR(), autoincrement=False, nullable=True))
    conn = op.get_bind()
    conn.execute(
        text(
            """
                UPDATE task_orders
                SET defense_component = portfolios.defense_component
                FROM portfolios;
            """
        )
    )
    op.drop_column('portfolios', 'defense_component')
    # ### end Alembic commands ###
