Class: AbstractImporter::Strategies::InsertStrategy

Inherits:
Base
  • Object
show all
Defined in:
lib/abstract_importer/strategies/insert_strategy.rb

Direct Known Subclasses

UpsertStrategy

Instance Attribute Summary

Attributes inherited from Base

#collection

Instance Method Summary collapse

Methods inherited from Base

#already_imported?, #prepare_attributes, #remap_ids?

Constructor Details

#initialize(collection, options = {}) ⇒ InsertStrategy

Returns a new instance of InsertStrategy.



7
8
9
10
11
12
13
14
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 7

def initialize(collection, options={})
  super
  @batch = []
  @batch_size = options.fetch(:batch_size, 250)
  @bulk_operation = options[:on_duplicate] == :update ? :upsert_all : :insert_all
  @insert_options = options.slice(:unique_by)
  @insert_options.merge!(returning: [:legacy_id, :id]) if remap_ids?
end

Instance Method Details

#add_batch_to_id_map(result) ⇒ Object



70
71
72
73
74
75
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 70

def add_batch_to_id_map(result)
  map = cast_result(result, collection.table_name).each_with_object({}) do |attrs, map|
    map[attrs.fetch("legacy_id")] = attrs.fetch("id")
  end
  id_map.merge! collection.table_name, map
end

#add_to_batch(attributes) ⇒ Object



62
63
64
65
66
67
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 62

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

#cast_result(result, table_name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 78

def cast_result(result, table_name)
  types_by_column = result.columns.each_with_object({}) do |column_name, types|
    types[column_name] = collection.scope.connection.lookup_cast_type_from_column(collection.scope.columns.find { |column| column.name == column_name })
  end

  result.to_a.map { |row|
    Hash[row.map { |column_name, value|
      [ column_name, types_by_column[column_name].deserialize(value) ]
    }]
  }
end

#flushObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 42

def flush
  invoke_callback(:before_batch, @batch)

  insert_batch(@batch)

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

  @batch = []
end

#insert_batch(batch) ⇒ Object



54
55
56
57
58
59
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 54

def insert_batch(batch)
  return if batch.empty?

  result = collection.scope.public_send(@bulk_operation, batch, @insert_options)
  add_batch_to_id_map(result) if remap_ids?
end

#process_record(hash) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 17

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