As you know Heroku **does not ** run rake db:migrate automatically.
Most of the time we all face this issue of not running migrations on Heroku post deployment. Either we forget or for some reason we miss it. However there are some gems/plugins that help us overcome these issues.

Even when we use [heroku auto deployment] [12] feature, there also we need to run rake db:migrate manually and Heroku will not run it automatically for us. That means it is not fully automatic.

When I setup heroku auto-deployment I wanted everything to happen automatically, why should I run migration command manually. Because every time after auto-deployment I have to make sure I run rake db:migarte otherwise I might end up having a broken environment.

To automate it I found a nice solution.

  • Heroku has buildpacks scripts and more details are here. Buildpacks are the scripts that power app builds on Heroku.

  • We can use some nice existing script and we execute any command, that we would like to execute during app deployment on Heroku.

  • [Heroku-buildpack-multi][11] is a good plugin to append additional commands to Heroku build-pack.

  • Open terminal, and go to project directoty and run heroku command.

      heroku buildpacks:set https://github.com/ddollar/heroku-buildpack-multi.git
    
  • Create a .buildpacks file in root of your project directory

     nano .buildpacks
    
  • Then enter/paste these git path to the .buildpacks file.

      https://github.com/heroku/heroku-buildpack-ruby.git
      https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks
    
  • Then on terminal run this command, it will set environment variable DEPLOY_TASKS which will run the migration

      heroku config:set DEPLOY_TASKS='db:migrate'
    

Yay Yay!! Now next time when deployment will happen you will see that heroku will auto run the rake db:migarte command as we have set it in the buildpack. And you need not to run it manually.

[11]: heroku buildpacks:set https://github.com/ddollar/heroku-buildpack-multi.git
[12]: http://knowbuddy.kiprosh-app.com/posts/auto-deployment-with-heroku-of-github-bitbucket-source-code