From 2cc49df85a4b0232a804944caead6f288a20ad26 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Fri, 28 Aug 2015 13:50:49 +0900 Subject: Add detailed error message to `IrreversibleMigration` --- activerecord/lib/active_record/migration/command_recorder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index b52e89d792..ab5a4cad89 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -69,7 +69,7 @@ module ActiveRecord # invert the +command+. def inverse_of(command, args, &block) method = :"invert_#{command}" - raise IrreversibleMigration unless respond_to?(method, true) + raise IrreversibleMigration, "#{method} is not defined, so #{command} is not reversible" unless respond_to?(method, true) send(method, args, &block) end -- cgit v1.2.3 From f13d31c37c93cabdb61c1de010af6ad134e2ec6f Mon Sep 17 00:00:00 2001 From: yui-knk Date: Fri, 28 Aug 2015 19:17:06 +0900 Subject: Add detailed error message to `IrreversibleMigration` --- activerecord/lib/active_record/migration/command_recorder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index ab5a4cad89..6a0539e938 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -69,7 +69,7 @@ module ActiveRecord # invert the +command+. def inverse_of(command, args, &block) method = :"invert_#{command}" - raise IrreversibleMigration, "#{method} is not defined, so #{command} is not reversible" unless respond_to?(method, true) + raise IrreversibleMigration, "This migration uses #{command} that is not automatically reversible, please define down method" unless respond_to?(method, true) send(method, args, &block) end -- cgit v1.2.3 From bef377c666d54719ec8bd58979da55fd48cb5dfd Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 29 Aug 2015 17:15:11 +0900 Subject: [ci skip] Add comments for `IrreversibleMigration` --- activerecord/lib/active_record/migration.rb | 70 +++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 4901f9dafd..93e7a04f55 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -10,6 +10,76 @@ module ActiveRecord end # Exception that can be raised to stop migrations from going backwards. + # For example IrreversibleMigrationExample is not reversible. + # When you rollback this migration, ActiveRecord::IrreversibleMigration is raised. + # + # class IrreversibleMigrationExample < ActiveRecord::Migration + # def change + # create_table :distributors do |t| + # t.string :zipcode + # end + # + # execute <<-SQL + # ALTER TABLE distributors + # ADD CONSTRAINT zipchk + # CHECK (char_length(zipcode) = 5) NO INHERIT; + # SQL + # end + # end + # + # There are two ways to mitigate this problem. + # + # 1. Use up and down methods instead of change: + # + # class ReversibleMigrationExample < ActiveRecord::Migration + # def up + # create_table :distributors do |t| + # t.string :zipcode + # end + # + # execute <<-SQL + # ALTER TABLE distributors + # ADD CONSTRAINT zipchk + # CHECK (char_length(zipcode) = 5) NO INHERIT; + # SQL + # end + # + # def down + # execute <<-SQL + # ALTER TABLE distributors + # DROP CONSTRAINT zipchk + # SQL + # + # drop_table :distributors + # end + # end + # + # 2. Use reversible method in change method: + # + # class ReversibleMigrationExample < ActiveRecord::Migration + # def change + # create_table :distributors do |t| + # t.string :zipcode + # end + # + # reversible do |dir| + # dir.up do + # execute <<-SQL + # ALTER TABLE distributors + # ADD CONSTRAINT zipchk + # CHECK (char_length(zipcode) = 5) NO INHERIT; + # SQL + # end + # + # dir.down do + # execute <<-SQL + # ALTER TABLE distributors + # DROP CONSTRAINT zipchk + # SQL + # end + # end + # end + # end class IrreversibleMigration < MigrationError end -- cgit v1.2.3 From e16c93400be814b8ea961ae58b00890e3860a8d8 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sun, 30 Aug 2015 09:21:36 +0900 Subject: Add detailed error message to `IrreversibleMigration` --- activerecord/lib/active_record/migration/command_recorder.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index 6a0539e938..73dc51912a 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -69,7 +69,12 @@ module ActiveRecord # invert the +command+. def inverse_of(command, args, &block) method = :"invert_#{command}" - raise IrreversibleMigration, "This migration uses #{command} that is not automatically reversible, please define down method" unless respond_to?(method, true) + raise IrreversibleMigration, <<-MSG.strip_heredoc unless respond_to?(method, true) + This migration uses #{command} that is not automatically reversible. + There are two ways to mitigate this problem. + 1. Define up and down methods instead of change method + 2. Use reversible method in change method + MSG send(method, args, &block) end -- cgit v1.2.3