5

I'm not quite sure how to migrate this simple old Flask-Script manage.py to the CLI provided in Flask 0.11+

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

from app import app, db

migrate = Migrate(app, db)
manager = Manager(app)

manager.add_command('db', MigrateCommand)


if __name__ == "__main__":
    manager.run()

It is used like this, with Flask Migrate:

python manage.py db migrate

Following the Flask 2.x documentation I can get as far as:

import click
from flask_migrate import Migrate, MigrateCommand

from app import app, db

@app.cli.command("db")
@click.argument("migration_command")
def handle_command(migration_command):
    print("Handling command {}".format(migration_command))

But when running:

python manage.py db migrate

But it appears handle_command is never called, and the db migrate command does not seem to run.

Also, what is the Flask CLI equivalent for:

if __name__ == "__main__":
    manager.run()

Since manager is no longer available? If I replace it with app.run(), it appears to ignore all the CLI parameters and the handle_command is never called.

Also, how about MigrateCommand in this:

manager.add_command('db', MigrateCommand)

I presume is creates an enumeration of available commands for db? Where do I pass MigrateCommand when using Flask CLI? The documentation for Flask Migrate is not really clear is it required or not when using Flask CLI.

1 Answer 1

1

I am still trying to get it working, but I can answer some of your queries:

if __name__ == "__main__":
    manager.run()

Here, manager.run() becomes cli()

if __name__ == "__main__":
    cli()

In commands like these:

python manage.py db migrate

replace python manage.py with flask everywhere, such as 'flask db migrate', etc.

manager.add_command('db', MigrateCommand)

Here, replace manager by cli. So, the above command becomes cli.add_command(...)

Not the answer you're looking for? Browse other questions tagged or ask your own question.