diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2012-05-21 20:55:03 -0700 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2012-05-21 20:55:03 -0700 |
commit | 1447aca70f231f07eedee9572bd45da6a175262b (patch) | |
tree | 9713d72dcf293f71270e871a2a4673795c49cd5a | |
parent | e44009aa2bca8fc8f57c432d10e74aff84bbae29 (diff) | |
parent | cb847b9f2e56eeff737323d9a42a2a0a6c23804d (diff) | |
download | rails-1447aca70f231f07eedee9572bd45da6a175262b.tar.gz rails-1447aca70f231f07eedee9572bd45da6a175262b.tar.bz2 rails-1447aca70f231f07eedee9572bd45da6a175262b.zip |
Merge pull request #6420 from chancancode/master_restore_frozen_state_on_rollback
Restore frozen state on rollback, fixes #6417
-rw-r--r-- | activerecord/lib/active_record/transactions.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/transactions_test.rb | 10 |
2 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 30e1035300..9cb9b4627b 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -329,7 +329,8 @@ module ActiveRecord @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1 if @_start_transaction_state[:level] < 1 restore_state = remove_instance_variable(:@_start_transaction_state) - @attributes = @attributes.dup if @attributes.frozen? + was_frozen = @attributes.frozen? + @attributes = @attributes.dup if was_frozen @new_record = restore_state[:new_record] @destroyed = restore_state[:destroyed] if restore_state.has_key?(:id) @@ -338,6 +339,7 @@ module ActiveRecord @attributes.delete(self.class.primary_key) @attributes_cache.delete(self.class.primary_key) end + @attributes.freeze if was_frozen end end end diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index 203dd054f1..a9ccd00fac 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -362,6 +362,16 @@ class TransactionTest < ActiveRecord::TestCase end end + def test_rollback_when_saving_a_frozen_record + topic = Topic.new(:title => 'test') + topic.freeze + e = assert_raise(RuntimeError) { topic.save } + assert_equal "can't modify frozen Hash", e.message + assert !topic.persisted?, 'not persisted' + assert_nil topic.id + assert topic.frozen?, 'not frozen' + end + def test_restore_active_record_state_for_all_records_in_a_transaction topic_1 = Topic.new(:title => 'test_1') topic_2 = Topic.new(:title => 'test_2') |