"""property performance index

Revision ID: fa97b0d0c351
Revises: b470795db8e1
Create Date: 2022-03-31 10:38:06.367277

"""
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 = 'fa97b0d0c351'
down_revision = 'b470795db8e1'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    if not _table_has_column('properties', 'performance_cost'):
        op.add_column(
            'properties',
            sa.Column(
                'performance_cost',
                mysql.DOUBLE(
                    precision=20,
                    scale=15,
                    asdecimal=False
                ),
                server_default='0.0',
                nullable=True
            )
        )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('properties', 'performance_cost')
    # ### 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
