提交 5b82f50f 编写于 作者: A Aaron Patterson

Use `table_exists?` from the schema cache.

上级 007965a6
...@@ -762,7 +762,7 @@ def set_sequence_name(value = nil, &block) #:nodoc: ...@@ -762,7 +762,7 @@ def set_sequence_name(value = nil, &block) #:nodoc:
# Indicates whether the table associated with this class exists # Indicates whether the table associated with this class exists
def table_exists? def table_exists?
connection.table_exists?(table_name) connection.schema_cache.table_exists?(table_name)
end end
# Returns an array of column objects for the table associated with this class. # Returns an array of column objects for the table associated with this class.
......
...@@ -42,10 +42,7 @@ def initialize(conn) ...@@ -42,10 +42,7 @@ def initialize(conn)
def table_exists?(name) def table_exists?(name)
return @tables[name] if @tables.key? name return @tables[name] if @tables.key? name
connection.tables.each { |table| @tables[table] = true } @tables[name] = connection.table_exists?(name)
@tables[name] = connection.table_exists?(name) if !@tables.key?(name)
@tables[name]
end end
# Clears out internal caches: # Clears out internal caches:
...@@ -66,6 +63,7 @@ def clear_table_cache!(table_name) ...@@ -66,6 +63,7 @@ def clear_table_cache!(table_name)
@columns_hash.delete table_name @columns_hash.delete table_name
@column_defaults.delete table_name @column_defaults.delete table_name
@primary_keys.delete table_name @primary_keys.delete table_name
@tables.delete table_name
end end
end end
end end
......
...@@ -825,12 +825,12 @@ def test_caching_of_columns ...@@ -825,12 +825,12 @@ def test_caching_of_columns
# clear cache possibly created by other tests # clear cache possibly created by other tests
david.projects.reset_column_information david.projects.reset_column_information
# One query for columns, one for primary key # One query for columns, one for primary key, one for table existence
assert_queries(2) { david.projects.columns; david.projects.columns } assert_queries(3) { david.projects.columns; david.projects.columns }
## and again to verify that reset_column_information clears the cache correctly ## and again to verify that reset_column_information clears the cache correctly
david.projects.reset_column_information david.projects.reset_column_information
assert_queries(2) { david.projects.columns; david.projects.columns } assert_queries(3) { david.projects.columns; david.projects.columns }
end end
def test_attributes_are_being_set_when_initialized_from_habm_association_with_where_clause def test_attributes_are_being_set_when_initialized_from_habm_association_with_where_clause
......
...@@ -9,6 +9,7 @@ class SessionTest < ActiveRecord::TestCase ...@@ -9,6 +9,7 @@ class SessionTest < ActiveRecord::TestCase
def setup def setup
super super
ActiveRecord::Base.connection.schema_cache.clear!
Session.drop_table! if Session.table_exists? Session.drop_table! if Session.table_exists?
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册