提交 9771f5e1 编写于 作者: G Grey Baker

Ignore index name in `index_exists?` when not passed a name to check for

上级 e73fe1dd
* Ignore index name in `index_exists?` and `remove_index` when not passed a
name to check for.
*Grey Baker*
* Version the API presented to migration classes, so we can change parameter
defaults without breaking existing migrations, or forcing them to be
rewritten through a deprecation cycle.
......
......@@ -82,11 +82,10 @@ def view_exists?(view_name)
#
def index_exists?(table_name, column_name, options = {})
column_names = Array(column_name).map(&:to_s)
index_name = options.key?(:name) ? options[:name].to_s : index_name(table_name, column: column_names)
checks = []
checks << lambda { |i| i.name == index_name }
checks << lambda { |i| i.columns == column_names }
checks << lambda { |i| i.unique } if options[:unique]
checks << lambda { |i| i.name == options[:name].to_s } if options[:name]
indexes(table_name).any? { |i| checks.all? { |check| check[i] } }
end
......
......@@ -28,6 +28,17 @@ def add_timestamps(*, **options)
options[:null] = true if options[:null].nil?
super
end
def index_exists?(table_name, column_name, options = {})
column_names = Array(column_name).map(&:to_s)
options[:name] =
if options.key?(:name).present?
options[:name].to_s
else
index_name(table_name, column: column_names)
end
super
end
end
class V4_2 < V5_0
......
......@@ -130,7 +130,9 @@ def test_unique_index_exists
def test_named_index_exists
connection.add_index :testings, :foo, :name => "custom_index_name"
assert connection.index_exists?(:testings, :foo)
assert connection.index_exists?(:testings, :foo, :name => "custom_index_name")
assert !connection.index_exists?(:testings, :foo, :name => "other_index_name")
end
def test_add_index_attribute_length_limit
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册