mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
added truncation strategy for AR. improved error messaging
This commit is contained in:
parent
5e8df327d8
commit
55bc444994
3 changed files with 56 additions and 1 deletions
|
@ -13,3 +13,4 @@ Feature: database cleaning
|
|||
Examples:
|
||||
| ORM | Strategy |
|
||||
| ActiveRecord | transaction |
|
||||
| ActiveRecord | truncation |
|
||||
|
|
47
lib/database_cleaner/active_record/truncation.rb
Normal file
47
lib/database_cleaner/active_record/truncation.rb
Normal 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
|
||||
|
||||
|
|
@ -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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue