Skip to content

Commit 5e67187

Browse files
committed
The default arg of index_name_exists? makes to optional
The `default` arg of `index_name_exists?` is only used the adapter does not implemented `indexes`. But currently all adapters implemented `indexes` (See rails#26688). Therefore the `default` arg is never used.
1 parent c6b4b4a commit 5e67187

File tree

5 files changed

+18
-24
lines changed

5 files changed

+18
-24
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,7 @@ def index_name(table_name, options) #:nodoc:
773773
end
774774

775775
# Verifies the existence of an index with a given name.
776-
#
777-
# The default argument is returned if the underlying implementation does not define the indexes method,
778-
# as there's no way to determine the correct answer in that case.
779-
def index_name_exists?(table_name, index_name, default)
780-
return default unless respond_to?(:indexes)
776+
def index_name_exists?(table_name, index_name, default = nil)
781777
index_name = index_name.to_s
782778
indexes(table_name).detect { |i| i.name == index_name }
783779
end
@@ -1149,7 +1145,7 @@ def add_index_options(table_name, column_name, comment: nil, **options) # :nodoc
11491145

11501146
validate_index_length!(table_name, index_name, options.fetch(:internal, false))
11511147

1152-
if data_source_exists?(table_name) && index_name_exists?(table_name, index_name, false)
1148+
if data_source_exists?(table_name) && index_name_exists?(table_name, index_name)
11531149
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' already exists"
11541150
end
11551151
index_columns = quoted_columns_for_index(column_names, options).join(", ")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def schema_exists?(name)
132132
end
133133

134134
# Verifies existence of an index with a given name.
135-
def index_name_exists?(table_name, index_name, default)
135+
def index_name_exists?(table_name, index_name, default = nil)
136136
table = Utils.extract_schema_qualified_name(table_name.to_s)
137137
index = Utils.extract_schema_qualified_name(index_name.to_s)
138138

activerecord/lib/active_record/migration/compatibility.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ def remove_index(table_name, options = {})
145145
def index_name_for_remove(table_name, options = {})
146146
index_name = index_name(table_name, options)
147147

148-
unless index_name_exists?(table_name, index_name, true)
148+
unless index_name_exists?(table_name, index_name)
149149
if options.is_a?(Hash) && options.has_key?(:name)
150150
options_without_column = options.dup
151151
options_without_column.delete :column
152152
index_name_without_column = index_name(table_name, options_without_column)
153153

154-
return index_name_without_column if index_name_exists?(table_name, index_name_without_column, false)
154+
return index_name_without_column if index_name_exists?(table_name, index_name_without_column)
155155
end
156156

157157
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' does not exist"

activerecord/test/cases/adapters/postgresql/schema_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,13 @@ def test_ignore_nil_schema_search_path
301301

302302
def test_index_name_exists
303303
with_schema_search_path(SCHEMA_NAME) do
304-
assert @connection.index_name_exists?(TABLE_NAME, INDEX_A_NAME, true)
305-
assert @connection.index_name_exists?(TABLE_NAME, INDEX_B_NAME, true)
306-
assert @connection.index_name_exists?(TABLE_NAME, INDEX_C_NAME, true)
307-
assert @connection.index_name_exists?(TABLE_NAME, INDEX_D_NAME, true)
308-
assert @connection.index_name_exists?(TABLE_NAME, INDEX_E_NAME, true)
309-
assert @connection.index_name_exists?(TABLE_NAME, INDEX_E_NAME, true)
310-
assert_not @connection.index_name_exists?(TABLE_NAME, "missing_index", true)
304+
assert @connection.index_name_exists?(TABLE_NAME, INDEX_A_NAME)
305+
assert @connection.index_name_exists?(TABLE_NAME, INDEX_B_NAME)
306+
assert @connection.index_name_exists?(TABLE_NAME, INDEX_C_NAME)
307+
assert @connection.index_name_exists?(TABLE_NAME, INDEX_D_NAME)
308+
assert @connection.index_name_exists?(TABLE_NAME, INDEX_E_NAME)
309+
assert @connection.index_name_exists?(TABLE_NAME, INDEX_E_NAME)
310+
assert_not @connection.index_name_exists?(TABLE_NAME, "missing_index")
311311
end
312312
end
313313

activerecord/test/cases/migration/index_test.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ def test_rename_index
3131
connection.add_index(table_name, [:foo], name: "old_idx")
3232
connection.rename_index(table_name, "old_idx", "new_idx")
3333

34-
# if the adapter doesn't support the indexes call, pick defaults that let the test pass
35-
assert_not connection.index_name_exists?(table_name, "old_idx", false)
36-
assert connection.index_name_exists?(table_name, "new_idx", true)
34+
assert_not connection.index_name_exists?(table_name, "old_idx")
35+
assert connection.index_name_exists?(table_name, "new_idx")
3736
end
3837

3938
def test_rename_index_too_long
@@ -45,8 +44,7 @@ def test_rename_index_too_long
4544
}
4645
assert_match(/too long; the limit is #{connection.allowed_index_name_length} characters/, e.message)
4746

48-
# if the adapter doesn't support the indexes call, pick defaults that let the test pass
49-
assert connection.index_name_exists?(table_name, "old_idx", false)
47+
assert connection.index_name_exists?(table_name, "old_idx")
5048
end
5149

5250
def test_double_add_index
@@ -63,7 +61,7 @@ def test_remove_nonexistent_index
6361
def test_add_index_works_with_long_index_names
6462
connection.add_index(table_name, "foo", name: good_index_name)
6563

66-
assert connection.index_name_exists?(table_name, good_index_name, false)
64+
assert connection.index_name_exists?(table_name, good_index_name)
6765
connection.remove_index(table_name, name: good_index_name)
6866
end
6967

@@ -75,15 +73,15 @@ def test_add_index_does_not_accept_too_long_index_names
7573
}
7674
assert_match(/too long; the limit is #{connection.allowed_index_name_length} characters/, e.message)
7775

78-
assert_not connection.index_name_exists?(table_name, too_long_index_name, false)
76+
assert_not connection.index_name_exists?(table_name, too_long_index_name)
7977
connection.add_index(table_name, "foo", name: good_index_name)
8078
end
8179

8280
def test_internal_index_with_name_matching_database_limit
8381
good_index_name = "x" * connection.index_name_length
8482
connection.add_index(table_name, "foo", name: good_index_name, internal: true)
8583

86-
assert connection.index_name_exists?(table_name, good_index_name, false)
84+
assert connection.index_name_exists?(table_name, good_index_name)
8785
connection.remove_index(table_name, name: good_index_name)
8886
end
8987

0 commit comments

Comments
 (0)