mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Remove Column#primary
It appears to have been used at some point in the past, but is no longer used in any meaningful way. Whether a column is considered primary is a property of the model, not the schema/column. This also removes the need for yet another layer of caching of the model's schema, and we can leave that to the schema cache.
This commit is contained in:
parent
b318758bda
commit
05dd3df35d
7 changed files with 10 additions and 52 deletions
|
@ -455,7 +455,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def pk_attribute?(name)
|
def pk_attribute?(name)
|
||||||
column_for_attribute(name).primary
|
name == self.class.primary_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def typecasted_attribute_value(name)
|
def typecasted_attribute_value(name)
|
||||||
|
|
|
@ -14,7 +14,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :name, :default, :cast_type, :null, :sql_type, :default_function
|
attr_reader :name, :default, :cast_type, :null, :sql_type, :default_function
|
||||||
attr_accessor :primary, :coder
|
attr_accessor :coder
|
||||||
|
|
||||||
alias :encoded? :coder
|
alias :encoded? :coder
|
||||||
|
|
||||||
|
@ -36,7 +36,6 @@ module ActiveRecord
|
||||||
@null = null
|
@null = null
|
||||||
@default = extract_default(default)
|
@default = extract_default(default)
|
||||||
@default_function = nil
|
@default_function = nil
|
||||||
@primary = nil
|
|
||||||
@coder = nil
|
@coder = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -219,16 +219,12 @@ module ActiveRecord
|
||||||
|
|
||||||
# Returns an array of column objects for the table associated with this class.
|
# Returns an array of column objects for the table associated with this class.
|
||||||
def columns
|
def columns
|
||||||
@columns ||= connection.schema_cache.columns(table_name).map do |col|
|
connection.schema_cache.columns(table_name)
|
||||||
col = col.dup
|
|
||||||
col.primary = (col.name == primary_key)
|
|
||||||
col
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a hash of column objects for the table associated with this class.
|
# Returns a hash of column objects for the table associated with this class.
|
||||||
def columns_hash
|
def columns_hash
|
||||||
@columns_hash ||= Hash[columns.map { |c| [c.name, c] }]
|
connection.schema_cache.columns_hash(table_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def column_types # :nodoc:
|
def column_types # :nodoc:
|
||||||
|
@ -271,7 +267,7 @@ module ActiveRecord
|
||||||
# Returns an array of column objects where the primary id, all columns ending in "_id" or "_count",
|
# Returns an array of column objects where the primary id, all columns ending in "_id" or "_count",
|
||||||
# and columns used for single table inheritance have been removed.
|
# and columns used for single table inheritance have been removed.
|
||||||
def content_columns
|
def content_columns
|
||||||
@content_columns ||= columns.reject { |c| c.primary || c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
|
@content_columns ||= columns.reject { |c| c.name == primary_key || c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Resets all the cached information about columns, which will cause them
|
# Resets all the cached information about columns, which will cause them
|
||||||
|
@ -308,8 +304,6 @@ module ActiveRecord
|
||||||
@arel_engine = nil
|
@arel_engine = nil
|
||||||
@column_defaults = nil
|
@column_defaults = nil
|
||||||
@column_names = nil
|
@column_names = nil
|
||||||
@columns = nil
|
|
||||||
@columns_hash = nil
|
|
||||||
@column_types = nil
|
@column_types = nil
|
||||||
@content_columns = nil
|
@content_columns = nil
|
||||||
@dynamic_methods_hash = nil
|
@dynamic_methods_hash = nil
|
||||||
|
|
|
@ -60,7 +60,6 @@ class CopyTableTest < ActiveRecord::TestCase
|
||||||
assert_equal original_id.type, copied_id.type
|
assert_equal original_id.type, copied_id.type
|
||||||
assert_equal original_id.sql_type, copied_id.sql_type
|
assert_equal original_id.sql_type, copied_id.sql_type
|
||||||
assert_equal original_id.limit, copied_id.limit
|
assert_equal original_id.limit, copied_id.limit
|
||||||
assert_equal original_id.primary, copied_id.primary
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -102,8 +102,8 @@ class BasicsTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_columns_should_obey_set_primary_key
|
def test_columns_should_obey_set_primary_key
|
||||||
pk = Subscriber.columns.find { |x| x.name == 'nick' }
|
pk = Subscriber.columns_hash[Subscriber.primary_key]
|
||||||
assert pk.primary, 'nick should be primary key'
|
assert_equal 'nick', pk.name, 'nick should be primary key'
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_primary_key_with_no_id
|
def test_primary_key_with_no_id
|
||||||
|
|
|
@ -21,7 +21,7 @@ module ActiveRecord
|
||||||
super
|
super
|
||||||
@connection = ActiveRecord::Base.connection
|
@connection = ActiveRecord::Base.connection
|
||||||
@subscriber = LogListener.new
|
@subscriber = LogListener.new
|
||||||
@pk = Topic.columns.find { |c| c.primary }
|
@pk = Topic.columns_hash[Topic.primary_key]
|
||||||
@subscription = ActiveSupport::Notifications.subscribe('sql.active_record', @subscriber)
|
@subscription = ActiveSupport::Notifications.subscribe('sql.active_record', @subscriber)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -60,12 +60,10 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_logs_bind_vars
|
def test_logs_bind_vars
|
||||||
pk = Topic.columns.find { |x| x.primary }
|
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
:name => 'SQL',
|
:name => 'SQL',
|
||||||
:sql => 'select * from topics where id = ?',
|
:sql => 'select * from topics where id = ?',
|
||||||
:binds => [[pk, 10]]
|
:binds => [[@pk, 10]]
|
||||||
}
|
}
|
||||||
event = ActiveSupport::Notifications::Event.new(
|
event = ActiveSupport::Notifications::Event.new(
|
||||||
'foo',
|
'foo',
|
||||||
|
@ -87,7 +85,7 @@ module ActiveRecord
|
||||||
}.new
|
}.new
|
||||||
|
|
||||||
logger.sql event
|
logger.sql event
|
||||||
assert_match([[pk.name, 10]].inspect, logger.debugs.first)
|
assert_match([[@pk.name, 10]].inspect, logger.debugs.first)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -149,38 +149,6 @@ class PrimaryKeysTest < ActiveRecord::TestCase
|
||||||
assert_equal k.connection.quote_column_name("foo"), k.quoted_primary_key
|
assert_equal k.connection.quote_column_name("foo"), k.quoted_primary_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_two_models_with_same_table_but_different_primary_key
|
|
||||||
k1 = Class.new(ActiveRecord::Base)
|
|
||||||
k1.table_name = 'posts'
|
|
||||||
k1.primary_key = 'id'
|
|
||||||
|
|
||||||
k2 = Class.new(ActiveRecord::Base)
|
|
||||||
k2.table_name = 'posts'
|
|
||||||
k2.primary_key = 'title'
|
|
||||||
|
|
||||||
assert k1.columns.find { |c| c.name == 'id' }.primary
|
|
||||||
assert !k1.columns.find { |c| c.name == 'title' }.primary
|
|
||||||
assert k1.columns_hash['id'].primary
|
|
||||||
assert !k1.columns_hash['title'].primary
|
|
||||||
|
|
||||||
assert !k2.columns.find { |c| c.name == 'id' }.primary
|
|
||||||
assert k2.columns.find { |c| c.name == 'title' }.primary
|
|
||||||
assert !k2.columns_hash['id'].primary
|
|
||||||
assert k2.columns_hash['title'].primary
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_models_with_same_table_have_different_columns
|
|
||||||
k1 = Class.new(ActiveRecord::Base)
|
|
||||||
k1.table_name = 'posts'
|
|
||||||
|
|
||||||
k2 = Class.new(ActiveRecord::Base)
|
|
||||||
k2.table_name = 'posts'
|
|
||||||
|
|
||||||
k1.columns.zip(k2.columns).each do |col1, col2|
|
|
||||||
assert !col1.equal?(col2)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_auto_detect_primary_key_from_schema
|
def test_auto_detect_primary_key_from_schema
|
||||||
MixedCaseMonkey.reset_primary_key
|
MixedCaseMonkey.reset_primary_key
|
||||||
assert_equal "monkeyID", MixedCaseMonkey.primary_key
|
assert_equal "monkeyID", MixedCaseMonkey.primary_key
|
||||||
|
|
Loading…
Reference in a new issue