From 43132756668cb916e04ec590ae1df37a8c69ba76 Mon Sep 17 00:00:00 2001 From: Andrii Konchyn Date: Sat, 28 Nov 2026 22:31:41 +0200 Subject: [PATCH] Raise RecordNotSaved in #update! when update callbacks abort --- lib/dynamoid/dirty.rb | 4 +- lib/dynamoid/persistence.rb | 23 ++++-- spec/dynamoid/persistence/update_spec.rb | 98 ++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 7 deletions(-) diff --git a/lib/dynamoid/dirty.rb b/lib/dynamoid/dirty.rb index 062a2939..f945e9eb 100644 --- a/lib/dynamoid/dirty.rb +++ b/lib/dynamoid/dirty.rb @@ -60,8 +60,8 @@ def save!(*) # @private def update(*) - super.tap do - clear_changes_information + super.tap do |status| + clear_changes_information if status end end diff --git a/lib/dynamoid/persistence.rb b/lib/dynamoid/persistence.rb index 5623e94b..7d45a55f 100644 --- a/lib/dynamoid/persistence.rb +++ b/lib/dynamoid/persistence.rb @@ -916,15 +916,23 @@ def update_attribute!(attribute, value) # If a document doesn't meet conditions it raises # +Dynamoid::Errors::StaleObjectError+ exception. # + # If any of the +before_*+ callbacks throws +:abort+ the updating is + # cancelled and +update!+ raises +Dynamoid::Errors::RecordNotSaved+. + # # It will increment the +lock_version+ attribute if a table has the column, # but will not check it. Thus, a concurrent +save+ call will never cause an # +update!+ to fail, but an +update!+ may cause a concurrent +save+ to # fail. # # @param conditions [Hash] Conditions on model attributes to make a conditional update (optional) + # @raise [Dynamoid::Errors::StaleObjectError] if the document does not meet the specified conditions or no longer exists + # @raise [Dynamoid::Errors::RecordNotSaved] if an +update+ callback aborts the update # @return [Dynamoid::Document] self def update!(conditions = {}) + aborted = true + run_callbacks(:update) do + aborted = false options = {} if range_key value = read_attribute(range_key) @@ -945,8 +953,8 @@ def update!(conditions = {}) update_item_options = options.merge(conditions: conditions) - new_attrs = Dynamoid.adapter.update_item(table_name, partition_key_dumped, update_item_options) do |t| - item_updater = ItemUpdaterWithDumping.new(self.class, t) + new_attrs = Dynamoid.adapter.update_item(table_name, partition_key_dumped, update_item_options) do |u| + item_updater = ItemUpdaterWithDumping.new(self.class, u) item_updater.add(lock_version: 1) if self.class.attributes[:lock_version] @@ -954,7 +962,7 @@ def update!(conditions = {}) item_updater.set(updated_at: DateTime.now.in_time_zone(Time.zone)) end - yield t + yield u end load(Undumping.undump_attributes(new_attrs, self.class.attributes)) rescue Dynamoid::Errors::ConditionalCheckFailedException @@ -965,6 +973,10 @@ def update!(conditions = {}) end end + if aborted + raise Dynamoid::Errors::RecordNotSaved, self + end + self end @@ -1043,7 +1055,8 @@ def update!(conditions = {}) # end # ``` # - # If a document doesn't meet conditions it just returns +false+. Otherwise it returns +true+. + # If a document doesn't meet conditions or any of the +before_*+ callbacks + # throws +:abort+ it just returns +false+. Otherwise it returns +true+. # # It will increment the +lock_version+ attribute if a table has the column, # but will not check it. Thus, a concurrent +save+ call will never cause an @@ -1055,7 +1068,7 @@ def update!(conditions = {}) def update(conditions = {}, &block) update!(conditions, &block) true - rescue Dynamoid::Errors::StaleObjectError + rescue Dynamoid::Errors::StaleObjectError, Dynamoid::Errors::RecordNotSaved false end diff --git a/spec/dynamoid/persistence/update_spec.rb b/spec/dynamoid/persistence/update_spec.rb index 7167710b..1b31e20a 100644 --- a/spec/dynamoid/persistence/update_spec.rb +++ b/spec/dynamoid/persistence/update_spec.rb @@ -814,6 +814,58 @@ def around_update_callback }.to raise_error(Dynamoid::Errors::StaleObjectError) end end + + describe 'callbacks' do + context 'when a callback aborts updating' do + it 'aborts updating and raises RecordNotSaved if callback throws :abort' do + if ActiveSupport.version < Gem::Version.new('5.0') + skip "Rails 4.x and below don't support aborting with `throw :abort`" + end + + klass = new_class do + field :name + before_update { throw :abort } + end + + obj = klass.create!(name: 'Alex') + obj.name = 'Alex [Dirty]' + + expect { + expect { + obj.update! { |t| t.set(name: 'Michael') } + }.to raise_error(Dynamoid::Errors::RecordNotSaved) { |error| + expect(error.record).to eq(obj) + } + }.not_to change { klass.find(obj.id).name } + + expect(obj).to be_changed + end + + it 'aborts updating and raises RecordNotSaved if around_update does not yield' do + klass = new_class do + field :name + around_update :abort_update + + def abort_update + # intentionally do not yield + end + end + + obj = klass.create!(name: 'Alex') + obj.name = 'Alex [Dirty]' + + expect { + expect { + obj.update! { |t| t.set(name: 'Michael') } + }.to raise_error(Dynamoid::Errors::RecordNotSaved) { |error| + expect(error.record).to eq(obj) + } + }.not_to change { klass.find(obj.id).name } + + expect(obj).to be_changed + end + end + end end describe '#update' do @@ -1179,6 +1231,52 @@ def around_update_callback }.to output(expected_output).to_stdout }.to output.to_stdout end + + context 'when a callback aborts updating' do + it 'aborts updating and returns false if callback throws :abort' do + if ActiveSupport.version < Gem::Version.new('5.0') + skip "Rails 4.x and below don't support aborting with `throw :abort`" + end + + klass = new_class do + field :name + before_update { throw :abort } + end + + obj = klass.create!(name: 'Alex') + obj.name = 'Alex [Dirty]' + + result = nil + expect { + result = obj.update { |t| t.set(name: 'Michael') } + }.not_to change { klass.find(obj.id).name } + + expect(result).to eql false + expect(obj).to be_changed + end + + it 'aborts updating and returns false if around_update does not yield' do + klass = new_class do + field :name + around_update :abort_update + + def abort_update + # intentionally do not yield + end + end + + obj = klass.create!(name: 'Alex') + obj.name = 'Alex [Dirty]' + + result = nil + expect { + result = obj.update { |t| t.set(name: 'Michael') } + }.not_to change { klass.find(obj.id).name } + + expect(result).to eql false + expect(obj).to be_changed + end + end end context 'when a model was concurrently deleted' do