Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dynamoid/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 18 additions & 5 deletions lib/dynamoid/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -945,16 +953,16 @@ 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]

if self.class.timestamps_enabled?
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
Expand All @@ -965,6 +973,10 @@ def update!(conditions = {})
end
end

if aborted
raise Dynamoid::Errors::RecordNotSaved, self
end

self
end

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
98 changes: 98 additions & 0 deletions spec/dynamoid/persistence/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading