Class: DatabaseCleaner::Sequel::Deletion

Inherits:
Truncation
  • Object
show all
Defined in:
lib/database_cleaner/sequel/deletion.rb

Instance Method Summary collapse

Methods inherited from Truncation

#start

Methods included from Generic::Truncation

#initialize, #start

Methods included from Base

#db, #db=

Methods included from Generic::Base

#cleaning, #db, included

Instance Method Details

#cleanObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/database_cleaner/sequel/deletion.rb', line 36

def clean
  return unless dirty?

  tables = tables_to_truncate(db)
  db.transaction do
    disable_referential_integrity(tables) do
      delete_tables(db, tables)
    end
  end
end

#delete_tables(db, tables) ⇒ Object



30
31
32
33
34
# File 'lib/database_cleaner/sequel/deletion.rb', line 30

def delete_tables(db, tables)
  tables.each do |table|
    db[table.to_sym].delete
  end
end

#disable_referential_integrity(tables) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/database_cleaner/sequel/deletion.rb', line 7

def disable_referential_integrity(tables)
  case db.database_type
  when :postgres
    db.run('SET CONSTRAINTS ALL DEFERRED')
    tables_to_truncate(db).each do |table|
      db.run("ALTER TABLE \"#{table}\" DISABLE TRIGGER ALL")
    end
  when :mysql
    old = db.fetch('SELECT @@FOREIGN_KEY_CHECKS').first[:@@FOREIGN_KEY_CHECKS]
    db.run('SET FOREIGN_KEY_CHECKS = 0')
  end
  yield
ensure
  case db.database_type
  when :postgres
    tables.each do |table|
      db.run("ALTER TABLE \"#{table}\" ENABLE TRIGGER ALL")
    end
  when :mysql
    db.run("SET FOREIGN_KEY_CHECKS = #{old}")
  end
end