Run migrations on Dokku/Heroku deployment

We at Firmhouse are currently moving our projects over to a new version of Intercity, which is using Dokku as its backend. Dokku is a "Docker powered mini-Heroku", this means you can deploy your code by doing a Git push.

One thing that we miss in Dokku is a way to have "After-deploy" hooks to run migrations on deployment. This means that after you deployed, you need to manually run the migrations on your server; this is annoying and error prone!

When inspecting the way Dokku deploys its code, we see that it is using the Heroku buildpacks on the background. Heroku runs the asset precompilation task in the Deploy cycle, and since this is a Rake task, we can hook into this!

By creating the following file, we can run all migrations after the asset compilation task.

# lib/tasks/run_migrations.rake
Rake::Task['assets:clean'].enhance do  
  Rake::Task['db:migrate'].invoke
end  

At first, this felt like a hack, but after using this method for over a month, I start to really like this!

If you have questions about this, ping me on Twitter (@jvanbaarsen), or send me an email