Skip to content

Commit

Permalink
make caching of tables to be truncated optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Otte-Witte committed Jul 17, 2013
1 parent 5dc784a commit f928b6d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Expand Up @@ -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))
* `: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

Expand Down
7 changes: 6 additions & 1 deletion lib/database_cleaner/active_record/truncation.rb
Expand Up @@ -239,14 +239,19 @@ def clean
private

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

# overwritten
def migration_storage_names
[::ActiveRecord::Migrator.schema_migrations_table_name]
end

def cache_tables?
!!@cache_tables
end

def pre_count?
@pre_count == true
end
Expand Down
5 changes: 3 additions & 2 deletions lib/database_cleaner/generic/truncation.rb
Expand Up @@ -2,8 +2,8 @@ module DatabaseCleaner
module Generic
module Truncation
def initialize(opts={})
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :reset_ids]).empty?
raise ArgumentError, "The only valid options are :only, :except, :pre_count or :reset_ids. You specified #{opts.keys.join(',')}."
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, :reset_ids or :cache_tables. You specified #{opts.keys.join(',')}."
end
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?"
Expand All @@ -14,6 +14,7 @@ def initialize(opts={})
@tables_to_exclude += migration_storage_names
@pre_count = opts[:pre_count]
@reset_ids = opts[:reset_ids]
@cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
end

def start
Expand Down
18 changes: 18 additions & 0 deletions spec/database_cleaner/active_record/truncation_spec.rb
Expand Up @@ -111,6 +111,24 @@ module ActiveRecord
subject.clean
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

describe '#pre_count?' do
Expand Down

0 comments on commit f928b6d

Please sign in to comment.