Merge pull request #166 from betelgeuse/faster_deletion_stragegy

One network trip for the deletion strategy
This commit is contained in:
Ben Mabey 2012-12-06 05:31:21 -08:00
commit 480fbebbbd

View file

@ -3,51 +3,6 @@ require 'active_record/connection_adapters/abstract_adapter'
require "database_cleaner/generic/truncation"
require 'database_cleaner/active_record/base'
require 'database_cleaner/active_record/truncation'
# This file may seem to have duplication with that of truncation, but by keeping them separate
# we avoiding loading this code when it is not being used (which is the common case).
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter < MYSQL_ADAPTER_PARENT
def delete_table(table_name)
execute("DELETE FROM #{quote_table_name(table_name)};")
end
end
class Mysql2Adapter < MYSQL2_ADAPTER_PARENT
def delete_table(table_name)
execute("DELETE FROM #{quote_table_name(table_name)};")
end
end
class JdbcAdapter < AbstractAdapter
def delete_table(table_name)
execute("DELETE FROM #{quote_table_name(table_name)};")
end
end
class PostgreSQLAdapter < POSTGRE_ADAPTER_PARENT
def delete_table(table_name)
execute("DELETE FROM #{quote_table_name(table_name)};")
end
end
class SQLServerAdapter < AbstractAdapter
def delete_table(table_name)
execute("DELETE FROM #{quote_table_name(table_name)};")
end
end
class OracleEnhancedAdapter < AbstractAdapter
def delete_table(table_name)
execute("DELETE FROM #{quote_table_name(table_name)}")
end
end
end
end
module DatabaseCleaner::ActiveRecord
class Deletion < Truncation
@ -55,9 +10,11 @@ module DatabaseCleaner::ActiveRecord
def clean
connection = connection_class.connection
connection.disable_referential_integrity do
tables_to_truncate(connection).each do |table_name|
connection.delete_table table_name
end
sql = tables_to_truncate(connection).map do |table_name|
"DELETE FROM #{connection.quote_table_name(table_name)}"
end.join(";")
connection.execute sql
end
end