"""reporter_id

Revision ID: 8e52b5c7568a
Revises: fa97b0d0c351
Create Date: 2022-04-02 17:35:54.814007

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection

# revision identifiers, used by Alembic.
revision = '8e52b5c7568a'
down_revision = 'fa97b0d0c351'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    if not _table_has_column('bug_reports', 'reporter_id'):
        op.add_column(
            'bug_reports',
            sa.Column(
                'reporter_id',
                mysql.INTEGER(),
                server_default='0',
                nullable=False
            )
        )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('bug_reports', 'reporter_id')
    # ### end Alembic commands ###

def _table_has_column(table, column):
    config = op.get_context().config
    engine = engine_from_config(
        config.get_section(config.config_ini_section), prefix='sqlalchemy.')
    insp = reflection.Inspector.from_engine(engine)
    has_column = False
    for col in insp.get_columns(table):
        if column not in col['name']:
            continue
        has_column = True
    return has_column
