Skip to content

Commit

Permalink
Merge branch 'rm-trasaction-fix-13166'
Browse files Browse the repository at this point in the history
Conflicts:
	activerecord/CHANGELOG.md

Backport of #13166
  • Loading branch information
rafaelfranca committed May 7, 2014
1 parent 4214063 commit e8f5a34
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Keep track of dirty attributes after transaction is rollback.

Related #13166.

*Bogdan Gusiev* *arthurnn*

* Revert the behaviour of `ActiveRecord::Relation#join` changed through 4.0 => 4.1 to 4.0.

In 4.1.0 `Relation#join` is delegated to `Arel#SelectManager`.
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ def remember_transaction_record_state #:nodoc:
end
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1
@_start_transaction_state[:frozen?] = @attributes.frozen?
@_start_transaction_state[:changed_attributes] ||= changed_attributes
end

# Clear the new record state and id of a record.
Expand All @@ -368,6 +369,9 @@ def restore_transaction_record_state(force = false) #:nodoc:
@attributes = @attributes.dup if @attributes.frozen?
@new_record = restore_state[:new_record]
@destroyed = restore_state[:destroyed]
changed_attributes.replace(restore_state[:changed_attributes]).delete_if do |attribute, old_value|
old_value == @attributes[attribute]
end
if restore_state.has_key?(:id)
write_attribute(self.class.primary_key, restore_state[:id])
else
Expand Down
42 changes: 42 additions & 0 deletions activerecord/test/cases/transactions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,48 @@ def test_invalid_keys_for_transaction
end
end

def test_rollback_when_changing_inside_transaction
assert !@first.approved?
Topic.transaction do
@first.approved = true
@first.save!
raise ActiveRecord::Rollback
end
assert @first.approved
assert @first.changes["approved"]
@first.save!
assert @first.reload.approved
end

def test_rollback_when_changing_outside_transaction
assert !@first.approved?
@first.approved = true
Topic.transaction do
@first.save!
raise ActiveRecord::Rollback
end
assert @first.changes["approved"]
assert @first.approved
@first.save!
assert @first.reload.approved
end

def test_rollback_when_changing_back_to_prev_stage
assert !@first.approved?
Topic.transaction do
@first.approved = true
@first.save!
@first.approved = false
@first.save!
raise ActiveRecord::Rollback
end
assert !@first.approved
assert !@first.changes["approved"]
@first.save!
assert !@first.reload.approved
end


def test_force_savepoint_in_nested_transaction
Topic.transaction do
@first.approved = true
Expand Down

0 comments on commit e8f5a34

Please sign in to comment.