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

Commands don't support alembic -x options #32

Closed
ntvakou opened this issue Jun 9, 2014 · 5 comments
Closed

Commands don't support alembic -x options #32

ntvakou opened this issue Jun 9, 2014 · 5 comments

Comments

@ntvakou
Copy link

ntvakou commented Jun 9, 2014

Hi, the current upgrade and downgrade commands don't support -x option, which is quite important for things like one off database migrations.

How do I extend command to support this? I can submit a patch if you're able to point me i the right direction

Is there a workaround to directly use alembic commands?

Thanks.

@miguelgrinberg
Copy link
Owner

You can use the alembic command directly. Have you tried that?

@ghost
Copy link

ghost commented May 15, 2015

I tried doing just as you said with the alembic command, specifically with this alembic -c ./migrations/alembic.ini upgrade head but it keeps on giving me a RuntimeError: working outside of application context most likely due to env.py#L18

I found an implementation where instead of using this:

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

they used this ...

app = create_app()
config.set_main_option("sqlalchemy.url", app.config["SQLALCHEMY_DATABASE_URI"])
target_metadata = db.metadata

haven't tried it personally, but the way i see it, if i use their implementation, i'll probably break the old one, perhaps you have a better solution ??

@miguelgrinberg
Copy link
Owner

The reason Flask-Migrate can use current_app is that Flask-Script installs a request context before it calls commands. None of that exists under Alembic, obviously.

You can use this logic to make it work for both Flask-Migrate and native Alembic:

try:
    app = current_app._get_current_object()
except RuntimeError:
    app = create_app()
config.set_main_option("sqlalchemy.url", app.config["SQLALCHEMY_DATABASE_URI"])
target_metadata = db.metadata

@ghost
Copy link

ghost commented May 15, 2015

Thanks for the great help, although i had to add some more code up top to make it work especially the importing of environment variables, ultimately i end up with the following code (for those that might be interested):

import os
import sys

...

# I honestly don't know what this thing do,
# but it let's you execute this file as if it's
# inside the app folder or something
sys.path.append(os.getcwd())

# look for the environment configuration file,
# if it exists, import them as environment variables
if os.path.exists('.env'):
    print('Importing environment from .env...')
    for line in open('.env'):
        var = line.strip().split('=')
        if len(var) == 2:
            os.environ[var[0]] = var[1]

...

from flask import current_app
from app import create_app, db

try:
    app = current_app._get_current_object()
except RuntimeError:
    app = create_app(os.getenv('FLASK_CONFIG') or 'default')

config.set_main_option("sqlalchemy.url", app.config["SQLALCHEMY_DATABASE_URI"])
target_metadata = db.metadata

the only flaw I can see here is with the importing of environment variable being executed twice when running inside the application context.

@miguelgrinberg
Copy link
Owner

You can move all that extra code that you added inside the except block, so that it only runs for the Alembic case.

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

No branches or pull requests

2 participants