Skip to content

Commit

Permalink
Merge pull request #32911 from eugeneius/finalize_transaction_record_…
Browse files Browse the repository at this point in the history
…state

Finalize transaction record state after real transaction
  • Loading branch information
kamipo committed May 23, 2018
1 parent 40ef775 commit d655790
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
Expand Up @@ -17,11 +17,19 @@ def finalized?
end

def committed?
@state == :committed
@state == :committed || @state == :fully_committed
end

def fully_committed?
@state == :fully_committed
end

def rolledback?
@state == :rolledback
@state == :rolledback || @state == :fully_rolledback
end

def fully_rolledback?
@state == :fully_rolledback
end

def fully_completed?
Expand Down Expand Up @@ -55,10 +63,19 @@ def rollback!
@state = :rolledback
end

def full_rollback!
@children.each { |c| c.rollback! }
@state = :fully_rolledback
end

def commit!
@state = :committed
end

def full_commit!
@state = :fully_committed
end

def nullify!
@state = nil
end
Expand Down Expand Up @@ -89,10 +106,6 @@ def add_record(record)
records << record
end

def rollback
@state.rollback!
end

def rollback_records
ite = records.uniq
while record = ite.shift
Expand All @@ -104,10 +117,6 @@ def rollback_records
end
end

def commit
@state.commit!
end

def before_commit_records
records.uniq.each(&:before_committed!) if @run_commit_callbacks
end
Expand Down Expand Up @@ -146,12 +155,12 @@ def initialize(connection, savepoint_name, parent_transaction, options, *args)

def rollback
connection.rollback_to_savepoint(savepoint_name)
super
@state.rollback!
end

def commit
connection.release_savepoint(savepoint_name)
super
@state.commit!
end

def full_rollback?; false; end
Expand All @@ -169,12 +178,12 @@ def initialize(connection, options, *args)

def rollback
connection.rollback_db_transaction
super
@state.full_rollback!
end

def commit
connection.commit_db_transaction
super
@state.full_commit!
end
end

Expand Down
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/transactions.rb
Expand Up @@ -485,7 +485,8 @@ def sync_with_transaction_state

def update_attributes_from_transaction_state(transaction_state)
if transaction_state && transaction_state.finalized?
restore_transaction_record_state if transaction_state.rolledback?
restore_transaction_record_state(transaction_state.fully_rolledback?) if transaction_state.rolledback?
force_clear_transaction_record_state if transaction_state.fully_committed?
clear_transaction_record_state if transaction_state.fully_completed?
end
end
Expand Down
29 changes: 29 additions & 0 deletions activerecord/test/cases/transactions_test.rb
Expand Up @@ -679,6 +679,35 @@ def test_restore_frozen_state_after_double_destroy
assert_not_predicate topic, :frozen?
end

def test_restore_new_record_after_double_save
topic = Topic.new

Topic.transaction do
topic.save!
topic.save!
raise ActiveRecord::Rollback
end

assert_predicate topic, :new_record?
end

def test_dont_restore_new_record_in_subsequent_transaction
topic = Topic.new

Topic.transaction do
topic.save!
topic.save!
end

Topic.transaction do
topic.save!
raise ActiveRecord::Rollback
end

assert_predicate topic, :persisted?
assert_not_predicate topic, :new_record?
end

def test_restore_id_after_rollback
topic = Topic.new

Expand Down

0 comments on commit d655790

Please sign in to comment.