Skip to content

Commit

Permalink
Merge pull request #18200 from brainopia/rollback_frozen_records
Browse files Browse the repository at this point in the history
Fix rollback of frozen records
  • Loading branch information
Arthur Nogueira Neves authored and arthurnn committed Mar 8, 2015
1 parent 471562f commit ec712fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/transactions.rb
Expand Up @@ -331,6 +331,7 @@ def add_to_transaction
if has_transactional_callbacks?
self.class.connection.add_transaction_record(self)
else
sync_with_transaction_state
set_transaction_state(self.class.connection.transaction_state)
end
remember_transaction_record_state
Expand Down Expand Up @@ -388,13 +389,14 @@ def restore_transaction_record_state(force = false) #:nodoc:
transaction_level = (@_start_transaction_state[:level] || 0) - 1
if transaction_level < 1 || force
restore_state = @_start_transaction_state
thaw unless restore_state[:frozen?]
thaw
@new_record = restore_state[:new_record]
@destroyed = restore_state[:destroyed]
pk = self.class.primary_key
if pk && read_attribute(pk) != restore_state[:id]
write_attribute(pk, restore_state[:id])
end
freeze if restore_state[:frozen?]
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/transactions_test.rb
Expand Up @@ -577,6 +577,24 @@ def test_restore_frozen_state_after_double_destroy
assert_not topic.frozen?
end

def test_rollback_of_frozen_records
topic = Topic.create.freeze
Topic.transaction do
topic.destroy
raise ActiveRecord::Rollback
end
assert topic.frozen?, 'frozen'
end

def test_rollback_for_freshly_persisted_records
topic = Topic.create
Topic.transaction do
topic.destroy
raise ActiveRecord::Rollback
end
assert topic.persisted?, 'persisted'
end

def test_sqlite_add_column_in_transaction
return true unless current_adapter?(:SQLite3Adapter)

Expand Down

0 comments on commit ec712fb

Please sign in to comment.