"""Add timing metrics

Revision ID: ad9b81f99615
Revises: 95dad744de97
Create Date: 2017-10-10 14:40:42.445701

"""

# revision identifiers, used by Alembic.
revision = 'ad9b81f99615'
down_revision = '95dad744de97'

from alembic import op
import sqlalchemy as sa


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('timing',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=1024), nullable=False),
    sa.Column('total', sa.Float(), nullable=True),
    sa.Column('session_id', sa.Integer(), nullable=False),
    sa.Column('test_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['session_id'], ['session.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['test_id'], ['test.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_timing_session', 'timing', ['session_id', 'name'], unique=False)
    op.create_index('ix_timing_session_no_test', 'timing', ['session_id', 'name'], unique=True, postgresql_where=sa.text('test_id IS NULL'))
    op.create_index('ix_timing_test', 'timing', ['test_id', 'name'], unique=True, postgresql_where=sa.text('test_id IS NOT NULL'))
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_timing_test', table_name='timing')
    op.drop_index('ix_timing_session_no_test', table_name='timing')
    op.drop_index('ix_timing_session', table_name='timing')
    op.drop_table('timing')
    # ### end Alembic commands ###
