1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Drop the correct index after reverting a migration

Previously when reverting a migration which added a named index it
would instead drop a corresponding index with matching columns but
without a name.
This commit is contained in:
Hubert Dąbrowski 2014-02-12 22:23:16 +01:00
parent 4dc42045e2
commit e1d4673102
4 changed files with 49 additions and 4 deletions

View file

@ -1,3 +1,15 @@
* When inverting add_index use the index name if present instead of
the columns.
If there are two indices with matching columns and one of them is
explicitly named then reverting the migration adding the named one
would instead drop the unnamed one.
The inversion of add_index will now drop the index by its name if
it is present.
*Hubert Dąbrowski*
* Add flag to disable schema dump after migration.
Add a config parameter on Active Record named `dump_schema_after_migration`

View file

@ -140,7 +140,12 @@ module ActiveRecord
def invert_add_index(args)
table, columns, options = *args
[:remove_index, [table, (options || {}).merge(column: columns)]]
options ||= {}
index_name = options[:name]
options_hash = index_name ? { name: index_name } : { column: columns }
[:remove_index, [table, options_hash]]
end
def invert_remove_index(args)

View file

@ -106,6 +106,22 @@ module ActiveRecord
end
end
class RevertNamedIndexMigration1 < SilentMigration
def change
create_table("horses") do |t|
t.column :content, :string
t.column :remind_at, :datetime
end
add_index :horses, :content
end
end
class RevertNamedIndexMigration2 < SilentMigration
def change
add_index :horses, :content, name: "horses_index_named"
end
end
def teardown
%w[horses new_horses].each do |table|
if ActiveRecord::Base.connection.table_exists?(table)
@ -255,5 +271,17 @@ module ActiveRecord
ActiveRecord::Base.table_name_prefix = ActiveRecord::Base.table_name_suffix = ''
end
def test_migrate_revert_add_index_with_name
RevertNamedIndexMigration1.new.migrate(:up)
RevertNamedIndexMigration2.new.migrate(:up)
RevertNamedIndexMigration2.new.migrate(:down)
connection = ActiveRecord::Base.connection
assert connection.index_exists?(:horses, :content),
"index on content should exist"
assert !connection.index_exists?(:horses, :content, name: "horses_index_named"),
"horses_index_named index should not exist"
end
end
end

View file

@ -174,13 +174,13 @@ module ActiveRecord
end
def test_invert_add_index
remove = @recorder.inverse_of :add_index, [:table, [:one, :two], options: true]
assert_equal [:remove_index, [:table, {column: [:one, :two], options: true}]], remove
remove = @recorder.inverse_of :add_index, [:table, [:one, :two]]
assert_equal [:remove_index, [:table, {column: [:one, :two]}]], remove
end
def test_invert_add_index_with_name
remove = @recorder.inverse_of :add_index, [:table, [:one, :two], name: "new_index"]
assert_equal [:remove_index, [:table, {column: [:one, :two], name: "new_index"}]], remove
assert_equal [:remove_index, [:table, {name: "new_index"}]], remove
end
def test_invert_add_index_with_no_options