How should I run alembic migrations on Heroku?

I too thought that I had to manually run alembic upgrade head on heroku for database changes to take place, but that is not the case. If you have the correct DATABASE_URL values in your application then alembic migrate is run on deploy and there is no need to run this again.

To confirm, you can connect to your database heroku pg:psql <database name> --app <application name> and check.


It turns out that Flask-Heroku is pulling the value of DATABASE_URL, which doesn't exist for my app on Heroku. If instead, I manually map the value of HEROKU_POSTGRESQL_CRIMSON_URL into app.config['SQLALCHEMY_DATABASE_URI'], it works as expected.

UPDATE: And it turns out that I forgot to pg:promote my DB so as to have a default for that app, which is why DATABASE_URL is non-existent. So the real solution is: don't forget to promote your DB.

UPDATE 2: Let me sum up: use flask-heroku to inject the proper SQLALCHEMY_DATABASE_URI into your app's config, tweak env.py to use your app's configured SQLALCHEMY_DATABASE_URI instead of the URL in alembic.ini, and then run alembic on Heroku's servers via heroku run alembic upgrade head (or whatever migration you want to run). This will prevent you from having to tweak the ini file to adjust for different environments (because the hosting environments will manage it for you).


You can specify a release phase task in your Procfile. Mine ended up looking like:

web: gunicorn ...
release: alembic upgrade head