aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeeraj Singh <neerajdotname@gmail.com>2013-05-03 00:46:18 -0400
committerNeeraj Singh <neerajdotname@gmail.com>2013-05-06 15:16:42 -0400
commit3771e4d51122e1ec22728029bae00f121d5d4e3b (patch)
treee9d684ba94974f8ebb2780c51d1b8178575e9dc4
parent32b4abddfa1f01009e0a9eb2a8820715b2fae78a (diff)
downloadrails-3771e4d51122e1ec22728029bae00f121d5d4e3b.tar.gz
rails-3771e4d51122e1ec22728029bae00f121d5d4e3b.tar.bz2
rails-3771e4d51122e1ec22728029bae00f121d5d4e3b.zip
raise IrreversibleMigration if no column given
fixes #10419 Following code should raise IrreversibleMigration. But the code was failing since options is an array and not a hash. def change change_table :users do |t| t.remove_index [:name, :email] end end Fix was to check if the options is a Hash before operating on it.
-rw-r--r--activerecord/CHANGELOG.md17
-rw-r--r--activerecord/lib/active_record/migration/command_recorder.rb5
-rw-r--r--activerecord/test/cases/invertible_migration_test.rb28
3 files changed, 49 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 917f606d0e..1304bcb7e4 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,20 @@
+* While removing index if column option is missing then raise IrreversibleMigration exception.
+
+ Following code should raise `IrreversibleMigration`. But the code was
+ failing since options is an array and not a hash.
+
+ def change
+ change_table :users do |t|
+ t.remove_index [:name, :email]
+ end
+ end
+
+ Fix was to check if the options is a Hash before operating on it.
+
+ Fixes #10419.
+
+ *Neeraj Singh*
+
* Do not overwrite manually built records during one-to-one nested attribute assignment
For one-to-one nested associations, if you build the new (in-memory)
diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb
index 79c55045ba..9782a48055 100644
--- a/activerecord/lib/active_record/migration/command_recorder.rb
+++ b/activerecord/lib/active_record/migration/command_recorder.rb
@@ -144,7 +144,10 @@ module ActiveRecord
def invert_remove_index(args)
table, options = *args
- raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option." unless options && options[:column]
+
+ unless options && options.is_a?(Hash) && options[:column]
+ raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option."
+ end
options = options.dup
[:add_index, [table, options.delete(:column), options]]
diff --git a/activerecord/test/cases/invertible_migration_test.rb b/activerecord/test/cases/invertible_migration_test.rb
index be59ffc4ab..0631871bd5 100644
--- a/activerecord/test/cases/invertible_migration_test.rb
+++ b/activerecord/test/cases/invertible_migration_test.rb
@@ -58,6 +58,24 @@ module ActiveRecord
end
end
+ class RemoveIndexMigration1 < SilentMigration
+ def self.up
+ create_table("horses") do |t|
+ t.column :name, :text
+ t.column :color, :text
+ t.index [:name, :color]
+ end
+ end
+ end
+
+ class RemoveIndexMigration2 < SilentMigration
+ def change
+ change_table("horses") do |t|
+ t.remove_index [:name, :color]
+ end
+ end
+ end
+
class LegacyMigration < ActiveRecord::Migration
def self.up
create_table("horses") do |t|
@@ -104,6 +122,16 @@ module ActiveRecord
end
end
+ def test_exception_on_removing_index_without_column_option
+ RemoveIndexMigration1.new.migrate(:up)
+ migration = RemoveIndexMigration2.new
+ migration.migrate(:up)
+
+ assert_raises(IrreversibleMigration) do
+ migration.migrate(:down)
+ end
+ end
+
def test_migrate_up
migration = InvertibleMigration.new
migration.migrate(:up)