Sha256: 9be8446ca46d9824c296f08b6b8e93e6d850b64d5cf4080e8b738734f130138f

Contents?: true

Size: 1.19 KB

Versions: 33

Compression:

Stored size: 1.19 KB

Contents

require 'database_cleaner/sequel/base'
require 'database_cleaner/generic/truncation'
require 'database_cleaner/sequel/truncation'

module DatabaseCleaner::Sequel
  class Deletion < Truncation
    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

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

    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
  end
end

Version data entries

33 entries across 33 versions & 4 rubygems

Version Path
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/sequel/deletion.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/sequel/deletion.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/sequel/deletion.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/database_cleaner-1.5.3/lib/database_cleaner/sequel/deletion.rb
database_cleaner-1.5.3 lib/database_cleaner/sequel/deletion.rb
database_cleaner-1.5.2 lib/database_cleaner/sequel/deletion.rb
database_cleaner-1.5.1 lib/database_cleaner/sequel/deletion.rb
database_cleaner-1.5.0 lib/database_cleaner/sequel/deletion.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/database_cleaner-1.4.1/lib/database_cleaner/sequel/deletion.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/database_cleaner-1.4.1/lib/database_cleaner/sequel/deletion.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/database_cleaner-1.4.1/lib/database_cleaner/sequel/deletion.rb
database_cleaner-1.4.1 lib/database_cleaner/sequel/deletion.rb
database_cleaner-1.4.0 lib/database_cleaner/sequel/deletion.rb