"""Add more non nullables

Revision ID: 933bd7d807c4
Revises: d87cea4e995d
Create Date: 2017-05-04 11:44:16.146471

"""
from alembic import op
import sqlalchemy as sa
import flaskbb

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


def upgrade():
    connection = op.get_bind()

    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('conversations', schema=None) as batch_op:
        batch_op.alter_column('date_created',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=False)

    with op.batch_alter_table('forumsread', schema=None) as batch_op:
        batch_op.alter_column('last_read',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=False)

    # See https://github.com/sh4nks/flaskbb/issues/309
    if connection.engine.dialect.name == "mysql":
        op.execute('SET FOREIGN_KEY_CHECKS=0;')
    with op.batch_alter_table('groups_users', schema=None) as batch_op:
        batch_op.alter_column('group_id',
               existing_type=sa.INTEGER(),
               nullable=False)
        batch_op.alter_column('user_id',
               existing_type=sa.INTEGER(),
               nullable=False)
    # activate them again
    if connection.engine.dialect.name == "mysql":
        op.execute('SET FOREIGN_KEY_CHECKS=1;')

    with op.batch_alter_table('messages', schema=None) as batch_op:
        batch_op.alter_column('date_created',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=False)

    with op.batch_alter_table('posts', schema=None) as batch_op:
        batch_op.alter_column('date_created',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=False)

    with op.batch_alter_table('reports', schema=None) as batch_op:
        batch_op.alter_column('post_id',
               existing_type=sa.INTEGER(),
               nullable=True)
        batch_op.alter_column('reported',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=False)
        batch_op.alter_column('reporter_id',
               existing_type=sa.INTEGER(),
               nullable=True)

    with op.batch_alter_table('topics', schema=None) as batch_op:
        batch_op.alter_column('date_created',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=False)
        batch_op.alter_column('important',
               existing_type=sa.BOOLEAN(),
               nullable=False)
        batch_op.alter_column('last_updated',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=False)
        batch_op.alter_column('locked',
               existing_type=sa.BOOLEAN(),
               nullable=False)
        batch_op.alter_column('post_count',
               existing_type=sa.INTEGER(),
               nullable=False)
        batch_op.alter_column('views',
               existing_type=sa.INTEGER(),
               nullable=False)

    with op.batch_alter_table('topicsread', schema=None) as batch_op:
        batch_op.alter_column('last_read',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=False)

    with op.batch_alter_table('users', schema=None) as batch_op:
        batch_op.alter_column('activated',
               existing_type=sa.BOOLEAN(),
               nullable=False)
        batch_op.alter_column('date_joined',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=False)
        batch_op.alter_column('login_attempts',
               existing_type=sa.INTEGER(),
               nullable=False)

    # ### end Alembic commands ###


def downgrade():
    connection = op.get_bind()

    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('users', schema=None) as batch_op:
        batch_op.alter_column('login_attempts',
               existing_type=sa.INTEGER(),
               nullable=True)
        batch_op.alter_column('date_joined',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=True)
        batch_op.alter_column('activated',
               existing_type=sa.BOOLEAN(),
               nullable=True)

    with op.batch_alter_table('topicsread', schema=None) as batch_op:
        batch_op.alter_column('last_read',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=True)

    with op.batch_alter_table('topics', schema=None) as batch_op:
        batch_op.alter_column('views',
               existing_type=sa.INTEGER(),
               nullable=True)
        batch_op.alter_column('post_count',
               existing_type=sa.INTEGER(),
               nullable=True)
        batch_op.alter_column('locked',
               existing_type=sa.BOOLEAN(),
               nullable=True)
        batch_op.alter_column('last_updated',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=True)
        batch_op.alter_column('important',
               existing_type=sa.BOOLEAN(),
               nullable=True)
        batch_op.alter_column('date_created',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=True)

    with op.batch_alter_table('reports', schema=None) as batch_op:
        batch_op.alter_column('reporter_id',
               existing_type=sa.INTEGER(),
               nullable=False)
        batch_op.alter_column('reported',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=True)
        batch_op.alter_column('post_id',
               existing_type=sa.INTEGER(),
               nullable=False)

    with op.batch_alter_table('posts', schema=None) as batch_op:
        batch_op.alter_column('date_created',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=True)

    with op.batch_alter_table('messages', schema=None) as batch_op:
        batch_op.alter_column('date_created',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=True)

    if connection.engine.dialect.name == "mysql":
        op.execute('SET FOREIGN_KEY_CHECKS=0;')
    with op.batch_alter_table('groups_users', schema=None) as batch_op:
        batch_op.alter_column('user_id',
               existing_type=sa.INTEGER(),
               nullable=True)
        batch_op.alter_column('group_id',
               existing_type=sa.INTEGER(),
               nullable=True)
    if connection.engine.dialect.name == "mysql":
        op.execute('SET FOREIGN_KEY_CHECKS=1;')

    with op.batch_alter_table('forumsread', schema=None) as batch_op:
        batch_op.alter_column('last_read',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=True)

    with op.batch_alter_table('conversations', schema=None) as batch_op:
        batch_op.alter_column('date_created',
               existing_type=flaskbb.utils.database.UTCDateTime(timezone=True),
               nullable=True)

    # ### end Alembic commands ###
