Memoize tables for truncation

As explained in issue #130 active record connections don't do caching of
schema information. This meant that database_cleaner did a query every
with the truncation strategy for every clean to find out the tables. Now
we cache the information. If someone needs cache invalidation then it
can be added. As we already did view caching this is consistent between
views and tables.
This commit is contained in:
Petteri Räty 2012-08-01 14:04:37 +03:00
parent 3c61caeecb
commit ee83a3b295
3 changed files with 12 additions and 5 deletions

View file

@ -1,5 +1,7 @@
== 0.8.x (in git)
* ActiveRecord truncation strategy caches the list of tables #130 (Petteri Räty)
== 0.8.0 2012-06-02
* Support for Mongoid 3/Moped (Andrew Bennett)

View file

@ -13,6 +13,11 @@ module ActiveRecord
@views ||= select_values("select table_name from information_schema.views where table_schema = '#{current_database}'") rescue []
end
def database_cleaner_table_cache
# the adapters don't do caching (#130) but we make the assumption that the list stays the same in tests
@database_cleaner_tables ||= tables
end
def truncate_table(table_name)
raise NotImplementedError
end
@ -137,7 +142,7 @@ module DatabaseCleaner::ActiveRecord
private
def tables_to_truncate(connection)
(@only || connection.tables) - @tables_to_exclude - connection.views
(@only || connection.database_cleaner_table_cache) - @tables_to_exclude - connection.views
end
# overwritten

View file

@ -30,14 +30,14 @@ module DatabaseCleaner
end
it "should truncate all tables except for schema_migrations" do
connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
connection.stub!(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs])
connection.should_receive(:truncate_tables).with(['widgets', 'dogs'])
Truncation.new.clean
end
it "should only truncate the tables specified in the :only option when provided" do
connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
connection.stub!(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs])
connection.should_receive(:truncate_tables).with(['widgets'])
@ -45,7 +45,7 @@ module DatabaseCleaner
end
it "should not truncate the tables specified in the :except option" do
connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
connection.stub!(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs])
connection.should_receive(:truncate_tables).with(['dogs'])
@ -63,7 +63,7 @@ module DatabaseCleaner
end
it "should not truncate views" do
connection.stub!(:tables).and_return(%w[widgets dogs])
connection.stub!(:database_cleaner_table_cache).and_return(%w[widgets dogs])
connection.stub!(:views).and_return(["widgets"])
connection.should_receive(:truncate_tables).with(['dogs'])