Skip to content

Commit

Permalink
Fix bug with rolling back frozen attributes.
Browse files Browse the repository at this point in the history
[#2991]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
Brian Durand authored and jeremy committed Jun 18, 2010
1 parent a186431 commit 237165f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
Expand Up @@ -304,7 +304,7 @@ def rollback_transaction_records(rollback) #:nodoc
begin
record.rolledback!(rollback)
rescue Exception => e
record.logger.error(e) if record.respond_to?(:logger)
record.logger.error(e) if record.respond_to?(:logger) && record.logger
end
end
end
Expand All @@ -319,7 +319,7 @@ def commit_transaction_records #:nodoc
begin
record.committed!
rescue Exception => e
record.logger.error(e) if record.respond_to?(:logger)
record.logger.error(e) if record.respond_to?(:logger) && record.logger
end
end
end
Expand Down
1 change: 1 addition & 0 deletions activerecord/lib/active_record/transactions.rb
Expand Up @@ -320,6 +320,7 @@ def restore_transaction_record_state(force = false) #:nodoc
if @_start_transaction_state[:level] < 1
restore_state = remove_instance_variable(:@_start_transaction_state)
if restore_state
@attributes = @attributes.dup if @attributes.frozen?
@new_record = restore_state[:new_record]
@destroyed = restore_state[:destroyed]
if restore_state[:id]
Expand Down
1 change: 0 additions & 1 deletion activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -712,7 +712,6 @@ def destroy(*args)
end

assert_raise(RuntimeError) { assert !@pirate.save }
assert before.first.frozen? # the first child was indeed destroyed
assert_equal before, @pirate.reload.send(association_name)
end

Expand Down
14 changes: 11 additions & 3 deletions activerecord/test/cases/transaction_callbacks_test.rb
Expand Up @@ -221,20 +221,28 @@ def @first.commits(i=0); @commits ||= 0; @commits += i if i; end
assert_equal 2, @first.rollbacks
end

def test_after_transaction_callbacks_should_not_raise_errors
def test_after_transaction_callbacks_should_prevent_callbacks_from_being_called
def @first.last_after_transaction_error=(e); @last_transaction_error = e; end
def @first.last_after_transaction_error; @last_transaction_error; end
@first.after_commit_block{|r| r.last_after_transaction_error = :commit; raise "fail!";}
@first.after_rollback_block{|r| r.last_after_transaction_error = :rollback; raise "fail!";}
@second.after_commit_block{|r| r.history << :after_commit}
@second.after_rollback_block{|r| r.history << :after_rollback}

@first.save!
Topic.transaction do
@first.save!
@second.save!
end
assert_equal :commit, @first.last_after_transaction_error
assert_equal [:after_commit], @second.history

@second.history.clear
Topic.transaction do
@first.save!
@second.save!
raise ActiveRecord::Rollback
end

assert_equal :rollback, @first.last_after_transaction_error
assert_equal [:after_rollback], @second.history
end
end

0 comments on commit 237165f

Please sign in to comment.