87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
from __future__ import with_statement
|
|
|
|
from logging.config import fileConfig
|
|
|
|
from flask import current_app
|
|
|
|
from alembic import context
|
|
|
|
config = context.config
|
|
|
|
fileConfig(config.config_file_name)
|
|
logger = fileConfig
|
|
|
|
|
|
def get_engine():
|
|
try:
|
|
return current_app.extensions['migrate'].db.get_engine()
|
|
except (TypeError, AttributeError):
|
|
return current_app.extensions['migrate'].db.engine
|
|
|
|
|
|
def get_engine_url():
|
|
try:
|
|
return get_engine().url.render_as_string(hide_password=False).replace('%', '%%')
|
|
except AttributeError:
|
|
return str(get_engine().url).replace('%', '%%')
|
|
|
|
|
|
config.set_main_option('sqlalchemy.url', get_engine_url())
|
|
target_db = current_app.extensions['migrate'].db
|
|
|
|
|
|
def get_metadata():
|
|
if hasattr(target_db, 'metadatas'):
|
|
return target_db.metadatas[None]
|
|
return target_db.metadata
|
|
|
|
|
|
def run_migrations_offline():
|
|
url = config.get_main_option('sqlalchemy.url')
|
|
conf_args = dict(current_app.extensions['migrate'].configure_args)
|
|
conf_args.setdefault('compare_type', True)
|
|
conf_args.setdefault('render_as_batch', True)
|
|
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=get_metadata(),
|
|
literal_binds=True,
|
|
**conf_args,
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def run_migrations_online():
|
|
conf_args = dict(current_app.extensions['migrate'].configure_args)
|
|
if conf_args.get('process_revision_directives') is None:
|
|
def process_revision_directives(context_, revision, directives):
|
|
if getattr(config.cmd_opts, 'autogenerate', False):
|
|
script = directives[0]
|
|
if script.upgrade_ops.is_empty():
|
|
directives[:] = []
|
|
print('No changes in schema detected.')
|
|
conf_args['process_revision_directives'] = process_revision_directives
|
|
|
|
connectable = get_engine()
|
|
|
|
with connectable.connect() as connection:
|
|
conf_args.setdefault('compare_type', True)
|
|
conf_args.setdefault('render_as_batch', True)
|
|
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=get_metadata(),
|
|
**conf_args,
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|