Posted about 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:

Copy
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:

Copy
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:

Copy
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) ===========================

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
about 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