"""add application_roles

Revision ID: 32438a35cfb5
Revises: 49e12ae7c9ca
Create Date: 2019-04-04 06:49:57.693753

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '32438a35cfb5'
down_revision = '49e12ae7c9ca'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('application_roles',
    sa.Column('time_created', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('time_updated', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False),
    sa.Column('application_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('status', sa.Enum('ACTIVE', 'DISABLED', 'PENDING', name='status', native_enum=False), nullable=True),
    sa.ForeignKeyConstraint(['application_id'], ['applications.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('application_role_user_application', 'application_roles', ['user_id', 'application_id'], unique=True)
    op.create_index(op.f('ix_application_roles_application_id'), 'application_roles', ['application_id'], unique=False)
    op.create_index(op.f('ix_application_roles_user_id'), 'application_roles', ['user_id'], unique=False)
    op.create_table('application_roles_permission_sets',
    sa.Column('application_role_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('permission_set_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.ForeignKeyConstraint(['application_role_id'], ['application_roles.id'], ),
    sa.ForeignKeyConstraint(['permission_set_id'], ['permission_sets.id'], )
    )
    op.create_index(op.f('ix_permission_sets_name'), 'permission_sets', ['name'], unique=True)
    op.create_index(op.f('ix_permission_sets_permissions'), 'permission_sets', ['permissions'], unique=False)
    op.drop_index('ix_roles_name', table_name='permission_sets')
    op.drop_index('ix_roles_permissions', table_name='permission_sets')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_index('ix_roles_permissions', 'permission_sets', ['permissions'], unique=False)
    op.create_index('ix_roles_name', 'permission_sets', ['name'], unique=True)
    op.drop_index(op.f('ix_permission_sets_permissions'), table_name='permission_sets')
    op.drop_index(op.f('ix_permission_sets_name'), table_name='permission_sets')
    op.drop_table('application_roles_permission_sets')
    op.drop_index(op.f('ix_application_roles_user_id'), table_name='application_roles')
    op.drop_index(op.f('ix_application_roles_application_id'), table_name='application_roles')
    op.drop_index('application_role_user_application', table_name='application_roles')
    op.drop_table('application_roles')
    # ### end Alembic commands ###
