Posted over 7 years ago. Visible to the public.

Rails 3.1 gives you free down migrations

In Rails 3.1+, instead of defining a separate up and down method you can define a single method change:

class AddComparisonFieldsToReport < ActiveRecord::Migration def change add_column :reports, :compare, :boolean update "UPDATE reports SET compare = #{quoted_false}" add_column :reports, :compare_start_date, :date add_column :reports, :compare_end_date, :date end end

Migrating up works as expected:

b rake db:migrate == AddComparisonFieldsToReport: migrating ==================================== -- add_column(:reports, :compare, :boolean) -> 0.0274s -- quoted_false() -> 0.0000s -- update("UPDATE reports SET compare = 0") -> 0.0018s -> 2 rows -- add_column(:reports, :compare_start_date, :date) -> 0.0610s -- add_column(:reports, :compare_end_date, :date) -> 0.0451s == AddComparisonFieldsToReport: migrated (0.1358s) ===========================

Migrating down also works as expected. Note how destructive commands that cannot be reverted (update) are simply skipped:

b rake db:rollback == AddComparisonFieldsToReport: reverting ==================================== -- remove_column("reports", :compare_end_date) -> 0.0699s -- remove_column("reports", :compare_start_date) -> 0.0448s -- remove_column("reports", :compare) -> 0.0294s == AddComparisonFieldsToReport: reverted (0.1444s) ===========================

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Henning Koch
Last edit:
over 7 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more