"""Use wire sizes instead of emittance at LI20.

Revision ID: 25d9a35ebe74
Revises: 403a72f75677
Create Date: 2015-03-23 10:22:07.181036

"""

# revision identifiers, used by Alembic.
revision = '25d9a35ebe74'
down_revision = '403a72f75677'

from alembic import op
import sqlalchemy as sa


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('shift_report', sa.Column('x_rms_li20', sa.Float(), nullable=True))
    op.add_column('shift_report', sa.Column('y_rms_li20', sa.Float(), nullable=True))
    drop_column_sqlite('shift_report', 'x_emittance_li20')
    drop_column_sqlite('shift_report', 'y_emittance_li20')
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('shift_report', sa.Column('y_emittance_li20', sa.FLOAT(), nullable=True))
    op.add_column('shift_report', sa.Column('x_emittance_li20', sa.FLOAT(), nullable=True))
    op.drop_column('shift_report', 'y_rms_li20')
    op.drop_column('shift_report', 'x_rms_li20')
    ### end Alembic commands ###

def drop_column_sqlite(tablename, columns):
    """ column dropping functionality for SQLite """
 
    # we need copy to make a deep copy of the column attributes
    from copy import copy
 
    # get the db engine and reflect database tables
    engine = op.get_bind()
    meta = sa.MetaData(bind=engine)
    meta.reflect()
 
    # create a select statement from the old table
    old_table = meta.tables[tablename]
    select = sa.sql.select([c for c in old_table.c if c.name not in columns])
 
    # get remaining columns without table attribute attached
    remaining_columns = [copy(c) for c in old_table.columns
            if c.name not in columns]
    for column in remaining_columns:
        column.table = None
 
    # create a temporary new table
    new_tablename = '{0}_new'.format(tablename)
    op.create_table(new_tablename, *remaining_columns)
    meta.reflect()
    new_table = meta.tables[new_tablename]
 
    # copy data from old table
    insert = sa.sql.insert(new_table).from_select(
            [c.name for c in remaining_columns], select)
    engine.execute(insert)
 
    # drop the old table and rename the new table to take the old tables
    # position
    op.drop_table(tablename)
    op.rename_table(new_tablename, tablename)
