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) == 0.8.x (in git)
* ActiveRecord truncation strategy caches the list of tables #130 (Petteri Räty)
== 0.8.0 2012-06-02 == 0.8.0 2012-06-02
* Support for Mongoid 3/Moped (Andrew Bennett) * 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 [] @views ||= select_values("select table_name from information_schema.views where table_schema = '#{current_database}'") rescue []
end 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) def truncate_table(table_name)
raise NotImplementedError raise NotImplementedError
end end
@ -137,7 +142,7 @@ module DatabaseCleaner::ActiveRecord
private private
def tables_to_truncate(connection) 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 end
# overwritten # overwritten

View file

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