Setting environments with Rails: how Rake and Capistrano differ

I keep forgetting this when I'm developing with Rails, and while it's not massively hard to find, it IS annoying to keep looking up, so scribbling it here should commit it slightly more fully to memory

If you want to make sure stuff happens in a certain environment with Rake, you need to make sure you set the environment before you call the command. You'll do this when setting up a database for testing.

# Drop the test database
RAILS_ENV=test rake db:drop
# Clear database and reset schema to restart
RAILS_ENV=test rake db:reset
# Recreate the staging database, but only migrate it to version 3
RAILS_ENV=staging rake db:reset VERSION=3

However, If you're using Capistrano, and multistage environments (and you really should, it makes live, way, way better) you'll need to set it after calling the command initial, but before you pass the argument like so:

# deploy an app the beta environment
cap beta deploy
# setup the staging environment before deployment
cap staging deploy:setup

Rake comes first, and cap comes second.

Copyright © 2020 Chris Adams
Powered by Cryogen
Theme by KingMob