make caching of tables to be truncated optional

This commit is contained in:
Marco Otte-Witte 2013-07-17 11:43:00 +02:00
parent 5dc784af66
commit f928b6dff8
4 changed files with 31 additions and 3 deletions

View file

@ -181,6 +181,10 @@ The following options are available for ActiveRecord's `:truncation` strategy _o
* `:pre_count` - When set to `true` this will check each table for existing rows before truncating it. This can speed up test suites when many of the tables to be truncated are never populated. Defaults to `:false`. (Also, see the section on [What strategy is fastest?](#what-strategy-is-fastest)) * `:pre_count` - When set to `true` this will check each table for existing rows before truncating it. This can speed up test suites when many of the tables to be truncated are never populated. Defaults to `:false`. (Also, see the section on [What strategy is fastest?](#what-strategy-is-fastest))
* `:reset_ids` - This only matters when `:pre_count` is used, and it will make sure that a tables auto-incrementing id is reset even if there are no rows in the table (e.g. records were created in the test but also removed before DatabaseCleaner gets to it). Defaults to `true`. * `:reset_ids` - This only matters when `:pre_count` is used, and it will make sure that a tables auto-incrementing id is reset even if there are no rows in the table (e.g. records were created in the test but also removed before DatabaseCleaner gets to it). Defaults to `true`.
The following option is available for ActiveRecord's `:truncation` and `:deletion` strategy for any DB.
* `:cache_tables` - When set to `true` the list of tables to truncate or delete from will only be read from the DB once, otherwise it will be read before each cleanup run. Set this to `false` if you create and drop tables in your tests. Defaults to `true`.
### RSpec Example ### RSpec Example

View file

@ -239,7 +239,8 @@ module DatabaseCleaner::ActiveRecord
private private
def tables_to_truncate(connection) def tables_to_truncate(connection)
(@only || connection.database_cleaner_table_cache) - @tables_to_exclude - connection.database_cleaner_view_cache tables_in_db = cache_tables? ? connection.database_cleaner_table_cache : connection.tables
(@only || tables_in_db) - @tables_to_exclude - connection.database_cleaner_view_cache
end end
# overwritten # overwritten
@ -247,6 +248,10 @@ module DatabaseCleaner::ActiveRecord
[::ActiveRecord::Migrator.schema_migrations_table_name] [::ActiveRecord::Migrator.schema_migrations_table_name]
end end
def cache_tables?
!!@cache_tables
end
def pre_count? def pre_count?
@pre_count == true @pre_count == true
end end

View file

@ -2,8 +2,8 @@ module DatabaseCleaner
module Generic module Generic
module Truncation module Truncation
def initialize(opts={}) def initialize(opts={})
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :reset_ids]).empty? if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :reset_ids, :cache_tables]).empty?
raise ArgumentError, "The only valid options are :only, :except, :pre_count or :reset_ids. You specified #{opts.keys.join(',')}." raise ArgumentError, "The only valid options are :only, :except, :pre_count, :reset_ids or :cache_tables. You specified #{opts.keys.join(',')}."
end end
if opts.has_key?(:only) && opts.has_key?(:except) if opts.has_key?(:only) && opts.has_key?(:except)
raise ArgumentError, "You may only specify either :only or :except. Doing both doesn't really make sense does it?" raise ArgumentError, "You may only specify either :only or :except. Doing both doesn't really make sense does it?"
@ -14,6 +14,7 @@ module DatabaseCleaner
@tables_to_exclude += migration_storage_names @tables_to_exclude += migration_storage_names
@pre_count = opts[:pre_count] @pre_count = opts[:pre_count]
@reset_ids = opts[:reset_ids] @reset_ids = opts[:reset_ids]
@cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
end end
def start def start

View file

@ -111,6 +111,24 @@ module DatabaseCleaner
subject.clean subject.clean
end end
end end
context 'when :cache_tables is set to true' do
it 'caches the list of tables to be truncated' do
connection.should_receive(:database_cleaner_table_cache)
connection.stub!(:truncate_tables)
Truncation.new({ :cache_tables => true }).clean
end
end
context 'when :cache_tables is set to false' do
it 'does not cache the list of tables to be truncated' do
connection.should_not_receive(:database_cleaner_table_cache)
connection.stub!(:truncate_tables)
Truncation.new({ :cache_tables => false }).clean
end
end
end end
describe '#pre_count?' do describe '#pre_count?' do