From e0d59e6219c752d8cffc6b78c2240755f5728922 Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Mon, 8 Jul 2013 12:31:07 +0300 Subject: #4566: Remove extra decrement of transaction level `rollback_active_record_state!` tries to restore model state on `Exception` by invoking `restore_transaction_record_state` it decrement deep level by `1`. After restoring it ensure that states to be cleared and level decremented by invoking `clear_transaction_record_state`, which cause the bug: because state already reduced in `restore_transaction_record_state`. Removed double derement of transaction level and removed duplicated code which clear transaction state for top level. --- activerecord/test/cases/transactions_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index c0cad52b22..9c5f2e4724 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -117,6 +117,20 @@ class TransactionTest < ActiveRecord::TestCase assert !Topic.find(1).approved? end + def test_raising_exception_in_nested_transaction_restore_state_in_save + topic = Topic.new + + def topic.after_save_for_transaction + raise 'Make the transaction rollback' + end + + assert_raises(RuntimeError) do + Topic.transaction { topic.save } + end + + assert topic.new_record?, "#{topic.inspect} should be new record" + end + def test_update_should_rollback_on_failure author = Author.find(1) posts_count = author.posts.size -- cgit v1.2.3