"""Add date_modified to conversations

Revision ID: 881dd22cab94
Revises:
Create Date: 2017-06-30 09:17:14.436704

"""
from alembic import op
import sqlalchemy as sa
import flaskbb
from flaskbb.utils.helpers import time_utcnow

# revision identifiers, used by Alembic.
revision = '881dd22cab94'
down_revision = '933bd7d807c4'
branch_labels = ()
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('conversations', schema=None) as batch_op:
        batch_op.add_column(sa.Column('date_modified', flaskbb.utils.database.UTCDateTime(timezone=True), nullable=True))

    with op.batch_alter_table('conversations', schema=None) as batch_op:
        date_modified = sa.sql.table('conversations', sa.sql.column('date_modified'))
        batch_op.execute(date_modified.update().values(date_modified=time_utcnow()))
        batch_op.alter_column('date_modified',
            existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
            nullable=False
        )

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('conversations', schema=None) as batch_op:
        batch_op.drop_column('date_modified')

    # ### end Alembic commands ###
