"""ticket fire_at ready_at

Revision ID: 2caed45e3853
Revises: 2505d6f40bee
Create Date: 2019-06-07 11:22:05.459741

"""
from alembic import op
from sqlalchemy import (
    Column,
    Date,
    DateTime,
    Time,
)


# revision identifiers, used by Alembic.
revision = '2caed45e3853'
down_revision = '8dc8e0f71a2d'
branch_labels = None
depends_on = None

first_agent_version = '19.7.9'


def upgrade():
    connection = op.get_bind()
    try:
        connection.execute('PRAGMA foreign_keys = "0";')

        ticket_times = []
        for row in connection.execute('select id, ready_date, ready_time from tickets').fetchall():
            ticket_times.append(
                {
                    "id": '"{}"'.format(row["id"]),
                    "ready_at": (
                        '"{} {}"'.format(row["ready_date"], row["ready_time"])
                        if row["ready_date"] and row["ready_time"]
                        else "NULL"
                    ),
                }
            )

        held_ticket_times = []
        for row in connection.execute('select id, ready_date, ready_time from held_tickets').fetchall():
            held_ticket_times.append(
                {
                    "id": '"{}"'.format(row["id"]),
                    "ready_at": (
                        '"{} {}"'.format(row["ready_date"], row["ready_time"])
                        if row["ready_date"] and row["ready_time"]
                        else "NULL"
                    ),
                }
            )

        with op.batch_alter_table('tickets') as batch_op:
            batch_op.drop_column('ready_date')
            batch_op.drop_column('ready_time')
            batch_op.add_column(Column('fire_at', DateTime, nullable=True))
            batch_op.add_column(Column('ready_at', DateTime, nullable=True))

        with op.batch_alter_table('held_tickets') as batch_op:
            batch_op.drop_column('ready_date')
            batch_op.drop_column('ready_time')
            batch_op.add_column(Column('fire_at', DateTime, nullable=True))
            batch_op.add_column(Column('ready_at', DateTime, nullable=True))

        with connection.begin():
            for tt in ticket_times:
                connection.execute('UPDATE tickets SET ready_at = {ready_at} WHERE id = {id}'.format(**tt))

            for htt in held_ticket_times:
                connection.execute('UPDATE held_tickets SET ready_at = {ready_at} WHERE id = {id}'.format(**htt))

    finally:
        connection.execute('PRAGMA foreign_keys = "1";')


def downgrade():
    connection = op.get_bind()
    try:
        connection.execute('PRAGMA foreign_keys = "0";')

        ticket_times = []
        for row in connection.execute('select id, ready_at from tickets').fetchall():
            ticket_times.append(
                {
                    "id": '"{}"'.format(row["id"]),
                    "ready_date": (
                        '"{}"'.format(row["ready_at"].split()[0])
                        if row["ready_at"]
                        else "NULL"
                    ),
                    "ready_time": (
                        '"{}"'.format(row["ready_at"].split()[1])
                        if row["ready_at"]
                        else "NULL"
                    ),
                }
            )

        held_ticket_times = []
        for row in connection.execute('select id, ready_at from held_tickets').fetchall():
            held_ticket_times.append(
                {
                    "id": '"{}"'.format(row["id"]),
                    "ready_date": (
                        '"{}"'.format(row["ready_at"].split()[0])
                        if row["ready_at"]
                        else "NULL"
                    ),
                    "ready_time": (
                        '"{}"'.format(row["ready_at"].split()[1])
                        if row["ready_at"]
                        else "NULL"
                    ),
                }
            )

        with op.batch_alter_table('tickets') as batch_op:
            batch_op.drop_column('fire_at')
            batch_op.drop_column('ready_at')
            batch_op.add_column(Column('ready_date', Date, nullable=True))
            batch_op.add_column(Column('ready_time', Time, nullable=True))

        with op.batch_alter_table('held_tickets') as batch_op:
            batch_op.drop_column('fire_at')
            batch_op.drop_column('ready_at')
            batch_op.add_column(Column('ready_date', Date, nullable=True))
            batch_op.add_column(Column('ready_time', Time, nullable=True))

        with connection.begin():
            for tt in ticket_times:
                connection.execute('UPDATE tickets SET ready_date = {ready_date}, ready_time = {ready_time} WHERE id = {id}'.format(**tt))

            for htt in held_ticket_times:
                connection.execute('UPDATE held_tickets SET ready_date = {ready_date}, ready_time = {ready_time} WHERE id = {id}'.format(**htt))

    finally:
        connection.execute('PRAGMA foreign_keys = "1";')
