Search code examples
gitgithubherokudatabase-migrationgithooks

Setup automatice database migration on Heroku after push


I've managed to setup my Heroku app to deploy automatically on a git push to Github. I see that Heroku itself offers Deploy Hooks, but those seem meant for integration with other services.

What I'd like to do is see if any changed files containing sql migrations were pushed, and if so, execute a simple bash command performing the latest migration on my Django app, for example in a git post-update hook.

How can I set this up on Heroku — I can't see any .git folder in my app?


Solution

  • Heroku's release phase is what you're looking for. One of its primary use cases is to run database migrations in conjunction with new releases. You can define a release process in your Procfile. For example, it might look something like this for a typical Rails app might be something like this:

    web: bundle exec puma -C config/puma.rb
    worker: bundle exec sidekiq
    release: bundle exec rake db:migrate
    

    Whereas you would want to swap in the appropriate migration command for your application in the release process.