from alembic import context
from sqlalchemy import engine_from_config, pool

from aleph.model import db

config = context.config
config.set_main_option("script_location", ".")
target_metadata = db.metadata


def ignore_autogen(obj, name, type_, reflexted, compare_to):
    if type_ == "table" and name.startswith("tabular_"):
        return False
    return True


def run_migrations_offline():
    url = config.get_main_option("sqlalchemy.url")
    context.configure(url=url, include_object=ignore_autogen)

    with context.begin_transaction():
        context.run_migrations()


def run_migrations_online():
    engine = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix="sqlalchemy.",
        poolclass=pool.NullPool,
    )

    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata,
        include_object=ignore_autogen,
    )

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()


if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()
