I am using python 3 and flask, with flask-migrate (which uses alembic) to handle my SQL migrations. When I run local integration tests, I want to rebuild the database each time so I can run my API calls against a clean db for each api call i'm testing (yes, i could use sqlite, but i want to check constraints are correct).
I can do the following on the command line easily:
mysql -uroot -e 'drop database DBNAME; create database DBNAME;'
FLASK_APP=flask_app.py flask db upgrade
But I would rather run it in the python code for 2 reasons:
- I don't want to have to worry about the mysql client being installed on the CI machines that will (eventually) run this code (they should just need to the python mysql packages).
- I want to manipulate the flask settings to force the database name to avoid accidents (so it needs to run in the same thread/memory space as the script which invokes it).
The app
object (created with app = Flask(__name__)
) has a cli
property, but it requires a context object, and it doesn't feel like i'm using the right tool. I expected app.cli.invoke('db', 'upgrade')
or similar...
Any suggestions on how to invoke flask commands from the code without a child cli process?