Skip to content

Commit

Permalink
Fix shortening of long index names for ActiveRecord < 7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Feb 16, 2024
1 parent ee4005e commit 4b85933
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## master (unreleased)

- Fix shortening of long index names for ActiveRecord < 7.1

## 0.14.0 (2024-02-01)

- Add ability to configure whether background migrations should be run inline
Expand Down
3 changes: 2 additions & 1 deletion lib/online_migrations/change_column_type_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ def __copy_indexes(table_name, from_column, to_column)
options[:opclass] = opclasses
end

add_index(table_name, new_columns, **options.merge(algorithm: :concurrently))
index_name = Utils.index_name(table_name, new_columns)
add_index(table_name, new_columns, **options.merge(algorithm: :concurrently, name: index_name))
end
end

Expand Down
18 changes: 1 addition & 17 deletions lib/online_migrations/schema_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def add_index(table_name, column_name, **options)

# Rewrite this with `IndexDefinition#defined_for?` when Active Record >= 7.1 is supported.
# See https://github.com/rails/rails/pull/45160.
index = indexes(table_name).find { |i| __index_defined_for?(i, column_name, **options) }
index = indexes(table_name).find { |i| __index_defined_for?(i, column_name, **options.except(:name)) }
if index
schema = __schema_for_table(table_name)

Expand Down Expand Up @@ -751,22 +751,6 @@ def remove_index(table_name, column_name = nil, **options)
end
end

# @private
# From ActiveRecord. Will not be needed for ActiveRecord >= 7.1.
def index_name(table_name, options)
if options.is_a?(Hash)
if options[:column]
Utils.index_name(table_name, options[:column])
elsif options[:name]
options[:name]
else
raise ArgumentError, "You must specify the index name"
end
else
index_name(table_name, column: options)
end
end

# Extends default method to be idempotent.
#
# @see https://edgeapi.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_foreign_key
Expand Down
2 changes: 2 additions & 0 deletions test/schema_statements/changing_column_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ def test_finalize_column_type_change_copies_indexes
end

def test_finalize_column_type_change_copies_indexes_with_long_names
skip if ar_version < 7.1

long_column_name = "a" * (@connection.max_identifier_length - "index_projects_on_".length)

@connection.change_table(:projects) do |t|
Expand Down

0 comments on commit 4b85933

Please sign in to comment.