-
Notifications
You must be signed in to change notification settings - Fork 84
/
bulk_insert.rb
38 lines (32 loc) · 950 Bytes
/
bulk_insert.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
require 'bulk_insert/worker'
module BulkInsert
extend ActiveSupport::Concern
module ClassMethods
def bulk_insert(*columns, values: nil, set_size:500, ignore: false, update_duplicates: false, return_primary_keys: false)
columns = default_bulk_columns if columns.empty?
worker = BulkInsert::Worker.new(connection, table_name, primary_key, columns, set_size, ignore, update_duplicates, return_primary_keys)
if values.present?
transaction do
worker.add_all(values)
worker.save!
end
nil
elsif block_given?
transaction do
yield worker
worker.save!
end
nil
else
worker
end
end
# helper method for preparing the columns before a call to :bulk_insert
def default_bulk_columns
self.column_names - %w(id)
end
end
end
ActiveSupport.on_load(:active_record) do
send(:include, BulkInsert)
end