Skip to content

Commit 2e382ef

Browse files
authored
Merge pull request rails#54298 from aliismayilov/index-exists-columns-optional
Make column name optional for `index_exists?`
2 parents c00096f + 08a0b4d commit 2e382ef

File tree

5 files changed

+11
-3
lines changed

5 files changed

+11
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Make column name optional for `index_exists?`.
2+
3+
This aligns well with `remove_index` signature as well, where
4+
index name doesn't need to be derived from the column names.
5+
6+
*Ali Ismayiliov*
7+
18
* Change the payload name of `sql.active_record` notification for eager
29
loading from "SQL" to "#{model.name} Eager Load".
310

activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ def index(column_name, **options)
764764
# end
765765
#
766766
# See {connection.index_exists?}[rdoc-ref:SchemaStatements#index_exists?]
767-
def index_exists?(column_name, **options)
767+
def index_exists?(column_name = nil, **options)
768768
@base.index_exists?(name, column_name, **options)
769769
end
770770

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def indexes(table_name)
9999
# # Check a valid index exists (PostgreSQL only)
100100
# index_exists?(:suppliers, :company_id, valid: true)
101101
#
102-
def index_exists?(table_name, column_name, **options)
102+
def index_exists?(table_name, column_name = nil, **options)
103103
indexes(table_name).any? { |i| i.defined_for?(column_name, **options) }
104104
end
105105

activerecord/lib/active_record/migration/compatibility.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def add_timestamps(table_name, **options)
445445
super
446446
end
447447

448-
def index_exists?(table_name, column_name, **options)
448+
def index_exists?(table_name, column_name = nil, **options)
449449
column_names = Array(column_name).map(&:to_s)
450450
options[:name] =
451451
if options[:name].present?

activerecord/test/cases/migration/index_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def test_index_exists_with_custom_name_checks_columns
178178
connection.add_index :testings, [:foo, :bar], name: "my_index"
179179
assert connection.index_exists?(:testings, [:foo, :bar], name: "my_index")
180180
assert connection.index_exists?(:testings, [], name: "my_index")
181+
assert connection.index_exists?(:testings, name: "my_index")
181182
assert_not connection.index_exists?(:testings, [:foo], name: "my_index")
182183
end
183184

0 commit comments

Comments
 (0)