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

Allow adding nonnamed expression indexes to be revertible

This commit is contained in:
fatkodima 2020-12-11 17:52:00 +02:00
parent 3e5d504f78
commit 633fa55c1c
3 changed files with 41 additions and 1 deletions

View file

@ -1,3 +1,16 @@
* Allow adding nonnamed expression indexes to be revertible.
Fixes #40732.
Previously, the following code would raise an error, when executed while rolling back,
and the index name should be specified explicitly. Now, the index name is inferred
automatically.
```ruby
add_index(:items, "to_tsvector('english', description)")
```
*fatkodima*
* Only warn about negative enums if a positive form that would cause conflicts exists.
Fixes #39065.

View file

@ -1387,8 +1387,14 @@ module ActiveRecord
checks = []
if !options.key?(:name) && column_name.is_a?(String) && /\W/.match?(column_name)
options[:name] = index_name(table_name, column_name)
column_names = []
else
column_names = index_column_names(column_name || options[:column])
end
checks << lambda { |i| i.name == options[:name].to_s } if options.key?(:name)
column_names = index_column_names(column_name || options[:column])
if column_names.present?
checks << lambda { |i| index_name(table_name, i.columns) == index_name(table_name, column_names) }

View file

@ -18,6 +18,7 @@ module ActiveRecord
create_table("horses") do |t|
t.column :content, :text
t.column :remind_at, :datetime
t.column :place_id, :integer
end
end
end
@ -198,6 +199,12 @@ module ActiveRecord
end
end
class RevertNonNamedExpressionIndexMigration < SilentMigration
def change
add_index :horses, "remind_at, place_id"
end
end
class RevertCustomForeignKeyTable < SilentMigration
def change
change_table(:horses) do |t|
@ -485,6 +492,20 @@ module ActiveRecord
end
end
def test_migrate_revert_add_index_without_name_on_expression
InvertibleMigration.new.migrate(:up)
RevertNonNamedExpressionIndexMigration.new.migrate(:up)
connection = ActiveRecord::Base.connection
assert connection.index_exists?(:horses, [:remind_at, :place_id]),
"index on remind_at and place_id should exist"
RevertNonNamedExpressionIndexMigration.new.migrate(:down)
assert_not connection.index_exists?(:horses, [:remind_at, :place_id]),
"index on remind_at and place_id should not exist"
end
def test_up_only
InvertibleMigration.new.migrate(:up)
horse1 = Horse.create