mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
add a deletion strategy for sequel
This commit is contained in:
parent
e331222c55
commit
e769615257
3 changed files with 36 additions and 2 deletions
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
|
|
33
lib/database_cleaner/sequel/deletion.rb
Normal file
33
lib/database_cleaner/sequel/deletion.rb
Normal 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
|
Loading…
Reference in a new issue