Code Monkey home page Code Monkey logo

Comments (5)

miguelgrinberg avatar miguelgrinberg commented on May 22, 2024

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

from flask-migrate.

 avatar commented on May 22, 2024

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 ??

from flask-migrate.

miguelgrinberg avatar miguelgrinberg commented on May 22, 2024

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

from flask-migrate.

 avatar commented on May 22, 2024

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.

from flask-migrate.

miguelgrinberg avatar miguelgrinberg commented on May 22, 2024

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

from flask-migrate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.