Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple indexes on the same column when loading the schema #26019

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class TableDefinition

def initialize(name, temporary = false, options = nil, as = nil, comment: nil)
@columns_hash = {}
@indexes = {}
@indexes = []
@foreign_keys = []
@primary_keys = nil
@temporary = temporary
Expand Down Expand Up @@ -328,7 +328,7 @@ def remove_column(name)
#
# index(:account_id, name: 'index_projects_on_account_id')
def index(column_name, options = {})
indexes[column_name] = options
indexes << [column_name, options]
end

def foreign_key(table_name, options = {}) # :nodoc:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def create_table(table_name, comment: nil, **options)
result = execute schema_creation.accept td

unless supports_indexes_in_create?
td.indexes.each_pair do |column_name, index_options|
td.indexes.each do |column_name, index_options|
add_index(table_name, column_name, index_options)
end
end
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/ar_schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ActiveRecordSchemaTest < ActiveRecord::TestCase
@connection.drop_table :nep_fruits rescue nil
@connection.drop_table :nep_schema_migrations rescue nil
@connection.drop_table :has_timestamps rescue nil
@connection.drop_table :multiple_indexes rescue nil
ActiveRecord::SchemaMigration.delete_all rescue nil
ActiveRecord::Migration.verbose = @original_verbose
end
Expand Down Expand Up @@ -93,6 +94,21 @@ def test_normalize_version
assert_equal "20131219224947", ActiveRecord::SchemaMigration.normalize_migration_number("20131219224947")
end

def test_schema_load_with_multiple_indexes_for_column_of_different_names
ActiveRecord::Schema.define do
create_table :multiple_indexes do |t|
t.string "foo"
t.index ["foo"], name: "multiple_indexes_foo_1"
t.index ["foo"], name: "multiple_indexes_foo_2"
end
end

indexes = @connection.indexes("multiple_indexes")

assert_equal 2, indexes.length
assert_equal ["multiple_indexes_foo_1", "multiple_indexes_foo_2"], indexes.collect(&:name).sort
end

def test_timestamps_without_null_set_null_to_false_on_create_table
ActiveRecord::Schema.define do
create_table :has_timestamps do |t|
Expand Down