Sha256: a43d4c502f0972c1cbf53d45ba6e24ce25a4febc5936a009ce44882cbf50055c

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

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

module DatabaseCleaner
  module Sequel
    class Truncation
      include ::DatabaseCleaner::Sequel::Base
      include ::DatabaseCleaner::Generic::Truncation

      def clean
        case db.database_type
        when :postgres
          # PostgreSQL requires all tables with FKs to be truncates in the same command, or have the CASCADE keyword
          # appended. Bulk truncation without CASCADE is:
          # * Safer. Tables outside of tables_to_truncate won't be affected.
          # * Faster. Less roundtrips to the db.
          unless (tables= tables_to_truncate(db)).empty?
            all_tables= tables.map{|t| %["#{t}"]}.join ','
            db.run "TRUNCATE TABLE #{all_tables};"
          end
        else
          # Truncate each table normally
          each_table do |db, table|
            db[table].truncate
          end
        end
      end

      def each_table
        tables_to_truncate(db).each do |table|
          yield db, table
        end
      end

      private

      def tables_to_truncate(db)
        (@only || db.tables) - @tables_to_exclude
      end

      # overwritten
      def migration_storage_names
        %w[schema_info schema_migrations]
      end

    end
  end
end


Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/database_cleaner-0.9.1/lib/database_cleaner/sequel/truncation.rb
database_cleaner-0.9.1 lib/database_cleaner/sequel/truncation.rb