Permalink
Browse files

hide more data in the schema cache

  • Loading branch information...
1 parent a0dfd84 commit 8d3e5c8c5200880a66abef3fd2f45104e65732e9 @tenderlove tenderlove committed Mar 15, 2013
View
1 Gemfile
@@ -8,6 +8,7 @@ gem 'bcrypt-ruby', '~> 3.0.0'
gem 'jquery-rails', '~> 2.2.0'
gem 'turbolinks'
gem 'coffee-rails', '~> 4.0.0.beta1'
+gem 'arel', :path => '/Users/aaron/git/arel'
# This needs to be with require false to avoid
# it being automatically loaded by sprockets
View
2 activerecord/lib/active_record/associations/association_scope.rb
@@ -22,7 +22,7 @@ def scope
private
def column_for(table_name, column_name)
- columns = alias_tracker.connection.schema_cache.columns_hash[table_name]
+ columns = alias_tracker.connection.schema_cache.columns_hash(table_name)
columns[column_name]
end
View
22 activerecord/lib/active_record/connection_adapters/schema_cache.rb
@@ -1,7 +1,9 @@
+require 'active_support/deprecation/reporting'
+
module ActiveRecord
module ConnectionAdapters
class SchemaCache
- attr_reader :tables, :version
+ attr_reader :version
attr_accessor :connection
def initialize(conn)
@@ -18,6 +20,7 @@ def primary_keys(table_name = nil)
if table_name
@primary_keys[table_name]
else
+ ActiveSupport::Deprecation.warn('call primary_keys with a table name!')
@primary_keys.dup
end
end
@@ -38,11 +41,21 @@ def add(table_name)
end
end
+ def tables(name = nil)
+ if name
+ @tables[name]
+ else
+ ActiveSupport::Deprecation.warn('call tables with a name!')
+ @tables.dup
+ end
+ end
+
# Get the columns for a table
def columns(table = nil)
if table
@columns[table]
else
+ ActiveSupport::Deprecation.warn('call columns with a table name!')
@columns.dup
end
end
@@ -53,6 +66,7 @@ def columns_hash(table = nil)
if table
@columns_hash[table]
else
+ ActiveSupport::Deprecation.warn('call columns_hash with a table name!')
@columns_hash.dup
end
end
@@ -66,6 +80,12 @@ def clear!
@version = nil
end
+ def size
+ [@columns, @columns_hash, @primary_keys, @tables].map { |x|
+ x.size
+ }.inject :+
+ end
+
# Clear out internal caches for table with +table_name+.
def clear_table_cache!(table_name)
@columns.delete table_name
View
4 activerecord/test/cases/base_test.rb
@@ -1358,9 +1358,9 @@ def test_compute_type_argument_error
def test_clear_cache!
# preheat cache
- c1 = Post.connection.schema_cache.columns['posts']
+ c1 = Post.connection.schema_cache.columns('posts')
ActiveRecord::Base.clear_cache!
- c2 = Post.connection.schema_cache.columns['posts']
+ c2 = Post.connection.schema_cache.columns('posts')
assert_not_equal c1, c2
end
View
23 activerecord/test/cases/connection_adapters/schema_cache_test.rb
@@ -27,30 +27,27 @@ def test_caches_columns_hash
end
def test_clearing
- @cache.columns['posts']
- @cache.columns_hash['posts']
- @cache.tables['posts']
+ @cache.columns('posts')
+ @cache.columns_hash('posts')
+ @cache.tables('posts')
@cache.primary_keys('posts')
@cache.clear!
- assert_equal 0, @cache.columns.size
- assert_equal 0, @cache.columns_hash.size
- assert_equal 0, @cache.tables.size
- assert_equal 0, @cache.primary_keys.size
+ assert_equal 0, @cache.size
end
def test_dump_and_load
- @cache.columns['posts']
- @cache.columns_hash['posts']
- @cache.tables['posts']
+ @cache.columns('posts')
+ @cache.columns_hash('posts')
+ @cache.tables('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 12, @cache.columns('posts').size
+ assert_equal 12, @cache.columns_hash('posts').size
+ assert @cache.tables('posts')
assert_equal 'id', @cache.primary_keys('posts')
end

0 comments on commit 8d3e5c8

Please sign in to comment.