Posted almost 10 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) ===========================

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).

Owner of this card:

Avatar
Henning Koch
Last edit:
almost 10 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 short-lived cookies to improve usability.
Accept or learn more