35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
from logging.config import fileConfig
|
|
from flask import current_app
|
|
from alembic import context
|
|
|
|
config = context.config
|
|
if config.config_file_name is not None:
|
|
try:
|
|
fileConfig(config.config_file_name)
|
|
except FileNotFoundError:
|
|
pass # alembic.ini absent or CWD mismatch — Flask-Migrate supplies logging elsewhere
|
|
|
|
target_metadata = current_app.extensions['migrate'].db.metadata
|
|
|
|
|
|
def run_migrations_offline():
|
|
url = config.get_main_option("sqlalchemy.url")
|
|
context.configure(url=url, target_metadata=target_metadata,
|
|
literal_binds=True, dialect_opts={"paramstyle": "named"})
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def run_migrations_online():
|
|
connectable = current_app.extensions['migrate'].db.engine
|
|
with connectable.connect() as connection:
|
|
context.configure(connection=connection, target_metadata=target_metadata)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|