Skip to content

Commit

Permalink
Cache the connection we use to clean the DB with (Take 2)
Browse files Browse the repository at this point in the history
The connection caching was a little bit too liberal and was caching
across databases (I think). No we just grab it when we enter clean.

Added an iterator so there isn't too much code duplication between
delete and truncate.
  • Loading branch information
johnf authored and bmabey committed Mar 24, 2011
1 parent 9a09d08 commit 9c39306
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
6 changes: 2 additions & 4 deletions lib/database_cleaner/active_record/deletion.rb
Expand Up @@ -53,10 +53,8 @@ module DatabaseCleaner::ActiveRecord
class Deletion < Truncation

def clean
connection.disable_referential_integrity do
tables_to_truncate.each do |table_name|
connection.delete_table table_name
end
each_table do |connection, table_name|
connection.delete_table table_name
end
end

Expand Down
18 changes: 10 additions & 8 deletions lib/database_cleaner/active_record/truncation.rb
Expand Up @@ -103,24 +103,26 @@ class Truncation
include ::DatabaseCleaner::Generic::Truncation

def clean
each_table do |connection, table_name|
connection.truncate_table table_name
end
end

def each_table
connection = connection_klass.connection
connection.disable_referential_integrity do
tables_to_truncate.each do |table_name|
connection.truncate_table table_name
tables_to_truncate(connection).each do |table_name|
yield connection, table_name
end
end
end

private

def tables_to_truncate
def tables_to_truncate(connection)
(@only || connection.tables) - @tables_to_exclude - connection.views
end

def connection
#::ActiveRecord::Base.connection
@connection ||= connection_klass.connection
end

# overwritten
def migration_storage_name
'schema_migrations'
Expand Down

0 comments on commit 9c39306

Please sign in to comment.