Commit a3fd2829 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Merge branch '276498_change_bulk_insert_safe_to_return_all_attributes' into 'master'

Allow BulkInsertSafe module to return all columns of a relation

See merge request gitlab-org/gitlab!72214
parents 7d1236ab e08fa9c3
......@@ -144,7 +144,7 @@ module BulkInsertSafe
when nil
false
else
raise ArgumentError, "returns needs to be :ids or nil"
returns
end
# Handle insertions for tables with a composite primary key
......@@ -161,7 +161,7 @@ module BulkInsertSafe
ActiveRecord::InsertAll
.new(insert_all_proxy_class, attributes, on_duplicate: on_duplicate, returning: returning, unique_by: unique_by)
.execute
.pluck(primary_key)
.cast_values(insert_all_proxy_class.attribute_types).to_a
end
end
end
......
......@@ -180,29 +180,26 @@ RSpec.describe BulkInsertSafe do
end
context 'with returns option set' do
let(:items) { bulk_insert_item_class.valid_list(1) }
subject(:bulk_insert) { bulk_insert_item_class.bulk_insert!(items, returns: returns) }
context 'when is set to :ids' do
it 'return an array with the primary key values for all inserted records' do
items = bulk_insert_item_class.valid_list(1)
let(:returns) { :ids }
expect(bulk_insert_item_class.bulk_insert!(items, returns: :ids)).to contain_exactly(a_kind_of(Integer))
end
it { is_expected.to contain_exactly(a_kind_of(Integer)) }
end
context 'when is set to nil' do
it 'returns an empty array' do
items = bulk_insert_item_class.valid_list(1)
let(:returns) { nil }
expect(bulk_insert_item_class.bulk_insert!(items, returns: nil)).to eq([])
end
it { is_expected.to eq([]) }
end
context 'when is set to anything else' do
it 'raises an error' do
items = bulk_insert_item_class.valid_list(1)
context 'when is set to a list of attributes' do
let(:returns) { [:id, :sha_value] }
expect { bulk_insert_item_class.bulk_insert!([items], returns: [:id, :name]) }
.to raise_error(ArgumentError, "returns needs to be :ids or nil")
end
it { is_expected.to contain_exactly([a_kind_of(Integer), '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12']) }
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment