Flask-Large-Application-Example icon indicating copy to clipboard operation
Flask-Large-Application-Example copied to clipboard

It will be nice to have migration functionality.

Open rohitkeshwani07 opened this issue 9 years ago • 4 comments
trafficstars

How do you recommend setting migration with this structure?

rohitkeshwani07 avatar Nov 14 '16 13:11 rohitkeshwani07

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.

Robpol86 avatar Nov 14 '16 21:11 Robpol86

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

rohitkeshwani07 avatar Nov 15 '16 14:11 rohitkeshwani07

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()

rohitkeshwani07 avatar Nov 15 '16 14:11 rohitkeshwani07

manager.add_command('db',migrateCommand)

jstacoder avatar Dec 09 '16 08:12 jstacoder