提交 0e928de3 编写于 作者: Y Yves Senn

make `remove_index :table, :column` reversible.

This used to raise a `IrreversibleMigration` error (since #10437).
However since `remove_index :table, :column` is probably the most basic
use-case we should make it reversible again.

Conflicts:
	activerecord/CHANGELOG.md
上级 863fcfa7
* Make `remove_index :table, :column` reversible.
*Yves Senn*
* Fixed an error which would occur in dirty checking when calling * Fixed an error which would occur in dirty checking when calling
`update_attributes` from a getter. `update_attributes` from a getter.
......
...@@ -587,7 +587,7 @@ def add_index(table_name, column_name, options = {}) ...@@ -587,7 +587,7 @@ def add_index(table_name, column_name, options = {})
# #
# Removes the +index_accounts_on_column+ in the +accounts+ table. # Removes the +index_accounts_on_column+ in the +accounts+ table.
# #
# remove_index :accounts, :column # remove_index :accounts, :branch_id
# #
# Removes the index named +index_accounts_on_branch_id+ in the +accounts+ table. # Removes the index named +index_accounts_on_branch_id+ in the +accounts+ table.
# #
......
...@@ -151,14 +151,16 @@ def invert_add_index(args) ...@@ -151,14 +151,16 @@ def invert_add_index(args)
end end
def invert_remove_index(args) def invert_remove_index(args)
table, options = *args table, options_or_column = *args
if (options = options_or_column).is_a?(Hash)
unless options && options.is_a?(Hash) && options[:column] unless options[:column]
raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option." raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option."
end
options = options.dup
[:add_index, [table, options.delete(:column), options]]
elsif (column = options_or_column).present?
[:add_index, [table, column]]
end end
options = options.dup
[:add_index, [table, options.delete(:column), options]]
end end
alias :invert_add_belongs_to :invert_add_reference alias :invert_add_belongs_to :invert_add_reference
......
...@@ -144,13 +144,17 @@ def test_no_reverse ...@@ -144,13 +144,17 @@ def test_no_reverse
end end
def test_exception_on_removing_index_without_column_option def test_exception_on_removing_index_without_column_option
RemoveIndexMigration1.new.migrate(:up) index_definition = ["horses", [:name, :color]]
migration = RemoveIndexMigration2.new migration1 = RemoveIndexMigration1.new
migration.migrate(:up) migration1.migrate(:up)
assert migration1.connection.index_exists?(*index_definition)
assert_raises(IrreversibleMigration) do migration2 = RemoveIndexMigration2.new
migration.migrate(:down) migration2.migrate(:up)
end assert_not migration2.connection.index_exists?(*index_definition)
migration2.migrate(:down)
assert migration2.connection.index_exists?(*index_definition)
end end
def test_migrate_up def test_migrate_up
......
...@@ -206,6 +206,11 @@ def test_invert_add_index_with_no_options ...@@ -206,6 +206,11 @@ def test_invert_add_index_with_no_options
end end
def test_invert_remove_index def test_invert_remove_index
add = @recorder.inverse_of :remove_index, [:table, :one]
assert_equal [:add_index, [:table, :one]], add
end
def test_invert_remove_index_with_column
add = @recorder.inverse_of :remove_index, [:table, {column: [:one, :two], options: true}] add = @recorder.inverse_of :remove_index, [:table, {column: [:one, :two], options: true}]
assert_equal [:add_index, [:table, [:one, :two], options: true]], add assert_equal [:add_index, [:table, [:one, :two], options: true]], add
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册