add a deletion strategy for sequel

This commit is contained in:
Timothée Peignier 2014-08-13 13:59:09 -07:00
parent e331222c55
commit e769615257
3 changed files with 36 additions and 2 deletions

View file

@ -87,6 +87,7 @@ Gem::Specification.new do |s|
"lib/database_cleaner/redis/base.rb",
"lib/database_cleaner/redis/truncation.rb",
"lib/database_cleaner/sequel/base.rb",
"lib/database_cleaner/sequel/deletion.rb",
"lib/database_cleaner/sequel/transaction.rb",
"lib/database_cleaner/sequel/truncation.rb",
"spec/database_cleaner/active_record/base_spec.rb",

View file

@ -2,7 +2,7 @@ require 'database_cleaner/generic/base'
module DatabaseCleaner
module Sequel
def self.available_strategies
%w[truncation transaction]
%w[truncation transaction deletion]
end
module Base
@ -14,7 +14,7 @@ module DatabaseCleaner
def db
return @db if @db && @db != :default
raise "As you have more than one active sequel database you have to specify the one to use manually!" if ::Sequel::DATABASES.count > 1
raise "As you have more than one active sequel database you have to specify the one to use manually!" if ::Sequel::DATABASES.count > 1
::Sequel::DATABASES.first || :default
end
end

View file

@ -0,0 +1,33 @@
require "database_cleaner/sequel/base"
require "database_cleaner/generic/truncation"
require "database_cleaner/sequel/truncation"
module DatabaseCleaner::Sequel
class Deletion < Truncation
def clean
db.transaction do
case db.database_type
when :postgres
db.run "SET CONSTRAINTS ALL DEFERRED"
tables_to_truncate(db).each do |table|
db.run "ALTER TABLE #{::Sequel.lit(table)} DISABLE TRIGGER ALL"
end
when :mysql
db.run "SET FOREIGN_KEY_CHECKS = 0"
end
each_table do |db, table|
db[table].delete
end
end
ensure
case db.database_type
when :postgres
tables_to_truncate(db).each do |table|
db.run "ALTER TABLE #{::Sequel.lit(table)} ENABLE TRIGGER ALL"
end
when :mysql
db.run "SET FOREIGN_KEY_CHECKS = 1"
end
end
end
end