In my spare time I’ve been playing with Rails for a while now. This week I tried using SwitchTower and ActiveRecord Migrations for the first time, and I’m very impressed.

Migrations provide a way for versioning and applying database changes. A simple call to script/generate migration MigrationName generates a new blank migration file with the appropriate version number. The file contains two methods up and down, which up- and down-grade (for rollback purposes) the database respectively. After adding the necessary statements to both methods (for example an add_column call in up and the corresponding remove_column call in down), applying the migration is as easy as running rake migrate.

SwitchTower is a deployment framework, although this term doesn’t really do it justice. It is highly configurable and able to execute multiple commands in parallel on different servers. This can be used to upgrade several application and database servers at the same time. While it can theoretically be used for any type of application (even non-Ruby applications), it is very much geared towards Rails and works best when used in conjunction with a Subversion or CVS repository.

SwitchTower can be installed using gem install switchtower. After that, a Rails app needs to be enabled for SwitchTower by running the following command: switchtower –apply-to /path/to/my/app. This copies over the necessary Rake tasks and creates a default recipe in config/deploy.rb, which defines all deployment parameters for the application (repository, the various servers, username, etc.). After customizing the recipe, a number of SwitchTower tasks can be invoked via Rake. A comprehensive list of all relevant tasks can be obtained via rake show_deploy_tasks. The most important task is deploy, which deploys the latest version of the application to the remote servers. In case anything goes wrong, the rollback task can be used to quickly roll back to the previously deployed version. A variant of the deploy task, deploy_with_migrations performs the necessary ActiveRecord Migrations (see above) as part of the deployment. Additionally, any task can be executed remotely via the remote_exec task. For example, rake remote_exec ACTION=migrate performs the remote database migrations. Note that if you haven’t done so already, you will probably want to setup ssh keys on the remote server in order to avoid having to enter your password multiple times during the deployment process.

So far I have only scratched the surface of SwitchTower. The basic functionality is easy to setup and use, and the comprehensive documentation reveals a wealth of additional features.

If you are using a hosting provider, you may want to google for some existing SwitchTower recipes for your provider. I am using DreamHost, and found Geoffrey Grossenbach’s DreamHost SwitchTower recipe. I ended up having to change most of the file because my setup varied considerably from the assumptions in this recipe, but depending on your setup this may be a good starting point.

If you are working with Rails, I strongly encourage you to check out these two technologies, as I can guarantee that they will make your life simpler.