"""empty message

Revision ID: a374e36d0888
Revises: 4a6559da7594
Create Date: 2017-05-21 22:53:53.490856

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.orm import Session

from models import physical
from models import user


# revision identifiers, used by Alembic.
revision = '4_add_physical_users'
down_revision = '3_modify_user_fields'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('crm_physical',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('fam', sa.String(length=255), nullable=True),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('otch', sa.String(length=255), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('crm_users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('login', sa.String(length=255), nullable=False),
    sa.Column('password', sa.LargeBinary(), nullable=False),
    sa.Column('access_level', sa.Integer(), server_default='10', nullable=False),
    sa.Column('is_active', sa.Boolean(), server_default='f', nullable=False),
    sa.ForeignKeyConstraint(['id'], ['crm_physical.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('login')
    )
    op.add_column('crm_users', sa.Column('sip_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'crm_users', 'asterisk_sip_users', ['sip_id'], ['id'], ondelete='SET NULL')
    # ### end Alembic commands ###
    bind = op.get_bind()
    session = Session(bind)

    phys = physical.Physical("Администратор")
    phys.user = user.User("admin", "admin")
    phys.user.access_level = user.User.BOSS

    session.add(phys)
    session.commit()


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('crm_users')
    op.drop_table('crm_physical')
    # ### end Alembic commands ###
