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.
One Comment
“RAILS_ENV=staging rake db:reset VERSION=3″ yeah exactly, this is how Rake works. And it’s just ridiculous, no reasonable CLI args parsing. So I wrote nake http://github.com/botanicus/nake (README is a bit old, but there is quite a few examples in examples/). However as all my gems, it’s Ruby 1.9-only.
To be more precise Rake do have CLI args parsing, but it’s such a piece of shit that anyone actually use it. And it’s pretty buggy as well.
Capistrano tend to have some real troubles with Git when deploy_to & repository is on the same machine, so I also wrote http://github.com/botanicus/git-deployer which works – and here the git magic comes – on post-receive hook. So when you create a project, you run some tasks for pushing a hook to the remote server and then when you do git push myserver master, than the hook run and can migrate DB or do whatever. Pretty handy, it can deploy to more servers, but it’s definitely more lightweight tool than Capistrano. BTW – git-deployer is in quite alpha stage so far.