"""Prepare the database for the new notifications system

Revision ID: 325a4d7c79d9
Revises: 4b7ef0c7bdb2
Create Date: 2014-07-31 13:08:18.667393

"""

# revision identifiers, used by Alembic.
revision = '325a4d7c79d9'
down_revision = '4b7ef0c7bdb2'

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

def upgrade(tables):
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('externalnotificationmethod',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('externalnotificationmethod_name', 'externalnotificationmethod', ['name'], unique=True)
    op.bulk_insert(tables.externalnotificationmethod,
    [
        {'id':1, 'name':'quay_notification'},
        {'id':2, 'name':'email'},
        {'id':3, 'name':'webhook'},
    ])
    op.create_table('externalnotificationevent',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('externalnotificationevent_name', 'externalnotificationevent', ['name'], unique=True)
    op.bulk_insert(tables.externalnotificationevent,
    [
        {'id':1, 'name':'repo_push'},
        {'id':2, 'name':'build_queued'},
        {'id':3, 'name':'build_start'},
        {'id':4, 'name':'build_success'},
        {'id':5, 'name':'build_failure'},
    ])
    op.create_table('repositoryauthorizedemail',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('repository_id', sa.Integer(), nullable=False),
    sa.Column('email', sa.String(length=255), nullable=False),
    sa.Column('code', sa.String(length=255), nullable=False),
    sa.Column('confirmed', sa.Boolean(), nullable=False),
    sa.ForeignKeyConstraint(['repository_id'], ['repository.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('repositoryauthorizedemail_code', 'repositoryauthorizedemail', ['code'], unique=True)
    op.create_index('repositoryauthorizedemail_email_repository_id', 'repositoryauthorizedemail', ['email', 'repository_id'], unique=True)
    op.create_index('repositoryauthorizedemail_repository_id', 'repositoryauthorizedemail', ['repository_id'], unique=False)
    op.create_table('repositorynotification',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('uuid', sa.String(length=255), nullable=False),
    sa.Column('repository_id', sa.Integer(), nullable=False),
    sa.Column('event_id', sa.Integer(), nullable=False),
    sa.Column('method_id', sa.Integer(), nullable=False),
    sa.Column('config_json', sa.Text(), nullable=False),
    sa.ForeignKeyConstraint(['event_id'], ['externalnotificationevent.id'], ),
    sa.ForeignKeyConstraint(['method_id'], ['externalnotificationmethod.id'], ),
    sa.ForeignKeyConstraint(['repository_id'], ['repository.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('repositorynotification_event_id', 'repositorynotification', ['event_id'], unique=False)
    op.create_index('repositorynotification_method_id', 'repositorynotification', ['method_id'], unique=False)
    op.create_index('repositorynotification_repository_id', 'repositorynotification', ['repository_id'], unique=False)
    op.create_index('repositorynotification_uuid', 'repositorynotification', ['uuid'], unique=False)
    op.add_column(u'notification', sa.Column('dismissed', sa.Boolean(), nullable=False))

    # Manually add the new notificationkind types
    op.bulk_insert(tables.notificationkind,
    [
        {'id':5, 'name':'repo_push'},
        {'id':6, 'name':'build_queued'},
        {'id':7, 'name':'build_start'},
        {'id':8, 'name':'build_success'},
        {'id':9, 'name':'build_failure'},
    ])

    # Manually add the new logentrykind types
    op.bulk_insert(tables.logentrykind,
    [
        {'id':39, 'name':'add_repo_notification'},
        {'id':40, 'name':'delete_repo_notification'},
    ])


    ### end Alembic commands ###


def downgrade(tables):
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_column(u'notification', 'dismissed')
    op.drop_table('repositorynotification')
    op.drop_table('repositoryauthorizedemail')
    op.drop_table('externalnotificationevent')
    op.drop_table('externalnotificationmethod')

    # Manually remove the notificationkind and logentrykind types
    op.execute(
        (tables.notificationkind.delete()
            .where(tables.notificationkind.c.name == op.inline_literal('repo_push')))

    )
    op.execute(
        (tables.notificationkind.delete()
            .where(tables.notificationkind.c.name == op.inline_literal('build_queued')))

    )
    op.execute(
        (tables.notificationkind.delete()
            .where(tables.notificationkind.c.name == op.inline_literal('build_start')))

    )
    op.execute(
        (tables.notificationkind.delete()
            .where(tables.notificationkind.c.name == op.inline_literal('build_success')))

    )
    op.execute(
        (tables.notificationkind.delete()
            .where(tables.notificationkind.c.name == op.inline_literal('build_failure')))

    )

    op.execute(
        (tables.logentrykind.delete()
            .where(tables.logentrykind.c.name == op.inline_literal('add_repo_notification')))

    )
    op.execute(
        (tables.logentrykind.delete()
            .where(tables.logentrykind.c.name == op.inline_literal('delete_repo_notification')))

    )
    ### end Alembic commands ###
