"""empty message

Revision ID: 0042_notification_history
Revises: 0041_email_template
Create Date: 2016-07-07 13:15:35.503107

"""

# revision identifiers, used by Alembic.
revision = '0042_notification_history'
down_revision = '0041_email_template'

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

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('notification_history',
        sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('job_id', postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column('job_row_number', sa.Integer(), nullable=True),
        sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column('template_id', postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column('template_version', sa.Integer(), nullable=False),
        sa.Column('api_key_id', postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column('key_type', sa.String(), nullable=False),
        sa.Column('content_char_count', sa.Integer(), nullable=True),
        sa.Column('notification_type', postgresql.ENUM('email', 'sms', 'letter', name='notification_type', create_type=False), nullable=False),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('sent_at', sa.DateTime(), nullable=True),
        sa.Column('sent_by', sa.String(), nullable=True),
        sa.Column('updated_at', sa.DateTime(), nullable=True),
        sa.Column('status', postgresql.ENUM('created', 'sending', 'delivered', 'pending', 'failed', 'technical-failure', 'temporary-failure', 'permanent-failure', name='notify_status_type', create_type=False), nullable=False),
        sa.Column('reference', sa.String(), nullable=True),
        sa.ForeignKeyConstraint(['api_key_id'], ['api_keys.id'], ),
        sa.ForeignKeyConstraint(['job_id'], ['jobs.id'], ),
        sa.ForeignKeyConstraint(['key_type'], ['key_types.name'], ),
        sa.ForeignKeyConstraint(['service_id'], ['services.id'], ),
        sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_notification_history_api_key_id'), 'notification_history', ['api_key_id'], unique=False)
    op.create_index(op.f('ix_notification_history_job_id'), 'notification_history', ['job_id'], unique=False)
    op.create_index(op.f('ix_notification_history_key_type'), 'notification_history', ['key_type'], unique=False)
    op.create_index(op.f('ix_notification_history_reference'), 'notification_history', ['reference'], unique=False)
    op.create_index(op.f('ix_notification_history_service_id'), 'notification_history', ['service_id'], unique=False)
    op.create_index(op.f('ix_notification_history_template_id'), 'notification_history', ['template_id'], unique=False)

    op.execute('''
    INSERT INTO notification_history
    (
      id,
      job_id,
      job_row_number,
      service_id,
      template_id,
      template_version,
      api_key_id,
      key_type,
      content_char_count,
      notification_type,
      created_at,
      sent_at,
      sent_by,
      updated_at,
      status,
      reference
    )
    SELECT
      id,
      job_id,
      job_row_number,
      service_id,
      template_id,
      template_version,
      api_key_id,
      key_type,
      content_char_count,
      notification_type,
      created_at,
      sent_at,
      sent_by,
      updated_at,
      status,
      reference
    FROM notifications
    ''')
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_notification_history_template_id'), table_name='notification_history')
    op.drop_index(op.f('ix_notification_history_service_id'), table_name='notification_history')
    op.drop_index(op.f('ix_notification_history_reference'), table_name='notification_history')
    op.drop_index(op.f('ix_notification_history_key_type'), table_name='notification_history')
    op.drop_index(op.f('ix_notification_history_job_id'), table_name='notification_history')
    op.drop_index(op.f('ix_notification_history_api_key_id'), table_name='notification_history')
    op.drop_table('notification_history')
    ### end Alembic commands ###
