Sha256: 22c7f0c5d54cae6079045c2ece91d240ed1d82d2e3a82633d76a9a4895cfc924

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    class MysqlAdapter
      def truncate_table(table_name)
        execute("TRUNCATE TABLE #{table_name};")
      end
    end

    class SQLite3Adapter
      def truncate_table(table_name)
        execute("DELETE FROM #{table_name};")
      end
    end

  end

end


module DatabaseCleaner::ActiveRecord
  class Truncation

    def initialize(options={})
      if !options.empty? && !(options.keys - [:only, :except]).empty?
        raise ArgumentError, "The only valid options are :only and :except. You specified #{options.keys.join(',')}."
      end
      if options.has_key?(:only) && options.has_key?(:except)
        raise ArgumentError, "You may only specify either :only or :either.  Doing both doesn't really make sense does it?" 
      end

      @only = options[:only]
      @tables_to_exclude = (options[:except] || []) << 'schema_migrations'
    end

    def start
      # no-op
    end


    def clean
      connection.disable_referential_integrity do
        tables_to_truncate.each do |table_name|
          connection.truncate_table table_name
        end
      end
  end

  private

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

    def connection
      ::ActiveRecord::Base.connection
    end

  end

end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bmabey-database_cleaner-0.1.1 lib/database_cleaner/active_record/truncation.rb