Tweaking Capistrano environment

Recently, we’ve been playing with our database setup in order to improve its resiliency. During the experimentation, we had multiple versions of the database compiled and running. To test everything, we temporarily needed to run db:migrate and related tasks against the new database.

Our assumption was that it would be taken care of by changing the credentials for the database to the new instance. The migration runs just fine, but at the end, it dumps the schema to a file. Enectiva uses Postgres and we’ve long ago moved from database agnostic schema.rb to structure.sql and dumping to SQL uses pg_dump under the hood.

In our experiment, the old version of Postgres binaries was on the $PATH while the experimental versions were sitting in another directory. The solution was, therefore, to specify the path to the binary. This turns out to be a problem, because Rails assumes the binary is on the $PATH and doesn’t give us any tools to configure it. That meant tricking it from the outside and putting the new binary on the $PATH.

Capistrano 3 has a variable called :default_env exactly for this purpose. However, no one has taken the time yet to rewrite all our custom Capistrano tasks to make them compatible with version 3 so we’re still on version 2. Which, as it turns out, doesn’t have this nice feature. Looking for documentation for an older version of a gem is always annoying - almost all StackOverflow questions and blog posts are about the current version and old documentation is well tucked away - so the search ended up in the source code. The code revealed that there is a configurable variable named :migrate_env specifically for database migration. The resulting code therefore looks like this:

set :migrate_env, defer {

Code for Capistrano 3 would look like (not tested):

set :default_env, defer {

It’s not clear what is the lesson here. One could be keep up with the updates, but the more significant seems to be prepare for reading Rails source code when you have non standard setup.

We're looking for developers to help us save energy

If you're interested in what we do and you would like to help us save energy, drop us a line at

comments powered by Disqus