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 3b03c98 commit 8de5d76
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
@@ -1,3 +1,9 @@
* Keep track of dirty attributes after transaction is rollback.

Related #13166.

*Bogdan Gusiev* *arthurnn*

* Stringify all variables keys of mysql connection configuration.

When `sql_mode` variable for mysql adapters set in configuration as `String`
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/transactions.rb
Expand Up @@ -351,6 +351,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 @@ -369,6 +370,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
Expand Up @@ -260,6 +260,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 8de5d76

Please sign in to comment.