Commit e08fa9c3 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Allow BulkInsertSafe module to return all columns of a relation

parent 4eaa23f6
...@@ -144,7 +144,7 @@ module BulkInsertSafe ...@@ -144,7 +144,7 @@ module BulkInsertSafe
when nil when nil
false false
else else
raise ArgumentError, "returns needs to be :ids or nil" returns
end end
# Handle insertions for tables with a composite primary key # Handle insertions for tables with a composite primary key
...@@ -161,7 +161,7 @@ module BulkInsertSafe ...@@ -161,7 +161,7 @@ module BulkInsertSafe
ActiveRecord::InsertAll ActiveRecord::InsertAll
.new(insert_all_proxy_class, attributes, on_duplicate: on_duplicate, returning: returning, unique_by: unique_by) .new(insert_all_proxy_class, attributes, on_duplicate: on_duplicate, returning: returning, unique_by: unique_by)
.execute .execute
.pluck(primary_key) .cast_values(insert_all_proxy_class.attribute_types).to_a
end end
end end
end end
......
...@@ -180,29 +180,26 @@ RSpec.describe BulkInsertSafe do ...@@ -180,29 +180,26 @@ RSpec.describe BulkInsertSafe do
end end
context 'with returns option set' do 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 context 'when is set to :ids' do
it 'return an array with the primary key values for all inserted records' do let(:returns) { :ids }
items = bulk_insert_item_class.valid_list(1)
expect(bulk_insert_item_class.bulk_insert!(items, returns: :ids)).to contain_exactly(a_kind_of(Integer)) it { is_expected.to contain_exactly(a_kind_of(Integer)) }
end
end end
context 'when is set to nil' do context 'when is set to nil' do
it 'returns an empty array' do let(:returns) { nil }
items = bulk_insert_item_class.valid_list(1)
expect(bulk_insert_item_class.bulk_insert!(items, returns: nil)).to eq([]) it { is_expected.to eq([]) }
end
end end
context 'when is set to anything else' do context 'when is set to a list of attributes' do
it 'raises an error' do let(:returns) { [:id, :sha_value] }
items = bulk_insert_item_class.valid_list(1)
expect { bulk_insert_item_class.bulk_insert!([items], returns: [:id, :name]) } it { is_expected.to contain_exactly([a_kind_of(Integer), '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12']) }
.to raise_error(ArgumentError, "returns needs to be :ids or nil")
end
end end
end 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