diff --git a/History.txt b/History.txt index da15194..9efe9a9 100644 --- a/History.txt +++ b/History.txt @@ -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) diff --git a/lib/database_cleaner/active_record/truncation.rb b/lib/database_cleaner/active_record/truncation.rb index aa492f1..3c62d1d 100755 --- a/lib/database_cleaner/active_record/truncation.rb +++ b/lib/database_cleaner/active_record/truncation.rb @@ -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 diff --git a/spec/database_cleaner/active_record/truncation_spec.rb b/spec/database_cleaner/active_record/truncation_spec.rb index 71c1587..0ad7895 100644 --- a/spec/database_cleaner/active_record/truncation_spec.rb +++ b/spec/database_cleaner/active_record/truncation_spec.rb @@ -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'])