1 from __future__ import with_statement
2 from alembic import context
3 from sqlalchemy import engine_from_config, pool
4 from logging.config import fileConfig
7 # this is the Alembic Config object, which provides
8 # access to the values within the .ini file in use.
9 config = context.config
11 # Interpret the config file for Python logging.
12 # This line sets up loggers basically.
13 fileConfig(config.config_file_name)
14 logger = logging.getLogger('alembic.env')
16 # add your model's MetaData object here
17 # for 'autogenerate' support
18 # from myapp import mymodel
19 # target_metadata = mymodel.Base.metadata
20 from flask import current_app
21 config.set_main_option('sqlalchemy.url',
22 current_app.config.get('SQLALCHEMY_DATABASE_URI'))
23 target_metadata = current_app.extensions['migrate'].db.metadata
25 # other values from the config, defined by the needs of env.py,
27 # my_important_option = config.get_main_option("my_important_option")
31 def run_migrations_offline():
32 """Run migrations in 'offline' mode.
34 This configures the context with just a URL
35 and not an Engine, though an Engine is acceptable
36 here as well. By skipping the Engine creation
37 we don't even need a DBAPI to be available.
39 Calls to context.execute() here emit the given string to the
43 url = config.get_main_option("sqlalchemy.url")
44 context.configure(url=url, compare_type=True)
46 with context.begin_transaction():
47 context.run_migrations()
50 def run_migrations_online():
51 """Run migrations in 'online' mode.
53 In this scenario we need to create an Engine
54 and associate a connection with the context.
58 # this callback is used to prevent an auto-migration from being generated
59 # when there are no changes to the schema
60 # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
61 def process_revision_directives(context, revision, directives):
62 if getattr(config.cmd_opts, 'autogenerate', False):
63 script = directives[0]
64 if script.upgrade_ops.is_empty():
66 logger.info('No changes in schema detected.')
68 engine = engine_from_config(config.get_section(config.config_ini_section),
70 poolclass=pool.NullPool)
72 connection = engine.connect()
73 context.configure(connection=connection,
74 target_metadata=target_metadata,
75 process_revision_directives=process_revision_directives,
77 **current_app.extensions['migrate'].configure_args)
80 with context.begin_transaction():
81 context.run_migrations()
85 if context.is_offline_mode():
86 run_migrations_offline()
88 run_migrations_online()