using flask-migrate with flask-script and application factory -
i'm building flask application , decided try application factory approach time, got trouble flask-migrate , can't figure out simple solution.
please note want pass config location option script
manage.py:
manager = manager(create_app) manager.add_option("-c", "--config", dest="config_module", required=false)
then need create migrate instance , add command manager:
with manager.app.app_context(): migrate = migrate(current_app, db) manager.add_command('db', migratecommand)
but app instance not created yet, fails
i know can pass config in environment variable , create application before creating manager instance, how using manager options?
when use --config
option have use application factory function know, function gets config argument , allows create app right configuration.
since have app factory, have use deferred initialization extensions. instantiate extensions without passing arguments, , in app factory function after create application, call init_app
on extensions, passing app , db have them.
the migratecommand
separate this, can add manager
without having app , db instances created.
example:
manage.py:
from app import create_app flask_migrate import migratecommand manager = manager(create_app) manager.add_option("-c", "--config", dest="config_module", required=false) manager.add_command('db', migratecommand)
app.py (or app factory module called)
from flask import flask flask_sqlalchemy import sqlalchemy flask_migrate import migrate db = sqlalchemy() migrate = migrate() def create_app(config): app = flask(__name__) # apply configuration # ... # initialize extensions db.init_app(app) migrate.init_app(app, db) return app
Comments
Post a Comment