-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_upsert.rb
61 lines (54 loc) · 1.99 KB
/
bulk_upsert.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'kiba'
require 'kiba-common/sources/enumerable'
require 'kiba-pro/destinations/sql_bulk_insert'
require 'sequel'
require_relative 'dsl_extensions/feedback'
module Kiba
module Blueprints
module BulkUpsert
module_function
def setup(database:, base_price:, description_prefix:, count: 100_000)
klass = self
Kiba.parse do
extend Kiba::Blueprints::DSLExtensions::Feedback
setup_feedback(klass)
source Kiba::Common::Sources::Enumerable, (1..count)
transform { |r|
{
product_ref: "ref-#{r}",
price_cents: base_price + r,
quantity: 5_000,
description: description_prefix + " (#{r})"
}
}
destination Kiba::Pro::Destinations::SQLBulkInsert,
database: database,
table: :products,
buffer_size: 25_000,
dataset: -> (dataset) {
dataset.insert_conflict(
target: :product_ref,
update: {
price_cents: Sequel[:excluded][:price_cents],
quantity: Sequel[:excluded][:quantity],
description: Sequel[:excluded][:description]
}
)
}
# NOTE: we use this cheap technique to ensure non regression of the blueprint.
# This code is not necessarily a recommendation of how to implement that.
post_process do
# just a couple of sanity checks here
fail "Invalid number of records" unless database[:products].count == count
test_record = database[:products].where(product_ref: "ref-1").first!
fail "Invalid test record (#{test_record})" unless test_record.slice(:description, :price_cents) == {
description: description_prefix + " (1)",
price_cents: base_price + 1
}
logger.info "Sanity checks OK!"
end
end
end
end
end
end