Blame alembic/env.py

Pierre-Yves Chibon 67d1cc
from __future__ import with_statement, print_function, absolute_import
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon 4e3ce9
import os
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon a95dda
from alembic import context
Pierre-Yves Chibon 4e3ce9
from sqlalchemy import create_engine, pool
Pierre-Yves Chibon a95dda
from logging.config import fileConfig
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
# this is the Alembic Config object, which provides
Pierre-Yves Chibon a95dda
# access to the values within the .ini file in use.
Pierre-Yves Chibon a95dda
config = context.config
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
# Interpret the config file for Python logging.
Pierre-Yves Chibon a95dda
# This line sets up loggers basically.
Pierre-Yves Chibon a95dda
fileConfig(config.config_file_name)
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon 4e3ce9
if 'PAGURE_CONFIG' not in os.environ \
Pierre-Yves Chibon 4e3ce9
        and os.path.exists('/etc/pagure/pagure.cfg'):
Vadim Rutkovsky 9b6a62
    print('Using configuration file `/etc/pagure/pagure.cfg`')
Pierre-Yves Chibon 4e3ce9
    os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg'
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon 4e3ce9
try:
Pierre-Yves Chibon 4e3ce9
    import pagure
Pierre-Yves Chibon 4e3ce9
    import pagure.lib.model
Pierre-Yves Chibon 4e3ce9
except ImportError:
Pierre-Yves Chibon 4e3ce9
    import sys
Pierre-Yves Chibon 4e3ce9
    sys.path.insert(0, '.')
Pierre-Yves Chibon 4e3ce9
    import pagure
Pierre-Yves Chibon 4e3ce9
    import pagure.lib.model
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon a95dda
# add your model's MetaData object here
Pierre-Yves Chibon a95dda
# for 'autogenerate' support
Patrick Uiterwijk 19091b
target_metadata = pagure.lib.model.BASE.metadata
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
# other values from the config, defined by the needs of env.py,
Pierre-Yves Chibon a95dda
# can be acquired:
Pierre-Yves Chibon a95dda
# my_important_option = config.get_main_option("my_important_option")
Pierre-Yves Chibon a95dda
# ... etc.
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon 4e3ce9
DBURL = config.get_main_option("sqlalchemy.url")
Pierre-Yves Chibon 4e3ce9
if not DBURL:
Pierre-Yves Chibon b130e5
    DBURL = pagure.config.config['DB_URL']
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon a95dda
def run_migrations_offline():
Pierre-Yves Chibon a95dda
    """Run migrations in 'offline' mode.
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
    This configures the context with just a URL
Pierre-Yves Chibon a95dda
    and not an Engine, though an Engine is acceptable
Pierre-Yves Chibon a95dda
    here as well.  By skipping the Engine creation
Pierre-Yves Chibon a95dda
    we don't even need a DBAPI to be available.
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
    Calls to context.execute() here emit the given string to the
Pierre-Yves Chibon a95dda
    script output.
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
    """
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon 4e3ce9
    context.configure(url=DBURL, target_metadata=target_metadata)
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
    with context.begin_transaction():
Pierre-Yves Chibon a95dda
        context.run_migrations()
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon a95dda
def run_migrations_online():
Pierre-Yves Chibon a95dda
    """Run migrations in 'online' mode.
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
    In this scenario we need to create an Engine
Pierre-Yves Chibon a95dda
    and associate a connection with the context.
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
    """
Pierre-Yves Chibon 4e3ce9
    connectable = create_engine(DBURL, poolclass=pool.NullPool)
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon 4e3ce9
    with connectable.connect() as connection:
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon 4e3ce9
        context.configure(
Pierre-Yves Chibon 4e3ce9
            connection=connection,
Pierre-Yves Chibon 4e3ce9
            target_metadata=target_metadata
Pierre-Yves Chibon 4e3ce9
        )
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon a95dda
        with context.begin_transaction():
Pierre-Yves Chibon a95dda
            context.run_migrations()
Pierre-Yves Chibon 4e3ce9
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
if context.is_offline_mode():
Pierre-Yves Chibon a95dda
    run_migrations_offline()
Pierre-Yves Chibon a95dda
else:
Pierre-Yves Chibon a95dda
    run_migrations_online()