"""initial

Revision ID: 359caaf8c5f1
Revises:
Create Date: 2018-09-20 10:25:44.438679

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

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


def upgrade():
    connection = op.get_bind()
    connection.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.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('object_name')
    )
    op.create_table('pe_numbers',
    sa.Column('number', sa.String(), nullable=False),
    sa.Column('description', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('number')
    )
    op.create_table('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('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_roles_name'), 'roles', ['name'], unique=True)
    op.create_index(op.f('ix_roles_permissions'), 'roles', ['permissions'], unique=False)
    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('number', sa.String(), nullable=True),
    sa.Column('source', sa.Enum('MANUAL', 'EDA', name='source', native_enum=False), nullable=True),
    sa.Column('funding_type', sa.Enum('RDTE', 'OM', 'PROC', 'OTHER', name='fundingtype', native_enum=False), nullable=True),
    sa.Column('funding_type_other', sa.String(), nullable=True),
    sa.Column('clin_0001', sa.Integer(), nullable=True),
    sa.Column('clin_0003', sa.Integer(), nullable=True),
    sa.Column('clin_1001', sa.Integer(), nullable=True),
    sa.Column('clin_1003', sa.Integer(), nullable=True),
    sa.Column('clin_2001', sa.Integer(), nullable=True),
    sa.Column('clin_2003', sa.Integer(), nullable=True),
    sa.Column('expiration_date', sa.Date(), nullable=True),
    sa.Column('attachment_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.ForeignKeyConstraint(['attachment_id'], ['attachments.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('number')
    )
    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('atat_role_id', postgresql.UUID(as_uuid=True), 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.ForeignKeyConstraint(['atat_role_id'], ['roles.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('dod_id'),
    sa.UniqueConstraint('email')
    )
    op.create_table('request_reviews',
    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=False),
    sa.Column('comment', sa.String(), nullable=True),
    sa.Column('fname_mao', sa.String(), nullable=True),
    sa.Column('lname_mao', sa.String(), nullable=True),
    sa.Column('email_mao', sa.String(), nullable=True),
    sa.Column('phone_mao', sa.String(), nullable=True),
    sa.Column('fname_ccpo', sa.String(), nullable=True),
    sa.Column('lname_ccpo', sa.String(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('requests',
    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('time_created', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('task_order_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.ForeignKeyConstraint(['task_order_id'], ['task_orders.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('request_internal_comments',
    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('text', sa.String(), nullable=False),
    sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('request_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.ForeignKeyConstraint(['request_id'], ['requests.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('request_revisions',
    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('request_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('sequence', sa.BigInteger(), nullable=False),
    sa.Column('am_poc', sa.Boolean(), nullable=True),
    sa.Column('dodid_poc', sa.String(), nullable=True),
    sa.Column('email_poc', sa.String(), nullable=True),
    sa.Column('fname_poc', sa.String(), nullable=True),
    sa.Column('lname_poc', sa.String(), nullable=True),
    sa.Column('jedi_usage', sa.String(), nullable=True),
    sa.Column('start_date', sa.Date(), nullable=True),
    sa.Column('cloud_native', sa.String(), nullable=True),
    sa.Column('dollar_value', sa.Integer(), nullable=True),
    sa.Column('dod_component', sa.String(), nullable=True),
    sa.Column('data_transfers', sa.String(), nullable=True),
    sa.Column('expected_completion_date', sa.String(), nullable=True),
    sa.Column('jedi_migration', sa.String(), nullable=True),
    sa.Column('num_software_systems', sa.Integer(), nullable=True),
    sa.Column('number_user_sessions', sa.Integer(), nullable=True),
    sa.Column('average_daily_traffic', sa.Integer(), nullable=True),
    sa.Column('engineering_assessment', sa.String(), nullable=True),
    sa.Column('technical_support_team', sa.String(), nullable=True),
    sa.Column('estimated_monthly_spend', sa.Integer(), nullable=True),
    sa.Column('average_daily_traffic_gb', sa.Integer(), nullable=True),
    sa.Column('rationalization_software_systems', sa.String(), nullable=True),
    sa.Column('organization_providing_assistance', sa.String(), nullable=True),
    sa.Column('name', sa.String(), nullable=True),
    sa.Column('citizenship', sa.String(), nullable=True),
    sa.Column('designation', sa.String(), nullable=True),
    sa.Column('phone_number', sa.String(), nullable=True),
    sa.Column('email_request', sa.String(), nullable=True),
    sa.Column('fname_request', sa.String(), nullable=True),
    sa.Column('lname_request', sa.String(), nullable=True),
    sa.Column('service_branch', sa.String(), nullable=True),
    sa.Column('date_latest_training', sa.Date(), nullable=True),
    sa.Column('pe_id', sa.String(), nullable=True),
    sa.Column('task_order_number', sa.String(), nullable=True),
    sa.Column('fname_co', sa.String(), nullable=True),
    sa.Column('lname_co', sa.String(), nullable=True),
    sa.Column('email_co', sa.String(), nullable=True),
    sa.Column('office_co', sa.String(), nullable=True),
    sa.Column('fname_cor', sa.String(), nullable=True),
    sa.Column('lname_cor', sa.String(), nullable=True),
    sa.Column('email_cor', sa.String(), nullable=True),
    sa.Column('office_cor', sa.String(), nullable=True),
    sa.Column('uii_ids', postgresql.ARRAY(sa.String()), nullable=True),
    sa.Column('treasury_code', sa.String(), nullable=True),
    sa.Column('ba_code', sa.String(), nullable=True),
    sa.ForeignKeyConstraint(['request_id'], ['requests.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('workspaces',
    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=True),
    sa.Column('request_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.ForeignKeyConstraint(['request_id'], ['requests.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name')
    )
    op.create_table('projects',
    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('description', sa.String(), nullable=False),
    sa.Column('workspace_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.ForeignKeyConstraint(['workspace_id'], ['workspaces.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('request_status_events',
    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('new_status', sa.Enum('STARTED', 'SUBMITTED', 'PENDING_FINANCIAL_VERIFICATION', 'PENDING_CCPO_ACCEPTANCE', 'PENDING_CCPO_APPROVAL', 'CHANGES_REQUESTED', 'CHANGES_REQUESTED_TO_FINVER', 'APPROVED', 'EXPIRED', 'DELETED', name='requeststatus', native_enum=False), nullable=True),
    sa.Column('time_created', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.Column('request_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('sequence', sa.BigInteger(), nullable=False),
    sa.Column('request_revision_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('request_review_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.ForeignKeyConstraint(['request_id'], ['requests.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['request_review_id'], ['request_reviews.id'], ),
    sa.ForeignKeyConstraint(['request_revision_id'], ['request_revisions.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('workspace_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('workspace_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('role_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.ForeignKeyConstraint(['role_id'], ['roles.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.ForeignKeyConstraint(['workspace_id'], ['workspaces.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_workspace_roles_user_id'), 'workspace_roles', ['user_id'], unique=False)
    op.create_index(op.f('ix_workspace_roles_workspace_id'), 'workspace_roles', ['workspace_id'], unique=False)
    op.create_index('workspace_role_user_workspace', 'workspace_roles', ['user_id', 'workspace_id'], unique=True)
    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('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('project_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ),
    sa.PrimaryKeyConstraint('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('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('user_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.ForeignKeyConstraint(['environment_id'], ['environments.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('environments_role_user_environment', 'environment_roles', ['user_id', 'environment_id'], unique=True)
    # ### end Alembic commands ###

    connection.execute(
        "CREATE SEQUENCE request_status_events_sequence_seq OWNED BY request_status_events.sequence;"
    )
    connection.execute(
        "CREATE SEQUENCE request_revisions_sequence_seq OWNED BY request_revisions.sequence;"
    )


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('environments')
    op.drop_index('workspace_role_user_workspace', table_name='workspace_roles')
    op.drop_index(op.f('ix_workspace_roles_workspace_id'), table_name='workspace_roles')
    op.drop_index(op.f('ix_workspace_roles_user_id'), table_name='workspace_roles')
    op.drop_table('workspace_roles')
    op.drop_table('request_status_events')
    op.drop_table('projects')
    op.drop_table('workspaces')
    op.drop_table('request_revisions')
    op.drop_table('request_internal_comments')
    op.drop_table('requests')
    op.drop_table('request_reviews')
    op.drop_table('users')
    op.drop_table('task_orders')
    op.drop_index(op.f('ix_roles_permissions'), table_name='roles')
    op.drop_index(op.f('ix_roles_name'), table_name='roles')
    op.drop_table('roles')
    op.drop_table('pe_numbers')
    op.drop_table('attachments')
    # ### end Alembic commands ###

    connection = op.get_bind()
    connection.execute('DROP EXTENSION IF EXISTS "uuid-ossp"')
