"""add schemas for queuing and annotations

Revision ID: e85102f15a03
Revises: 397de36338cc
Create Date: 2017-03-03 22:43:53.686762

"""

# revision identifiers, used by Alembic.
revision = 'e85102f15a03'
down_revision = '397de36338cc'
branch_labels = None
depends_on = None

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

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('annotation_tasks',
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('id', sa.BigInteger(), nullable=False),
    sa.Column('name', sa.Text(), nullable=True),
    sa.Column('type', sa.Text(), nullable=True),
    sa.Column('topics', postgresql.JSON(), nullable=True),
    sa.Column('active_task_id', sa.BigInteger(), nullable=True),
    sa.Column('status', sa.Text(), nullable=True),
    sa.Column('config', postgresql.JSON(), nullable=True),
    sa.ForeignKeyConstraint(['active_task_id'], ['annotation_tasks.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.execute(CreateSequence(Sequence('annotation_task_id_seq')))
    op.create_index(op.f('ix_annotation_tasks_active_task_id'), 'annotation_tasks', ['active_task_id'], unique=False)
    op.create_index(op.f('ix_annotation_tasks_name'), 'annotation_tasks', ['name'], unique=False)
    op.create_index(op.f('ix_annotation_tasks_status'), 'annotation_tasks', ['status'], unique=False)
    op.create_index(op.f('ix_annotation_tasks_type'), 'annotation_tasks', ['type'], unique=False)
    op.create_table('annotation_jobs',
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('id', sa.BigInteger(), nullable=False),
    sa.Column('annotation_task_id', sa.BigInteger(), nullable=True),
    sa.Column('user_id', sa.BigInteger(), nullable=True),
    sa.Column('was_preassigned', sa.Boolean(), nullable=True),
    sa.Column('doc_id', sa.BigInteger(), nullable=True),
    sa.Column('priority', sa.Float(), nullable=True),
    sa.Column('type', sa.Text(), nullable=True),
    sa.Column('status', sa.Text(), nullable=True),
    sa.Column('completed_at', sa.DateTime(), nullable=True),
    sa.Column('assigned_at', sa.DateTime(), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('subject_annotation_id', sa.BigInteger(), nullable=True),
    sa.Column('subject_annotation_table', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['annotation_task_id'], ['annotation_tasks.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.execute(CreateSequence(Sequence('annotation_job_id_seq')))
    op.create_index(op.f('ix_annotation_jobs_annotation_task_id'), 'annotation_jobs', ['annotation_task_id'], unique=False)
    op.create_index(op.f('ix_annotation_jobs_assigned_at'), 'annotation_jobs', ['assigned_at'], unique=False)
    op.create_index(op.f('ix_annotation_jobs_completed_at'), 'annotation_jobs', ['completed_at'], unique=False)
    op.create_index(op.f('ix_annotation_jobs_doc_id'), 'annotation_jobs', ['doc_id'], unique=False)
    op.create_index(op.f('ix_annotation_jobs_priority'), 'annotation_jobs', ['priority'], unique=False)
    op.create_index(op.f('ix_annotation_jobs_status'), 'annotation_jobs', ['status'], unique=False)
    op.create_index(op.f('ix_annotation_jobs_subject_annotation_id'), 'annotation_jobs', ['subject_annotation_id'], unique=False)
    op.create_index(op.f('ix_annotation_jobs_subject_annotation_table'), 'annotation_jobs', ['subject_annotation_table'], unique=False)
    op.create_index(op.f('ix_annotation_jobs_type'), 'annotation_jobs', ['type'], unique=False)
    op.create_index(op.f('ix_annotation_jobs_user_id'), 'annotation_jobs', ['user_id'], unique=False)
    op.create_table('topic_annotations',
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('id', sa.BigInteger(), nullable=False),
    sa.Column('doc_id', sa.BigInteger(), nullable=True),
    sa.Column('annotation_job_id', sa.BigInteger(), nullable=True),
    sa.Column('annotation_task_id', sa.BigInteger(), nullable=True),
    sa.Column('user_id', sa.BigInteger(), nullable=True),
    sa.Column('is_positive', sa.Boolean(), nullable=True),
    sa.Column('display_style', sa.Text(), nullable=True),
    sa.Column('topic_name', sa.Text(), nullable=True),
    sa.Column('details', postgresql.JSON(), nullable=True),
    sa.ForeignKeyConstraint(['annotation_job_id'], ['annotation_jobs.id'], ),
    sa.ForeignKeyConstraint(['annotation_task_id'], ['annotation_tasks.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.execute(CreateSequence(Sequence('topic_annotation_id_seq')))
    op.create_index(op.f('ix_topic_annotations_annotation_job_id'), 'topic_annotations', ['annotation_job_id'], unique=False)
    op.create_index(op.f('ix_topic_annotations_annotation_task_id'), 'topic_annotations', ['annotation_task_id'], unique=False)
    op.create_index(op.f('ix_topic_annotations_doc_id'), 'topic_annotations', ['doc_id'], unique=False)
    op.create_index(op.f('ix_topic_annotations_is_positive'), 'topic_annotations', ['is_positive'], unique=False)
    op.create_index(op.f('ix_topic_annotations_topic_name'), 'topic_annotations', ['topic_name'], unique=False)
    op.create_index(op.f('ix_topic_annotations_user_id'), 'topic_annotations', ['user_id'], unique=False)
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_topic_annotations_user_id'), table_name='topic_annotations')
    op.drop_index(op.f('ix_topic_annotations_topic_name'), table_name='topic_annotations')
    op.drop_index(op.f('ix_topic_annotations_is_positive'), table_name='topic_annotations')
    op.drop_index(op.f('ix_topic_annotations_doc_id'), table_name='topic_annotations')
    op.drop_index(op.f('ix_topic_annotations_annotation_task_id'), table_name='topic_annotations')
    op.drop_index(op.f('ix_topic_annotations_annotation_job_id'), table_name='topic_annotations')
    op.drop_table('topic_annotations')
    op.drop_index(op.f('ix_annotation_jobs_user_id'), table_name='annotation_jobs')
    op.drop_index(op.f('ix_annotation_jobs_type'), table_name='annotation_jobs')
    op.drop_index(op.f('ix_annotation_jobs_subject_annotation_table'), table_name='annotation_jobs')
    op.drop_index(op.f('ix_annotation_jobs_subject_annotation_id'), table_name='annotation_jobs')
    op.drop_index(op.f('ix_annotation_jobs_status'), table_name='annotation_jobs')
    op.drop_index(op.f('ix_annotation_jobs_priority'), table_name='annotation_jobs')
    op.drop_index(op.f('ix_annotation_jobs_doc_id'), table_name='annotation_jobs')
    op.drop_index(op.f('ix_annotation_jobs_completed_at'), table_name='annotation_jobs')
    op.drop_index(op.f('ix_annotation_jobs_assigned_at'), table_name='annotation_jobs')
    op.drop_index(op.f('ix_annotation_jobs_annotation_task_id'), table_name='annotation_jobs')
    op.drop_table('annotation_jobs')
    op.drop_index(op.f('ix_annotation_tasks_type'), table_name='annotation_tasks')
    op.drop_index(op.f('ix_annotation_tasks_status'), table_name='annotation_tasks')
    op.drop_index(op.f('ix_annotation_tasks_name'), table_name='annotation_tasks')
    op.drop_index(op.f('ix_annotation_tasks_active_task_id'), table_name='annotation_tasks')
    op.drop_table('annotation_tasks')
    ### end Alembic commands ###
