added truncation strategy for AR. improved error messaging

This commit is contained in:
Ben Mabey 2009-03-04 13:18:13 -07:00
parent 5e8df327d8
commit 55bc444994
3 changed files with 56 additions and 1 deletions

View file

@ -13,3 +13,4 @@ Feature: database cleaning
Examples:
| ORM | Strategy |
| ActiveRecord | transaction |
| ActiveRecord | truncation |

View file

@ -0,0 +1,47 @@
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter
def truncate_tabe(table_name)
execute("TRUNCATE TABLE #{table_name};")
end
end
class SQLite3Adapter
def truncate_table(table_name)
execute("DELETE FROM #{table_name};")
end
end
end
end
module DatabaseCleaner::ActiveRecord
class Truncation
def start
# no-op
end
def clean
connection.disable_referential_integrity do
(connection.tables - %w{schema_migrations}).each do |table_name|
connection.truncate_table table_name
end
end
end
private
def connection
ActiveRecord::Base.connection
end
end
end

View file

@ -4,6 +4,13 @@ module DatabaseCleaner
class NoORMDetected < StandardError; end
class UnknownStrategySpecified < ArgumentError; end
module ActiveRecord
def self.available_strategies
['truncation', 'transaction']
end
end
class << self
def strategy=(args)
strategy, *strategy_args = args
@ -37,7 +44,7 @@ module DatabaseCleaner
require "database_cleaner/#{orm}/#{strategy}"
orm_module.const_get(strategy.to_s.capitalize)
rescue LoadError => e
raise UnknownStrategySpecified, "foo"
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
end