Making the mysql deletion cleaner to touch only those tables that have new records in them

This commit is contained in:
Nikolay Nemshilov 2014-04-08 17:24:24 +10:00
parent 35bd646903
commit 8d0016e748

View file

@ -54,3 +54,17 @@ module DatabaseCleaner::ActiveRecord
end
end
end
if defined?(Mysql2Adapter)
class DatabaseCleaner::ActiveRecord::Deletion
def tables_to_truncate(connection)
(@only || tables_with_new_rows(connection)) - @tables_to_exclude
end
def tables_with_new_rows(connection)
@db_name ||= connection.instance_variable_get('@config')[:database]
result = connection.execute("SELECT table_name FROM information_schema.tables WHERE table_schema = '#{@db_name}' AND table_rows > 0")
result.map{ |row| row[0] } - ['schema_migrations']
end
end
end