Continuous Delivery - Part 4: Rolling back database migrations with Capistrano rollbackleena
August 14, 2011
According to the book Continuous Delivery, the database also should be under version control, and Rails allows us to achieve this with ActiveRecord Migrations. Even though Capistrano can run the migrations automatically with its
deploy command, its
deploy:rollback task does not rollback the DB migrations automatically. I’ve created a small capistrano recipe which can take care of rolling back migrations. The assumptions made are:
- All the migrations have the down method defined properly. You can check for this by running
- The schema.rb exists in the repository. This is one of the suggested practices for Rails.
The script is very simple, it extracts the version from the
schema.rb and runs the
rake db:migrate with the same version. The task gets run automatically along with
deploy:rollback. This approach should work for most small and medium complexity Rails apps.