"""simplify network

Revision ID: 1eb51c6159ac
Revises: 7d6099524c02
Create Date: 2017-12-29 11:36:35.323359

"""
from alembic import op
import sqlalchemy as sa
from hydra_base import db

import logging
log = logging.getLogger(__name__)

# revision identifiers, used by Alembic.
revision = '1eb51c6159ac'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###


    if op.get_bind().dialect.name == 'mysql':
        try:
            op.alter_column('tNetwork', 'network_id', new_column_name='id', existing_type=sa.Integer(), autoincrement=True, nullable=False)
            op.alter_column('tNetwork', 'network_name', new_column_name='name', existing_type=sa.String(200))
            op.alter_column('tNetwork', 'network_description', new_column_name='description', existing_type=sa.String(1000))
        except Exception as e:
            log.exception(e)
    else:
        try:
            op.drop_table('tNetwork_new')
        except:
            log.info("tNetwork_new does not exist")

        try:
            op.create_table(
                'tNetwork_new',
                sa.Column('id', sa.Integer, primary_key=True),
                sa.Column('name', sa.Text(200), nullable=False),
                sa.Column('description', sa.Text(1000)),
                sa.Column('layout', sa.Text(1000)),
                sa.Column('project_id', sa.Integer(), sa.ForeignKey('tProject.project_id'),  nullable=False),
                sa.Column('status', sa.String(1),  nullable=False, server_default=sa.text(u"'A'")),
                sa.Column('cr_date', sa.TIMESTAMP(),  nullable=False, server_default=sa.text(u'CURRENT_TIMESTAMP')),
                sa.Column('projection', sa.String(1000)),
                sa.Column('created_by', sa.Integer(), sa.ForeignKey('tUser.id')),
                sa.UniqueConstraint('name', 'project_id', name="unique net name")
            )

            op.execute("insert into tNetwork_new (id, name, description, layout, project_id, status, cr_date, projection, created_by) select network_id, network_name, network_description, layout, project_id, status, cr_date, projection, created_by from tNetwork")


            op.rename_table('tNetwork','tNetwork_old')
            op.rename_table('tNetwork_new', 'tNetwork')
            op.drop_table('tNetwork_old')
        except Exception as e:
            log.exception(e)

    # ### end Alembic commands ###


def downgrade():

    if op.get_bind().dialect.name == 'mysql':
        try:
            op.alter_column('tNetwork', 'network_id', new_column_name='id', existing_type=sa.Integer(), autoincrement=True, nullable=False)
            op.alter_column('tNetwork', 'network_name', new_column_name='name', existing_type=sa.String(200))
            op.alter_column('tNetwork', 'network_description', new_column_name='description', existing_type=sa.String(1000))
        except Exception as e:
            log.exception(e)
    else:
        try:
            op.drop_table('tNetwork_new')
        except:
            log.info("tNetwork_new does not exist")

        try:
            op.create_table(
                'tNetwork_new',
                sa.Column('network_id', sa.Integer, primary_key=True),
                sa.Column('network_name', sa.Text(200), nullable=False),
                sa.Column('network_description', sa.Text(1000)),
                sa.Column('layout', sa.Text(1000)),
                sa.Column('project_id', sa.Integer(), sa.ForeignKey('tProject.project_id'),  nullable=False),
                sa.Column('status', sa.String(1),  nullable=False, server_default=sa.text(u"'A'")),
                sa.Column('cr_date', sa.TIMESTAMP(),  nullable=False, server_default=sa.text(u'CURRENT_TIMESTAMP')),
                sa.Column('projection', sa.String(1000)),
                sa.Column('created_by', sa.Integer(), sa.ForeignKey('tUser.id')),
                sa.UniqueConstraint('name', 'project_id', name="unique net name")
            )

            op.execute("insert into tNetwork_new (network_id, network_name, network_description, layout, project_id, status, cr_date, projection, created_by) select id, name, description, layout, project_id, status, cr_date, projection, created_by from tNetwork")


            op.rename_table('tNetwork','tNetwork_old')
            op.rename_table('tNetwork_new', 'tNetwork')
            op.drop_table('tNetwork_old')
        except Exception as e:
            log.exception(e)
