From 9d484ec5b02221a18c6089e6677420a5787a68de Mon Sep 17 00:00:00 2001 From: Emmanuel Quentin Date: Wed, 25 Apr 2018 11:18:35 +0200 Subject: [PATCH 1/2] Fix spec about upsert_keys due to invalid records --- spec/active_record/key_spec.rb | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/spec/active_record/key_spec.rb b/spec/active_record/key_spec.rb index 2e01ba7..294bb56 100644 --- a/spec/active_record/key_spec.rb +++ b/spec/active_record/key_spec.rb @@ -44,7 +44,7 @@ module ActiveRecord end context 'different ways of setting keys' do let(:attrs) { {make: 'Ford', name: 'Focus', long_field: SecureRandom.uuid} } - before { Vehicle.create(attrs) } + let!(:vehicule) { Vehicle.create(attrs) } it 'works with multiple symbol args' do Vehicle.upsert_keys :make, :name @@ -71,28 +71,31 @@ module ActiveRecord expect(upserted.wheels_count).to eq(1) end it 'works with a single symbol' do - v = Vehicle.create Vehicle.upsert_keys :id - upserted = Vehicle.new(id: v.id, wheels_count: 1) - upserted.upsert + upserted = Vehicle.new(id: vehicule.id, name: 'ford', wheels_count: 1) + result = upserted.upsert + + expect(result).to be_truthy expect(upserted.wheels_count).to eq(1) - expect(upserted.id).to eq(v.id) + expect(upserted.id).to eq(vehicule.id) end it 'works with a single string' do - v = Vehicle.create Vehicle.upsert_keys 'id' - upserted = Vehicle.new(id: v.id, wheels_count: 1) - upserted.upsert + upserted = Vehicle.new(id: vehicule.id, name: 'ford', wheels_count: 1) + result = upserted.upsert + + expect(result).to be_truthy expect(upserted.wheels_count).to eq(1) - expect(upserted.id).to eq(v.id) + expect(upserted.id).to eq(vehicule.id) end it 'works with a literal' do - v = Vehicle.create Vehicle.upsert_keys literal: 'md5(long_field)' - upserted = Vehicle.new(id: v.id, long_field: attrs[:long_field]) - upserted.upsert + upserted = Vehicle.new(id: vehicule.id, name: 'ford', long_field: attrs[:long_field]) + result = upserted.upsert + + expect(result).to be_truthy expect(upserted.long_field).to eq(attrs[:long_field]) - expect(upserted.id).to eq(v.id) + expect(upserted.id).to eq(vehicule.id) end end From e727f950955b21835caa7bcc93e52eb9b8223366 Mon Sep 17 00:00:00 2001 From: Emmanuel Quentin Date: Wed, 25 Apr 2018 11:20:15 +0200 Subject: [PATCH 2/2] Literal key is stored in the options instead of the upsert_keys like the where condition --- lib/active_record_upsert/arel/crud.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_record_upsert/arel/crud.rb b/lib/active_record_upsert/arel/crud.rb index 1d831c9..ccf0d5b 100644 --- a/lib/active_record_upsert/arel/crud.rb +++ b/lib/active_record_upsert/arel/crud.rb @@ -13,7 +13,7 @@ module Arel module Crud def compile_upsert(upsert_keys, upsert_options, upsert_values, insert_values, wheres) # Support non-attribute key (like `md5(my_attribute)``) - target = self[upsert_keys.kind_of?(Hash) ? ::Arel::Nodes::SqlLiteral.new(upsert_keys[:literal]) : upsert_keys.join(',')] + target = self[upsert_options.key?(:literal) ? ::Arel::Nodes::SqlLiteral.new(upsert_options[:literal]) : upsert_keys.join(',')] on_conflict_do_update = OnConflictDoUpdateManager.new on_conflict_do_update.target = target