How to auto run `rake db:migrate` after deploying to Heroku

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 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 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

vi .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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: