Class: AbstractImporter::Strategies::InsertStrategy
- Inherits:
-
Base
- Object
- Base
- AbstractImporter::Strategies::InsertStrategy
show all
- Defined in:
- lib/abstract_importer/strategies/insert_strategy.rb
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
8
9
10
11
12
13
14
|
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 8
def initialize(collection, options={})
super
@batch = []
@batch_size = options.fetch(:batch_size, 250)
@insert_options = options.slice(:on_conflict)
@insert_options.merge!(returning: [:legacy_id, :id]) if remap_ids?
end
|
Instance Method Details
#add_batch_to_id_map(result) ⇒ Object
68
69
70
71
72
73
|
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 68
def add_batch_to_id_map(result)
map = result.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
60
61
62
63
64
65
|
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 60
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
|
#flush ⇒ Object
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
|
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 54
def insert_batch(batch)
result = collection.scope.insert_many(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
|