Home > postgresql > heroku – alembic

heroku – alembic

Problem:
Alembic migrations are not run automatically on heroku. On first install of the application, attempting to run $heroku run alembic upgrade head --app application-name results in an error as it appears that tables are build from the models that were defined in the application.

$ heroku run alembic history --app application-name
$ heroku run alembic downgrade -1 --app application-name fails with

INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
ERROR [alembic.util.messaging] Relative revision -1 didn't produce 1 migrations
FAILED: Relative revision -1 didn't produce 1 migrations

$ heroku run alembic downgrade 017e517ce319 --app application-name fails with
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
ERROR [alembic.util.messaging] Destination 017e517ce319 is not a valid downgrade target from current head(s)
FAILED: Destination 017e517ce319 is not a valid downgrade target from current head(s)

On checking the alembic version in the database
$ heroku pg:psql DATABASE_URL --app application-name
application-name::DATABASE=>select * from alembic_version;
version_num
-------------
(0 rows)

application-name::DATABASE=> update alembic_version set version_num = '017e517ce319';
UPDATE 0
application-name::DATABASE=> select * from alembic_version;
version_num
-------------
(0 rows)

Solution:
To start using alembic I needed to drop all tables and run alembic. Doing a dump of the db, running alembic, then reading back the dump should also work.

1. Clear all data
$ heroku run pg:reset DATABASE_URL --app application-name
2. Run alembic
3. Moving forward, always manually run alembic. (You could add this to your deploy step as well, depends on your needs.)
4. alembic downgrade,upgrade should now work.

Categories: postgresql Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.