"""Allow articles to be associated with a publisher.

Revision ID: a33edb7272a2
Revises: 21ec84874208
Create Date: 2021-10-22 20:10:50.440849

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'a33edb7272a2'
down_revision = '21ec84874208'
branch_labels = None
depends_on = None


from alembic import context
is_sqlite = context.config.get_main_option( "sqlalchemy.url" ).startswith( "sqlite://" )


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('article', sa.Column('publ_id', sa.Integer(), nullable=True))
    if is_sqlite:
        op.execute( "PRAGMA foreign_keys = off" ) # nb: stop cascading deletes
    with op.batch_alter_table('article') as batch_op:
        batch_op.create_foreign_key('fk_article_publisher', 'publisher', ['publ_id'], ['publ_id'], ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    if is_sqlite:
        op.execute( "PRAGMA foreign_keys = off" ) # nb: stop cascading deletes
    with op.batch_alter_table('article') as batch_op:
        batch_op.drop_constraint('fk_article_publisher', type_='foreignkey')
    op.drop_column('article', 'publ_id')
    # ### end Alembic commands ###
