"""add application_invitation

Revision ID: 432c5287256d
Revises: 1880551a32e4
Create Date: 2019-04-23 09:23:05.738680

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

# revision identifiers, used by Alembic.
revision = '432c5287256d'
down_revision = '1880551a32e4'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.rename_table('invitations', 'portfolio_invitations')
    op.create_table('application_invitations',
        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('status', sa.Enum('ACCEPTED', 'REVOKED', 'PENDING', 'REJECTED_WRONG_USER', 'REJECTED_EXPIRED', name='status', native_enum=False), nullable=True),
        sa.Column('expiration_time', sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column('token', sa.String(), nullable=True),
        sa.Column('email', sa.String(), nullable=False),
        sa.Column('application_role_id', postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column('inviter_id', postgresql.UUID(as_uuid=True), nullable=True),
        sa.ForeignKeyConstraint(['application_role_id'], ['application_roles.id'], ),
        sa.ForeignKeyConstraint(['inviter_id'], ['users.id'], ),
        sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_application_invitations_application_role_id'), 'application_invitations', ['application_role_id'], unique=False)
    op.create_index(op.f('ix_application_invitations_inviter_id'), 'application_invitations', ['inviter_id'], unique=False)
    op.create_index(op.f('ix_application_invitations_token'), 'application_invitations', ['token'], unique=False)
    op.create_index(op.f('ix_application_invitations_user_id'), 'application_invitations', ['user_id'], unique=False)
    op.drop_index('ix_invitations_inviter_id', table_name='invitations')
    op.drop_index('ix_invitations_portfolio_role_id', table_name='invitations')
    op.drop_index('ix_invitations_token', table_name='invitations')
    op.drop_index('ix_invitations_user_id', table_name='invitations')
    op.create_index(op.f('ix_portfolio_invitations_inviter_id'), 'portfolio_invitations', ['inviter_id'], unique=False)
    op.create_index(op.f('ix_portfolio_invitations_portfolio_role_id'), 'portfolio_invitations', ['portfolio_role_id'], unique=False)
    op.create_index(op.f('ix_portfolio_invitations_token'), 'portfolio_invitations', ['token'], unique=False)
    op.create_index(op.f('ix_portfolio_invitations_user_id'), 'portfolio_invitations', ['user_id'], unique=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.rename_table('portfolio_invitations', 'invitations')
    op.drop_index(op.f('ix_application_invitations_user_id'), table_name='application_invitations')
    op.drop_index(op.f('ix_application_invitations_token'), table_name='application_invitations')
    op.drop_index(op.f('ix_application_invitations_inviter_id'), table_name='application_invitations')
    op.drop_index(op.f('ix_application_invitations_application_role_id'), table_name='application_invitations')
    op.drop_table('application_invitations')
    op.drop_index(op.f('ix_portfolio_invitations_user_id'), table_name='portfolio_invitations')
    op.drop_index(op.f('ix_portfolio_invitations_token'), table_name='portfolio_invitations')
    op.drop_index(op.f('ix_portfolio_invitations_portfolio_role_id'), table_name='portfolio_invitations')
    op.drop_index(op.f('ix_portfolio_invitations_inviter_id'), table_name='portfolio_invitations')
    op.create_index('ix_invitations_user_id', 'invitations', ['user_id'], unique=False)
    op.create_index('ix_invitations_token', 'invitations', ['token'], unique=False)
    op.create_index('ix_invitations_portfolio_role_id', 'invitations', ['portfolio_role_id'], unique=False)
    op.create_index('ix_invitations_inviter_id', 'invitations', ['inviter_id'], unique=False)
    # ### end Alembic commands ###
