diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 58d23d2f784dd67f4b8f43590e71c8c499a7c652..ba75dc6d0989882094eda244a39e3b8bb8ae131c 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -762,7 +762,7 @@ def set_sequence_name(value = nil, &block) #:nodoc: # Indicates whether the table associated with this class exists def table_exists? - connection.table_exists?(table_name) + connection.schema_cache.table_exists?(table_name) end # Returns an array of column objects for the table associated with this class. diff --git a/activerecord/lib/active_record/connection_adapters/schema_cache.rb b/activerecord/lib/active_record/connection_adapters/schema_cache.rb index b14b37ce89dd265e92c2df5b15263884edcc0b2d..bee03abd4405fa41ada7629348775ef54a1302ea 100644 --- a/activerecord/lib/active_record/connection_adapters/schema_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/schema_cache.rb @@ -42,10 +42,7 @@ def initialize(conn) def table_exists?(name) return @tables[name] if @tables.key? name - connection.tables.each { |table| @tables[table] = true } - @tables[name] = connection.table_exists?(name) if !@tables.key?(name) - - @tables[name] + @tables[name] = connection.table_exists?(name) end # Clears out internal caches: @@ -66,6 +63,7 @@ def clear_table_cache!(table_name) @columns_hash.delete table_name @column_defaults.delete table_name @primary_keys.delete table_name + @tables.delete table_name end end end 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 b49510b2020a502ae3a6d0ead8464a31ab21b94e..745f7132e79677655f440f893c1790a2381491a9 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 @@ -825,12 +825,12 @@ def test_caching_of_columns # clear cache possibly created by other tests david.projects.reset_column_information - # One query for columns, one for primary key - assert_queries(2) { david.projects.columns; david.projects.columns } + # One query for columns, one for primary key, one for table existence + assert_queries(3) { 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(2) { david.projects.columns; david.projects.columns } + assert_queries(3) { 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/session_store/session_test.rb b/activerecord/test/cases/session_store/session_test.rb index 258cee7abac25c2ded0ba1bfa8ae4ed6cd59524b..c206e3de4ac2db8041be3e9eb6a6803a8fe7c4a3 100644 --- a/activerecord/test/cases/session_store/session_test.rb +++ b/activerecord/test/cases/session_store/session_test.rb @@ -9,6 +9,7 @@ class SessionTest < ActiveRecord::TestCase def setup super + ActiveRecord::Base.connection.schema_cache.clear! Session.drop_table! if Session.table_exists? end