Blame alembic/env.py

Pierre-Yves Chibon a95dda
from __future__ import with_statement
Pierre-Yves Chibon a95dda
from alembic import context
Pierre-Yves Chibon a95dda
from sqlalchemy import engine_from_config, 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 a95dda
# add your model's MetaData object here
Pierre-Yves Chibon a95dda
# for 'autogenerate' support
Pierre-Yves Chibon a95dda
# from myapp import mymodel
Pierre-Yves Chibon a95dda
# target_metadata = mymodel.Base.metadata
Pierre-Yves Chibon a95dda
target_metadata = None
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 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 a95dda
    url = config.get_main_option("sqlalchemy.url")
Pierre-Yves Chibon a95dda
    context.configure(url=url, 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 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 a95dda
    engine = engine_from_config(
Pierre-Yves Chibon a95dda
                config.get_section(config.config_ini_section),
Pierre-Yves Chibon a95dda
                prefix='sqlalchemy.',
Pierre-Yves Chibon a95dda
                poolclass=pool.NullPool)
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
    connection = engine.connect()
Pierre-Yves Chibon a95dda
    context.configure(
Pierre-Yves Chibon a95dda
                connection=connection,
Pierre-Yves Chibon a95dda
                target_metadata=target_metadata
Pierre-Yves Chibon a95dda
                )
Pierre-Yves Chibon a95dda
Pierre-Yves Chibon a95dda
    try:
Pierre-Yves Chibon a95dda
        with context.begin_transaction():
Pierre-Yves Chibon a95dda
            context.run_migrations()
Pierre-Yves Chibon a95dda
    finally:
Pierre-Yves Chibon a95dda
        connection.close()
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()
Pierre-Yves Chibon a95dda