Skip to content

Commit ea0f0a2

Browse files
committed
Pass the transaction to the transaction.active_record event
1 parent 91a441b commit ea0f0a2

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

activerecord/lib/active_record/connection_adapters/abstract/transaction.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def initialize(connection, isolation: nil, joinable: true, run_commit_callbacks:
141141
@run_commit_callbacks = run_commit_callbacks
142142
@lazy_enrollment_records = nil
143143
@dirty = false
144-
@instrumenter = TransactionInstrumenter.new(connection: connection)
144+
@instrumenter = TransactionInstrumenter.new(connection: connection, transaction: self)
145145
end
146146

147147
def dirty!

activerecord/test/cases/transaction_instrumentation_test.rb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ def test_transaction_instrumentation_on_commit
1111
topic = topics(:fifth)
1212

1313
notified = false
14+
expected_transaction = nil
15+
1416
subscriber = ActiveSupport::Notifications.subscribe("transaction.active_record") do |event|
1517
assert event.payload[:connection]
18+
assert_same expected_transaction, event.payload[:transaction]
1619
assert_equal :commit, event.payload[:outcome]
1720
notified = true
1821
end
1922

20-
ActiveRecord::Base.transaction do
23+
ActiveRecord::Base.transaction do |transaction|
24+
expected_transaction = transaction
2125
topic.update(title: "Ruby on Rails")
2226
end
2327

@@ -30,13 +34,17 @@ def test_transaction_instrumentation_on_rollback
3034
topic = topics(:fifth)
3135

3236
notified = false
37+
expected_transaction = nil
38+
3339
subscriber = ActiveSupport::Notifications.subscribe("transaction.active_record") do |event|
3440
assert event.payload[:connection]
41+
assert_same expected_transaction, event.payload[:transaction]
3542
assert_equal :rollback, event.payload[:outcome]
3643
notified = true
3744
end
3845

39-
ActiveRecord::Base.transaction do
46+
ActiveRecord::Base.transaction do |transaction|
47+
expected_transaction = transaction
4048
topic.update(title: "Ruby on Rails")
4149
raise ActiveRecord::Rollback
4250
end
@@ -54,17 +62,24 @@ def test_transaction_instrumentation_with_savepoints
5462
events << event
5563
end
5664

57-
ActiveRecord::Base.transaction do
65+
real_transaction = savepoint_transaction = nil
66+
ActiveRecord::Base.transaction do |transaction|
67+
real_transaction = transaction
5868
topic.update(title: "Sinatra")
59-
ActiveRecord::Base.transaction(requires_new: true) do
69+
ActiveRecord::Base.transaction(requires_new: true) do |transaction|
70+
savepoint_transaction = transaction
6071
topic.update(title: "Ruby on Rails")
6172
end
6273
end
6374

6475
assert_equal 2, events.count
65-
savepoint, real = events
66-
assert_equal :commit, savepoint.payload[:outcome]
67-
assert_equal :commit, real.payload[:outcome]
76+
savepoint_event, real_event = events
77+
78+
assert_same savepoint_transaction, savepoint_event.payload[:transaction]
79+
assert_equal :commit, savepoint_event.payload[:outcome]
80+
81+
assert_same real_transaction, real_event.payload[:transaction]
82+
assert_equal :commit, real_event.payload[:outcome]
6883
ensure
6984
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
7085
end

guides/source/active_support_instrumentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ This event is emmited for every transaction to the database.
413413
| Key | Value |
414414
| -------------------- | ---------------------------------------------------- |
415415
| `:connection` | Connection object |
416+
| `:transaction` | Transaction object
416417
| `:outcome` | `:commit`, `:rollback`, `:restart`, or `:incomplete` |
417418

418419
### Action Mailer

0 commit comments

Comments
 (0)