"""OEM104 Create tables

Revision ID: 5ab66dd07312
Revises:
Create Date: 2020-02-19 16:42:26.558005

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from sqlalchemy.schema import Sequence, CreateSequence, DropSequence

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


def upgrade():
    # ### commands generated by Alembic ###
    op.execute(CreateSequence(Sequence('process_status_detail_status_id_seq')))

    op.create_table('process_status_detail',
    sa.Column('status_id', sa.BigInteger(), server_default=sa.text("nextval('process_status_detail_status_id_seq'::regclass)"), nullable=False),
    sa.Column('fwan_process_id', postgresql.UUID(), nullable=True),
    sa.Column('file_id', sa.Text(), nullable=True),
    sa.Column('component_id', sa.Text(), nullable=True),
    sa.Column('status', sa.Text(), nullable=True),
    sa.Column('status_date', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('status_id')
    )

    op.create_index('process_status_detail_idx', 'process_status_detail', ['fwan_process_id', 'file_id', 'component_id', 'status'], unique=False)

    op.create_table('error_detail',
    sa.Column('status_id', sa.BigInteger(), nullable=False),
    sa.Column('details', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.ForeignKeyConstraint(['status_id'], ['process_status_detail.status_id'], ),
    sa.PrimaryKeyConstraint('status_id')
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands generated by Alembic ###
    op.drop_table('error_detail')
    op.drop_index('process_status_detail_idx', table_name='process_status_detail')
    op.drop_table('process_status_detail')
    op.execute(DropSequence(Sequence('process_status_detail_status_id_seq')))
   # ### end Alembic commands ###
