Permalink
Browse files

safely publish columns and columns hash info

  • Loading branch information...
1 parent 258f1e5 commit a0dfd84440f28d2862b7eb7ea340ca28d98fb23f @tenderlove tenderlove committed Mar 15, 2013
@@ -90,7 +90,7 @@ def get_primary_key(base_name) #:nodoc:
base_name.foreign_key
else
if ActiveRecord::Base != self && table_exists?
- connection.schema_cache.primary_keys[table_name]
+ connection.schema_cache.primary_keys(table_name)
else
'id'
end
@@ -1,7 +1,7 @@
module ActiveRecord
module ConnectionAdapters
class SchemaCache
- attr_reader :primary_keys, :tables, :version
+ attr_reader :tables, :version
attr_accessor :connection
def initialize(conn)
@@ -14,6 +14,14 @@ def initialize(conn)
prepare_default_proc
end
+ def primary_keys(table_name = nil)
+ if table_name
+ @primary_keys[table_name]
+ else
+ @primary_keys.dup
+ end
+ end
+
# A cached lookup for table existence.
def table_exists?(name)
return @tables[name] if @tables.key? name
@@ -35,7 +43,7 @@ def columns(table = nil)
if table
@columns[table]
else
- @columns
+ @columns.dup
end
end
@@ -45,7 +53,7 @@ def columns_hash(table = nil)
if table
@columns_hash[table]
else
- @columns_hash
+ @columns_hash.dup
end
end
@@ -89,7 +97,7 @@ def prepare_default_proc
end
@columns_hash.default_proc = Proc.new do |h, table_name|
- h[table_name] = Hash[columns[table_name].map { |col|
+ h[table_name] = Hash[columns(table_name).map { |col|
[col.name, col]
}]
end
@@ -205,7 +205,7 @@ def table_exists?
# Returns an array of column objects for the table associated with this class.
def columns
- @columns ||= connection.schema_cache.columns[table_name].map do |col|
+ @columns ||= connection.schema_cache.columns(table_name).map do |col|
col = col.dup
col.primary = (col.name == primary_key)
col
@@ -9,28 +9,28 @@ def setup
end
def test_primary_key
- assert_equal 'id', @cache.primary_keys['posts']
+ assert_equal 'id', @cache.primary_keys('posts')
end
def test_primary_key_for_non_existent_table
- assert_nil @cache.primary_keys['omgponies']
+ assert_nil @cache.primary_keys('omgponies')
end
def test_caches_columns
- columns = @cache.columns['posts']
- assert_equal columns, @cache.columns['posts']
+ columns = @cache.columns('posts')
+ assert_equal columns, @cache.columns('posts')
end
def test_caches_columns_hash
- columns_hash = @cache.columns_hash['posts']
- assert_equal columns_hash, @cache.columns_hash['posts']
+ columns_hash = @cache.columns_hash('posts')
+ assert_equal columns_hash, @cache.columns_hash('posts')
end
def test_clearing
@cache.columns['posts']
@cache.columns_hash['posts']
@cache.tables['posts']
- @cache.primary_keys['posts']
+ @cache.primary_keys('posts')
@cache.clear!
@@ -44,14 +44,14 @@ def test_dump_and_load
@cache.columns['posts']
@cache.columns_hash['posts']
@cache.tables['posts']
- @cache.primary_keys['posts']
+ @cache.primary_keys('posts')
@cache = Marshal.load(Marshal.dump(@cache))
assert_equal 12, @cache.columns['posts'].size
assert_equal 12, @cache.columns_hash['posts'].size
assert @cache.tables['posts']
- assert_equal 'id', @cache.primary_keys['posts']
+ assert_equal 'id', @cache.primary_keys('posts')
end
end

0 comments on commit a0dfd84

Please sign in to comment.