Skip to content
This repository
Browse code

hide more data in the schema cache

  • Loading branch information...
commit 8d3e5c8c5200880a66abef3fd2f45104e65732e9 1 parent a0dfd84
Aaron Patterson authored March 14, 2013
1  Gemfile
@@ -8,6 +8,7 @@ gem 'bcrypt-ruby', '~> 3.0.0'
8 8
 gem 'jquery-rails', '~> 2.2.0'
9 9
 gem 'turbolinks'
10 10
 gem 'coffee-rails', '~> 4.0.0.beta1'
  11
+gem 'arel', :path => '/Users/aaron/git/arel'
11 12
 
12 13
 # This needs to be with require false to avoid
13 14
 # it being automatically loaded by sprockets
2  activerecord/lib/active_record/associations/association_scope.rb
@@ -22,7 +22,7 @@ def scope
22 22
       private
23 23
 
24 24
       def column_for(table_name, column_name)
25  
-        columns = alias_tracker.connection.schema_cache.columns_hash[table_name]
  25
+        columns = alias_tracker.connection.schema_cache.columns_hash(table_name)
26 26
         columns[column_name]
27 27
       end
28 28
 
22  activerecord/lib/active_record/connection_adapters/schema_cache.rb
... ...
@@ -1,7 +1,9 @@
  1
+require 'active_support/deprecation/reporting'
  2
+
1 3
 module ActiveRecord
2 4
   module ConnectionAdapters
3 5
     class SchemaCache
4  
-      attr_reader :tables, :version
  6
+      attr_reader :version
5 7
       attr_accessor :connection
6 8
 
7 9
       def initialize(conn)
@@ -18,6 +20,7 @@ def primary_keys(table_name = nil)
18 20
         if table_name
19 21
           @primary_keys[table_name]
20 22
         else
  23
+          ActiveSupport::Deprecation.warn('call primary_keys with a table name!')
21 24
           @primary_keys.dup
22 25
         end
23 26
       end
@@ -38,11 +41,21 @@ def add(table_name)
38 41
         end
39 42
       end
40 43
 
  44
+      def tables(name = nil)
  45
+        if name
  46
+          @tables[name]
  47
+        else
  48
+          ActiveSupport::Deprecation.warn('call tables with a name!')
  49
+          @tables.dup
  50
+        end
  51
+      end
  52
+
41 53
       # Get the columns for a table
42 54
       def columns(table = nil)
43 55
         if table
44 56
           @columns[table]
45 57
         else
  58
+          ActiveSupport::Deprecation.warn('call columns with a table name!')
46 59
           @columns.dup
47 60
         end
48 61
       end
@@ -53,6 +66,7 @@ def columns_hash(table = nil)
53 66
         if table
54 67
           @columns_hash[table]
55 68
         else
  69
+          ActiveSupport::Deprecation.warn('call columns_hash with a table name!')
56 70
           @columns_hash.dup
57 71
         end
58 72
       end
@@ -66,6 +80,12 @@ def clear!
66 80
         @version = nil
67 81
       end
68 82
 
  83
+      def size
  84
+        [@columns, @columns_hash, @primary_keys, @tables].map { |x|
  85
+          x.size
  86
+        }.inject :+
  87
+      end
  88
+
69 89
       # Clear out internal caches for table with +table_name+.
70 90
       def clear_table_cache!(table_name)
71 91
         @columns.delete table_name
4  activerecord/test/cases/base_test.rb
@@ -1358,9 +1358,9 @@ def test_compute_type_argument_error
1358 1358
 
1359 1359
   def test_clear_cache!
1360 1360
     # preheat cache
1361  
-    c1 = Post.connection.schema_cache.columns['posts']
  1361
+    c1 = Post.connection.schema_cache.columns('posts')
1362 1362
     ActiveRecord::Base.clear_cache!
1363  
-    c2 = Post.connection.schema_cache.columns['posts']
  1363
+    c2 = Post.connection.schema_cache.columns('posts')
1364 1364
     assert_not_equal c1, c2
1365 1365
   end
1366 1366
 
23  activerecord/test/cases/connection_adapters/schema_cache_test.rb
@@ -27,30 +27,27 @@ def test_caches_columns_hash
27 27
       end
28 28
 
29 29
       def test_clearing
30  
-        @cache.columns['posts']
31  
-        @cache.columns_hash['posts']
32  
-        @cache.tables['posts']
  30
+        @cache.columns('posts')
  31
+        @cache.columns_hash('posts')
  32
+        @cache.tables('posts')
33 33
         @cache.primary_keys('posts')
34 34
 
35 35
         @cache.clear!
36 36
 
37  
-        assert_equal 0, @cache.columns.size
38  
-        assert_equal 0, @cache.columns_hash.size
39  
-        assert_equal 0, @cache.tables.size
40  
-        assert_equal 0, @cache.primary_keys.size
  37
+        assert_equal 0, @cache.size
41 38
       end
42 39
 
43 40
       def test_dump_and_load
44  
-        @cache.columns['posts']
45  
-        @cache.columns_hash['posts']
46  
-        @cache.tables['posts']
  41
+        @cache.columns('posts')
  42
+        @cache.columns_hash('posts')
  43
+        @cache.tables('posts')
47 44
         @cache.primary_keys('posts')
48 45
 
49 46
         @cache = Marshal.load(Marshal.dump(@cache))
50 47
 
51  
-        assert_equal 12, @cache.columns['posts'].size
52  
-        assert_equal 12, @cache.columns_hash['posts'].size
53  
-        assert @cache.tables['posts']
  48
+        assert_equal 12, @cache.columns('posts').size
  49
+        assert_equal 12, @cache.columns_hash('posts').size
  50
+        assert @cache.tables('posts')
54 51
         assert_equal 'id', @cache.primary_keys('posts')
55 52
       end
56 53
 

0 notes on commit 8d3e5c8

Please sign in to comment.
Something went wrong with that request. Please try again.