aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorGrey Baker <greysteil@gmail.com>2015-03-22 11:14:51 +0000
committerGrey Baker <greysteil@gmail.com>2015-12-15 17:36:09 +0000
commit9771f5e178ebc1b8c5a61f813373ca3b7bd983c8 (patch)
tree8b2c4d02f657c269b3935d6d764c6117f009b234 /activerecord
parente73fe1dd8c2740ae29e7a7f48d71a62b46e6b49d (diff)
downloadrails-9771f5e178ebc1b8c5a61f813373ca3b7bd983c8.tar.gz
rails-9771f5e178ebc1b8c5a61f813373ca3b7bd983c8.tar.bz2
rails-9771f5e178ebc1b8c5a61f813373ca3b7bd983c8.zip
Ignore index name in `index_exists?` when not passed a name to check for
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb3
-rw-r--r--activerecord/lib/active_record/migration/compatibility.rb11
-rw-r--r--activerecord/test/cases/migration/index_test.rb2
4 files changed, 19 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 87420a0746..781b786012 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
+* 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.
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
index b50d28862c..93f0d9d0e7 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -82,11 +82,10 @@ module ActiveRecord
#
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
diff --git a/activerecord/lib/active_record/migration/compatibility.rb b/activerecord/lib/active_record/migration/compatibility.rb
index 4c8db8a2d5..bc5b97a711 100644
--- a/activerecord/lib/active_record/migration/compatibility.rb
+++ b/activerecord/lib/active_record/migration/compatibility.rb
@@ -28,6 +28,17 @@ module ActiveRecord
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
diff --git a/activerecord/test/cases/migration/index_test.rb b/activerecord/test/cases/migration/index_test.rb
index b23b9a679f..70169e501c 100644
--- a/activerecord/test/cases/migration/index_test.rb
+++ b/activerecord/test/cases/migration/index_test.rb
@@ -130,7 +130,9 @@ module ActiveRecord
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