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
Returns a new instance of InsertStrategy.
8
9
10
11
12
13
|
# 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)
end
|
Instance Method Details
#add_to_batch(attributes) ⇒ Object
57
58
59
60
61
62
|
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 57
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
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 38
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
|
#id_map_record_batch(batch) ⇒ Object
65
66
67
68
69
|
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 65
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
|
#insert_batch(batch) ⇒ Object
52
53
54
|
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 52
def insert_batch(batch)
collection.scope.insert_many(batch, @insert_options)
end
|
#process_record(hash) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 16
def process_record(hash)
summary.total += 1
if already_imported?(hash)
summary.already_imported += 1
return
end
remap_foreign_keys!(hash)
if redundant_record?(hash)
summary.redundant += 1
return
end
add_to_batch prepare_attributes(hash)
rescue ::AbstractImporter::Skip
summary.skipped += 1
end
|