Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find a way to autogenerate migrations outside of app context #3

Open
reginafcompton opened this issue Jun 21, 2019 · 0 comments
Open

Comments

@reginafcompton
Copy link
Contributor

reginafcompton commented Jun 21, 2019

This repo operates outside of a current_app context. However, the alembic env script derives values from the app context, and thus, most basic alembic commands (e.g., autogenerate) cannot run without a corresponding app.

from flask import current_app
config.set_main_option('sqlalchemy.url',
current_app.config.get('SQLALCHEMY_DATABASE_URI'))
target_metadata = current_app.extensions['migrate'].db.metadata

The alembic docs suggest that the env.py file can be "hacked" or modified to meet your needs. Let's adjust the env.py file, so we can autogenerate migrations outside of an app context, something like:

# Helper variable to indicate that Alembic is operating outside of an app context.
is_offline = False
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from flask import current_app
try:
config.set_main_option('sqlalchemy.url',
current_app.config.get('SQLALCHEMY_DATABASE_URI'))
target_metadata = current_app.extensions['migrate'].db.metadata
except RuntimeError as e:
is_offline = True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant