Sha256: a5416faafa0d9d3983ec9c920fd6aa2a280e73b0de3a724036e91e53da5e61de

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

require "abstract_importer/strategies/base"
require "activerecord/insert_many"

module AbstractImporter
  module Strategies
    class InsertStrategy < Base

      def initialize(collection, options={})
        super
        @batch = []
        @batch_size = options.fetch(:batch_size, 250)
        @insert_options = options.slice(:on_conflict)
      end


      def process_record(hash)
        summary.total += 1

        if already_imported?(hash)
          summary.already_imported += 1
          reporter.record_skipped hash
          return
        end

        remap_foreign_keys!(hash)

        if redundant_record?(hash)
          summary.redundant += 1
          reporter.record_skipped hash
          return
        end

        add_to_batch prepare_attributes(hash)

      rescue ::AbstractImporter::Skip
        summary.skipped += 1
        reporter.record_skipped hash
      end


      def flush
        invoke_callback(:before_batch, @batch)

        insert_batch(@batch)

        id_map_record_batch(@batch) if remap_ids?

        summary.created += @batch.length
        reporter.batch_inserted(@batch.length)

        @batch = []
      end


      def insert_batch(batch)
        collection.scope.insert_many(batch, @insert_options)
      end


      def add_to_batch(attributes)
        @batch << attributes
        legacy_id, id = attributes.values_at(:legacy_id, :id)
        id_map.merge! collection.table_name, legacy_id => id if id && legacy_id
        flush if @batch.length >= @batch_size
      end


      def id_map_record_batch(batch)
        return if generate_id
        id_map.merge! collection.table_name,
          collection.scope.where(legacy_id: @batch.map { |hash| hash[:legacy_id] })
      end


    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
abstract_importer-1.5.3 lib/abstract_importer/strategies/insert_strategy.rb
abstract_importer-1.5.2 lib/abstract_importer/strategies/insert_strategy.rb