提交 ffed7acd 编写于 作者: Y Yves Senn

Merge pull request #14143 from derekprior/dp-compound-index-ordering

Use type column first in multi-column indexes

Conflicts:
	activerecord/CHANGELOG.md
* Use type column first in multi-column indexes created with `add-reference`.
*Derek Prior*
* Fix `Relation.rewhere` to work with Range values.
*Dan Olson*
......
......@@ -312,7 +312,7 @@ def references(*args)
args.each do |col|
column("#{col}_id", type, options)
column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
index(polymorphic ? %w(id type).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
index(polymorphic ? %w(type id).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
end
end
alias :belongs_to :references
......
......@@ -629,7 +629,7 @@ def add_reference(table_name, ref_name, options = {})
type = options.delete(:type) || :integer
add_column(table_name, "#{ref_name}_id", type, options)
add_column(table_name, "#{ref_name}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
add_index(table_name, polymorphic ? %w[id type].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
add_index(table_name, polymorphic ? %w[type id].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
end
alias :add_belongs_to :add_reference
......
......@@ -55,7 +55,7 @@ def test_creates_polymorphic_index
t.references :foo, :polymorphic => true, :index => true
end
assert connection.index_exists?(table_name, [:foo_id, :foo_type], :name => :index_testings_on_foo_id_and_foo_type)
assert connection.index_exists?(table_name, [:foo_type, :foo_id], name: :index_testings_on_foo_type_and_foo_id)
end
end
......@@ -93,7 +93,7 @@ def test_creates_polymorphic_index_for_existing_table
t.references :foo, :polymorphic => true, :index => true
end
assert connection.index_exists?(table_name, [:foo_id, :foo_type], :name => :index_testings_on_foo_id_and_foo_type)
assert connection.index_exists?(table_name, [:foo_type, :foo_id], name: :index_testings_on_foo_type_and_foo_id)
end
end
end
......
......@@ -42,7 +42,7 @@ def test_does_not_create_reference_id_index
def test_creates_polymorphic_index
add_reference table_name, :taggable, polymorphic: true, index: true
assert index_exists?(table_name, [:taggable_id, :taggable_type])
assert index_exists?(table_name, [:taggable_type, :taggable_id])
end
def test_creates_reference_type_column_with_default
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册