"""Add position to newsletter link tables.

Revision ID: 40f652bd98a8
Revises: 7525fd3b67d5
Create Date: 2018-02-10 13:12:04.962735

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

# revision identifiers, used by Alembic.
revision = '40f652bd98a8'
down_revision = '7525fd3b67d5'


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute('SET FOREIGN_KEY_CHECKS = 0')
    op.add_column('newsletter_activities', sa.Column('position', sa.Integer(), nullable=False))
    op.alter_column('newsletter_activities', 'activity_id',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=False)
    op.alter_column('newsletter_activities', 'newsletter_id',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=False)
    op.add_column('newsletter_news', sa.Column('position', sa.Integer(), nullable=False))
    op.alter_column('newsletter_news', 'news_id',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=False)
    op.alter_column('newsletter_news', 'newsletter_id',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=False)
    op.drop_constraint('fk_newsletter_activities_newsletter_id_newsletter', 'newsletter_activities', type_='foreignkey')
    op.create_foreign_key(op.f('fk_newsletter_activities_newsletter_id_newsletter'), 'newsletter_activities', 'newsletter', ['newsletter_id'], ['id'], ondelete='cascade')
    op.drop_constraint('fk_newsletter_news_newsletter_id_newsletter', 'newsletter_news', type_='foreignkey')
    op.create_foreign_key(op.f('fk_newsletter_news_newsletter_id_newsletter'), 'newsletter_news', 'newsletter', ['newsletter_id'], ['id'], ondelete='cascade')
    op.alter_column('password_ticket', 'user_id',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=False)
    op.execute('SET FOREIGN_KEY_CHECKS = 1')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute('SET FOREIGN_KEY_CHECKS = 0')
    op.alter_column('newsletter_news', 'newsletter_id',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=True)
    op.alter_column('newsletter_news', 'news_id',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=True)
    op.drop_column('newsletter_news', 'position')
    op.alter_column('newsletter_activities', 'newsletter_id',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=True)
    op.alter_column('newsletter_activities', 'activity_id',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=True)
    op.drop_column('newsletter_activities', 'position')
    op.drop_constraint(op.f('fk_newsletter_news_newsletter_id_newsletter'), 'newsletter_news', type_='foreignkey')
    op.create_foreign_key('fk_newsletter_news_newsletter_id_newsletter', 'newsletter_news', 'newsletter', ['newsletter_id'], ['id'])
    op.drop_constraint(op.f('fk_newsletter_activities_newsletter_id_newsletter'), 'newsletter_activities', type_='foreignkey')
    op.create_foreign_key('fk_newsletter_activities_newsletter_id_newsletter', 'newsletter_activities', 'newsletter', ['newsletter_id'], ['id'])
    op.execute('SET FOREIGN_KEY_CHECKS = 1')
    # ### end Alembic commands ###
