Skip to content

Commit 61fa3b6

Browse files
committed
Return primary keys when using blocks or values
1 parent c12755d commit 61fa3b6

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ values. Default value for this option is false.
160160
destination_columns = [:title, :author]
161161

162162
# Update duplicate rows
163+
163164
Book.bulk_insert(*destination_columns, update_duplicates: true) do |worker|
164165
worker.add(...)
165166
worker.add(...)
@@ -169,23 +170,34 @@ end
169170

170171
### Return Primary Keys (PostgreSQL, PostGIS)
171172

172-
If you want the worker to store primary keys of inserted records, then you can
173-
use the _return_primary_keys_ option. The worker will store a `result_sets`
174-
array of `ActiveRecord::Result` objects. Each `ActiveRecord::Result` object
175-
will contain the primary keys of a batch of inserted records.
173+
If you want the worker to store primary keys of inserted records, then you
174+
can use the _return_primary_keys_ option. The `bulk_insert` block will the
175+
list of primary keys values inserted.
176176

177177
```ruby
178-
worker = Book.bulk_insert(*destination_columns, return_primary_keys: true) do
179-
|worker|
178+
inserted_ids = Book.bulk_insert(return_primary_keys: true) do |worker|
180179
worker.add(...)
181180
worker.add(...)
182181
# ...
183182
end
183+
```
184+
185+
When working with the worker instance directly, the primary keys of the
186+
inserted records will be stored in the `results_sets` attribute as an array
187+
of `ActiveRecord::Result` objects. Each `ActiveRecord::Result` object will
188+
contain the primary keys of a batch of inserted records.
189+
190+
The `inserted_ids` method will return these ids as a flat list.
191+
192+
```ruby
193+
worker = Book.bulk_insert(*destination_columns, return_primary_keys: true)
194+
worker.add(...)
195+
worker.save!
184196

185197
worker.result_sets
198+
worker.inserted_ids
186199
```
187200

188-
189201
## License
190202

191203
BulkInsert is released under the MIT license (see MIT-LICENSE) by

lib/bulk_insert.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ def bulk_insert(*columns, values: nil, set_size:500, ignore: false, update_dupli
88
columns = default_bulk_columns if columns.empty?
99
worker = BulkInsert::Worker.new(connection, table_name, primary_key, columns, set_size, ignore, update_duplicates, return_primary_keys)
1010

11-
if values.present?
11+
if !values.nil?
1212
transaction do
1313
worker.add_all(values)
1414
worker.save!
1515
end
16-
nil
16+
return_primary_keys ? worker.inserted_ids : nil
1717
elsif block_given?
1818
transaction do
1919
yield worker
2020
worker.save!
2121
end
22-
nil
22+
return_primary_keys ? worker.inserted_ids : nil
2323
else
2424
worker
2525
end

lib/bulk_insert/worker.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def initialize(connection, table_name, primary_key, column_names, set_size=500,
3232
@set = []
3333
end
3434

35+
def inserted_ids
36+
@return_primary_keys ? @result_sets.map(&:rows).flatten : nil
37+
end
38+
3539
def pending?
3640
@set.any?
3741
end

test/bulk_insert/worker_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
128128
@insert.save!
129129

130130
assert_equal 0, @insert.result_sets.count
131+
assert_nil @insert.inserted_ids
131132
end
132133

133134

@@ -151,11 +152,13 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
151152
worker.add greeting: "second"
152153
worker.save!
153154
assert_equal 1, worker.result_sets.count
155+
assert_equal [], worker.inserted_ids
154156

155157
worker.add greeting: "third"
156158
worker.add greeting: "fourth"
157159
worker.save!
158160
assert_equal 2, worker.result_sets.count
161+
assert_equal [], worker.inserted_ids
159162
end
160163

161164
test "initialized with empty result sets array" do

test/bulk_insert_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,26 @@ class BulkInsertTest < ActiveSupport::TestCase
3636

3737
test "bulk_insert with array should save the array immediately" do
3838
assert_difference "Testing.count", 2 do
39-
Testing.bulk_insert values: [
39+
result = Testing.bulk_insert values: [
4040
[ "Hello", 15, true ],
4141
{ greeting: "Hey", age: 20, happy: false }
4242
]
43+
assert_nil result
4344
end
4445
end
4546

47+
test "bulk_insert with an empty value should not return worker" do
48+
assert_nil Testing.bulk_insert values: []
49+
end
50+
51+
test "bulk_insert with option to return primary keys and values should return the new ids" do
52+
output = Testing.bulk_insert(
53+
values: [["Hello", 15, true]],
54+
return_primary_keys: true
55+
)
56+
assert_equal output, []
57+
end
58+
4659
test "default_bulk_columns should return all columns without id" do
4760
default_columns = %w(greeting age happy created_at updated_at color)
4861

0 commit comments

Comments
 (0)