From 0b08ff7d92124cc370e9f0795d1559204f04f9a4 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 16 Dec 2011 11:46:34 +0000 Subject: Cache columns at the model level. Allows two models to use the same table but have different primary keys. --- .../has_and_belongs_to_many_associations_test.rb | 4 +-- activerecord/test/cases/base_test.rb | 4 +-- .../cases/connection_adapters/schema_cache_test.rb | 9 ----- activerecord/test/cases/primary_keys_test.rb | 38 +++++++++++++++++----- 4 files changed, 34 insertions(+), 21 deletions(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 745f7132e7..bc6ebb076e 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -826,11 +826,11 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase david.projects.reset_column_information # One query for columns, one for primary key, one for table existence - assert_queries(3) { david.projects.columns; david.projects.columns } + assert_queries(1) { david.projects.columns; david.projects.columns } ## and again to verify that reset_column_information clears the cache correctly david.projects.reset_column_information - assert_queries(3) { david.projects.columns; david.projects.columns } + assert_queries(1) { david.projects.columns; david.projects.columns } end def test_attributes_are_being_set_when_initialized_from_habm_association_with_where_clause diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 6b24364216..a034dd7e3a 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1963,9 +1963,9 @@ class BasicsTest < ActiveRecord::TestCase def test_clear_cache! # preheat cache - c1 = Post.columns + c1 = Post.connection.schema_cache.columns['posts'] ActiveRecord::Base.clear_cache! - c2 = Post.columns + c2 = Post.connection.schema_cache.columns['posts'] assert_not_equal c1, c2 end diff --git a/activerecord/test/cases/connection_adapters/schema_cache_test.rb b/activerecord/test/cases/connection_adapters/schema_cache_test.rb index d60de54aed..d5ae08cd58 100644 --- a/activerecord/test/cases/connection_adapters/schema_cache_test.rb +++ b/activerecord/test/cases/connection_adapters/schema_cache_test.rb @@ -16,15 +16,6 @@ module ActiveRecord assert_equal 'id', @cache.primary_keys['omgponies'] end - def test_primary_key_is_set_on_columns - posts_columns = @cache.columns_hash['posts'] - assert posts_columns['id'].primary - - (posts_columns.keys - ['id']).each do |key| - assert !posts_columns[key].primary - end - end - def test_caches_columns columns = @cache.columns['posts'] assert_equal columns, @cache.columns['posts'] diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index a02dcb9e1c..0669707baf 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -149,14 +149,36 @@ class PrimaryKeysTest < ActiveRecord::TestCase assert_equal k.connection.quote_column_name("foo"), k.quoted_primary_key end - def test_set_primary_key_sets_schema_cache - klass = Class.new(ActiveRecord::Base) - klass.table_name = 'fuuuuuu' - klass.connection.create_table(:fuuuuuu, :id => false) { |t| t.integer :omg } - klass.primary_key = 'omg' - assert klass.connection.schema_cache.columns_hash['fuuuuuu']['omg'].primary - ensure - klass.connection.drop_table(:fuuuuuu) if klass.table_exists? + 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 end -- cgit v1.2.3