"""link bindings to access entities

Revision ID: f2d2359b923a
Revises: b32a4bf96447
Create Date: 2018-01-09 16:59:13.885801

"""

# revision identifiers, used by Alembic.
revision = 'f2d2359b923a'
down_revision = 'b32a4bf96447'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa
from sqlalchemy import FetchedValue
from netprofile.db import ddl as npd
from netprofile.db import fields as npf

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('netdev_bindings', sa.Column('aeid', npf.UInt32(), npd.Comment('Access entity ID'), server_default=sa.text('NULL'), nullable=True))
    op.add_column('netdev_bindings', sa.Column('cvlanid', npf.UInt16(), npd.Comment('Customer (inner) VLAN ID'), server_default=sa.text('NULL'), nullable=True))
    op.add_column('netdev_bindings', sa.Column('svlanid', npf.UInt16(), npd.Comment('Service provider (outer) VLAN ID'), server_default=sa.text('NULL'), nullable=True))
    op.create_index('netdev_bindings_i_aeid', 'netdev_bindings', ['aeid'], unique=False)
    op.create_index('netdev_bindings_i_qinq', 'netdev_bindings', ['svlanid', 'cvlanid'], unique=False)
    op.create_foreign_key('netdev_bindings_fk_aeid', 'netdev_bindings', 'entities_access', ['aeid'], ['entityid'], onupdate='CASCADE', ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('netdev_bindings_fk_aeid', 'netdev_bindings', type_='foreignkey')
    op.drop_index('netdev_bindings_i_qinq', table_name='netdev_bindings')
    op.drop_index('netdev_bindings_i_aeid', table_name='netdev_bindings')
    op.drop_column('netdev_bindings', 'svlanid')
    op.drop_column('netdev_bindings', 'cvlanid')
    op.drop_column('netdev_bindings', 'aeid')
    # ### end Alembic commands ###


Add DB migration for netdev bindings