Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix bugs with changed attributes tracking when transaction gets rollback

  • Loading branch information...
commit 998ba0e7084345a045d49a64ee9cf9d2d2441780 1 parent b6f189e
@bogdan bogdan authored
View
1  activerecord/lib/active_record/core.rb
@@ -428,7 +428,6 @@ def init_internals
@destroyed_by_association = nil
@new_record = true
@txn = nil
- @_start_transaction_state = {}
@transaction_state = nil
@reflects_state = [false]
end
View
6 activerecord/lib/active_record/transactions.rb
@@ -341,6 +341,7 @@ def with_transaction_returning_status
# Save the new record state and id of a record so it can be restored later if a transaction fails.
def remember_transaction_record_state #:nodoc:
+ @_start_transaction_state ||= {}
@_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key)
unless @_start_transaction_state.include?(:new_record)
@_start_transaction_state[:new_record] = @new_record
@@ -350,6 +351,8 @@ 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
+ @_start_transaction_state
end
# Clear the new record state and id of a record.
@@ -368,6 +371,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)
self.id = restore_state[:id]
else
View
56 activerecord/test/cases/transactions_test.rb
@@ -268,6 +268,62 @@ def test_invalid_keys_for_transaction
end
end
+ def test_dirty_state_rollback
+ 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_dirty_state_rollback2
+ 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_dirty_state_rollback3
+ assert !@first.approved?
+ @first.approved = true
+ @first.save!
+ Topic.transaction do
+ @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_dirty_state_rollback4
+ 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
Please sign in to comment.
Something went wrong with that request. Please try again.