"""reset migrations with new schema

Revision ID: e0c6eb21771f
Revises:
Create Date: 2019-06-19 15:17:59.205433

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

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


def upgrade():
    connection = op.get_bind()
    op.execute("""
    CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
    """)
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('attachments',
    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('filename', sa.String(), nullable=False),
    sa.Column('object_name', sa.String(), nullable=False),
    sa.Column('resource', sa.String(), nullable=True),
    sa.Column('resource_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('object_name')
    )
    op.create_index(op.f('ix_attachments_resource_id'), 'attachments', ['resource_id'], unique=False)
    op.create_table('notification_recipients',
    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('email', sa.String(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('permission_sets',
    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('name', sa.String(), nullable=False),
    sa.Column('display_name', sa.String(), nullable=False),
    sa.Column('description', sa.String(), nullable=False),
    sa.Column('permissions', postgresql.ARRAY(sa.String()), server_default='{}', nullable=False),
    sa.PrimaryKeyConstraint('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.create_table('portfolios',
    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('deleted', sa.Boolean(), server_default=sa.text('false'), nullable=False),
    sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False),
    sa.Column('name', sa.String(), nullable=True),
    sa.Column('defense_component', sa.String(), nullable=True),
    sa.Column('app_migration', sa.String(), nullable=True),
    sa.Column('complexity', sa.ARRAY(sa.String()), nullable=True),
    sa.Column('complexity_other', sa.String(), nullable=True),
    sa.Column('description', sa.String(), nullable=True),
    sa.Column('dev_team', sa.ARRAY(sa.String()), nullable=True),
    sa.Column('dev_team_other', sa.String(), nullable=True),
    sa.Column('native_apps', sa.String(), nullable=True),
    sa.Column('team_experience', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('users',
    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('username', sa.String(), nullable=True),
    sa.Column('email', sa.String(), nullable=True),
    sa.Column('dod_id', sa.String(), nullable=False),
    sa.Column('first_name', sa.String(), nullable=True),
    sa.Column('last_name', sa.String(), nullable=True),
    sa.Column('phone_number', sa.String(), nullable=True),
    sa.Column('phone_ext', sa.String(), nullable=True),
    sa.Column('service_branch', sa.String(), nullable=True),
    sa.Column('citizenship', sa.String(), nullable=True),
    sa.Column('designation', sa.String(), nullable=True),
    sa.Column('date_latest_training', sa.Date(), nullable=True),
    sa.Column('last_login', sa.TIMESTAMP(timezone=True), nullable=True),
    sa.Column('last_session_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('provisional', sa.Boolean(), nullable=True),
    sa.Column('cloud_id', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('dod_id')
    )
    op.create_table('applications',
    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('deleted', sa.Boolean(), server_default=sa.text('false'), nullable=False),
    sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.String(), nullable=False),
    sa.Column('portfolio_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.ForeignKeyConstraint(['portfolio_id'], ['portfolios.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('portfolio_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('portfolio_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('status', sa.Enum('ACTIVE', 'DISABLED', 'PENDING', name='status', native_enum=False), nullable=True),
    sa.ForeignKeyConstraint(['portfolio_id'], ['portfolios.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_portfolio_roles_portfolio_id'), 'portfolio_roles', ['portfolio_id'], unique=False)
    op.create_index(op.f('ix_portfolio_roles_user_id'), 'portfolio_roles', ['user_id'], unique=False)
    op.create_index('portfolio_role_user_portfolio', 'portfolio_roles', ['user_id', 'portfolio_id'], unique=True)
    op.create_table('task_orders',
    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('portfolio_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('pdf_attachment_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('number', sa.String(), nullable=True),
    sa.Column('signer_dod_id', sa.String(), nullable=True),
    sa.Column('signed_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['pdf_attachment_id'], ['attachments.id'], ),
    sa.ForeignKeyConstraint(['portfolio_id'], ['portfolios.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('users_permission_sets',
    sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('permission_set_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.ForeignKeyConstraint(['permission_set_id'], ['permission_sets.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], )
    )
    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('deleted', sa.Boolean(), server_default=sa.text('false'), 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=True),
    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('audit_events',
    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('user_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('portfolio_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('application_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('changed_state', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('event_details', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('resource_type', sa.String(), nullable=False),
    sa.Column('resource_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('display_name', sa.String(), nullable=True),
    sa.Column('action', sa.String(), nullable=False),
    sa.ForeignKeyConstraint(['application_id'], ['applications.id'], ),
    sa.ForeignKeyConstraint(['portfolio_id'], ['portfolios.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_audit_events_application_id'), 'audit_events', ['application_id'], unique=False)
    op.create_index(op.f('ix_audit_events_portfolio_id'), 'audit_events', ['portfolio_id'], unique=False)
    op.create_index(op.f('ix_audit_events_resource_id'), 'audit_events', ['resource_id'], unique=False)
    op.create_index(op.f('ix_audit_events_user_id'), 'audit_events', ['user_id'], unique=False)
    op.create_table('clins',
    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('task_order_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('number', sa.String(), nullable=True),
    sa.Column('loas', postgresql.ARRAY(sa.String()), server_default='{}', nullable=True),
    sa.Column('start_date', sa.Date(), nullable=True),
    sa.Column('end_date', sa.Date(), nullable=True),
    sa.Column('obligated_amount', sa.Numeric(scale=2), nullable=True),
    sa.Column('jedi_clin_type', sa.Enum('JEDI_CLIN_1', 'JEDI_CLIN_2', 'JEDI_CLIN_3', 'JEDI_CLIN_4', name='jediclintype', native_enum=False), nullable=True),
    sa.ForeignKeyConstraint(['task_order_id'], ['task_orders.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('environments',
    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('deleted', sa.Boolean(), server_default=sa.text('false'), nullable=False),
    sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('application_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('cloud_id', sa.String(), nullable=True),
    sa.ForeignKeyConstraint(['application_id'], ['applications.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('portfolio_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('dod_id', sa.String(), nullable=True),
    sa.Column('first_name', sa.String(), nullable=True),
    sa.Column('last_name', sa.String(), nullable=True),
    sa.Column('phone_number', sa.String(), nullable=True),
    sa.Column('portfolio_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(['inviter_id'], ['users.id'], ),
    sa.ForeignKeyConstraint(['portfolio_role_id'], ['portfolio_roles.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    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)
    op.create_table('portfolio_roles_permission_sets',
    sa.Column('portfolio_role_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('permission_set_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.ForeignKeyConstraint(['permission_set_id'], ['permission_sets.id'], ),
    sa.ForeignKeyConstraint(['portfolio_role_id'], ['portfolio_roles.id'], )
    )
    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('dod_id', sa.String(), nullable=True),
    sa.Column('first_name', sa.String(), nullable=True),
    sa.Column('last_name', sa.String(), nullable=True),
    sa.Column('phone_number', sa.String(), nullable=True),
    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.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_table('environment_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('deleted', sa.Boolean(), server_default=sa.text('false'), nullable=False),
    sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False),
    sa.Column('environment_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('role', sa.String(), nullable=True),
    sa.Column('application_role_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.ForeignKeyConstraint(['application_role_id'], ['application_roles.id'], ),
    sa.ForeignKeyConstraint(['environment_id'], ['environments.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('environments_role_user_environment', 'environment_roles', ['application_role_id', 'environment_id'], unique=True)
    connection = op.get_bind()
    connection.execute("""
CREATE OR REPLACE FUNCTION lock_dod_id()
RETURNS TRIGGER
AS $$
BEGIN
    IF NEW.dod_id != OLD.dod_id THEN
        RAISE EXCEPTION 'DOD ID cannot be updated';
    END IF;

    RETURN NEW;
END
$$ LANGUAGE plpgsql;

CREATE TRIGGER lock_dod_id
BEFORE UPDATE ON users
FOR EACH ROW
    EXECUTE PROCEDURE lock_dod_id();
    """)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('environments_role_user_environment', table_name='environment_roles')
    op.drop_table('environment_roles')
    op.drop_table('application_roles_permission_sets')
    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_table('portfolio_roles_permission_sets')
    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.drop_table('portfolio_invitations')
    op.drop_table('environments')
    op.drop_table('clins')
    op.drop_index(op.f('ix_audit_events_user_id'), table_name='audit_events')
    op.drop_index(op.f('ix_audit_events_resource_id'), table_name='audit_events')
    op.drop_index(op.f('ix_audit_events_portfolio_id'), table_name='audit_events')
    op.drop_index(op.f('ix_audit_events_application_id'), table_name='audit_events')
    op.drop_table('audit_events')
    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')
    op.drop_table('users_permission_sets')
    op.drop_table('task_orders')
    op.drop_index('portfolio_role_user_portfolio', table_name='portfolio_roles')
    op.drop_index(op.f('ix_portfolio_roles_user_id'), table_name='portfolio_roles')
    op.drop_index(op.f('ix_portfolio_roles_portfolio_id'), table_name='portfolio_roles')
    op.drop_table('portfolio_roles')
    op.drop_table('applications')
    op.drop_table('users')
    op.drop_table('portfolios')
    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('permission_sets')
    op.drop_table('notification_recipients')
    op.drop_index(op.f('ix_attachments_resource_id'), table_name='attachments')
    op.drop_table('attachments')
    connection = op.get_bind()
    connection.execute("""
DROP TRIGGER IF EXISTS lock_dod_id ON users;
DROP FUNCTION IF EXISTS lock_dod_id();
    """)
    # ### end Alembic commands ###
