Flask-Large-Application-Example
Flask-Large-Application-Example copied to clipboard
It will be nice to have migration functionality.
How do you recommend setting migration with this structure?
I would probably put it in manage.py but I haven't had to implement migration in a Flask app using this template so I'm not too sure.
But you need to register the 'db' command like in this example
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128))
if __name__ == '__main__':
manager.run()
And If you do this in manage.py file inside a command function. It will not work. As the command you will fire will be. Python manage.py 'command/function name' db init
How I managed to do. I made a new file called migration.py with the below code. I don't know how to do this in manage.py.
import sys
import os
from personalization_engine.application import create_app, get_config
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from personalization_engine.extensions import db
def parse_options():
"""Parses command line options for Flask.
Returns:
Config instance to pass into create_app().
"""
# Figure out which class will be imported.
if os.environ.get('CONFIG') == "production":
config_class_string = 'personalization_engine.config.Production'
else:
config_class_string = 'personalization_engine.config.Config'
config_obj = get_config(config_class_string)
return config_obj
if __name__ == '__main__':
app = create_app(parse_options())
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
manager.run()
manager.add_command('db',migrateCommand)