File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed
connection_adapters/abstract Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
+ require "active_support/core_ext/digest"
4
+
3
5
module ActiveRecord
4
6
module ConnectionAdapters
5
7
# = Active Record Connection Adapters Transaction State
@@ -119,6 +121,7 @@ def materialized?; false; end
119
121
def before_commit ; yield ; end
120
122
def after_commit ; yield ; end
121
123
def after_rollback ; end # noop
124
+ def uuid ; Digest ::UUID . nil_uuid ; end
122
125
end
123
126
124
127
class Transaction < ActiveRecord ::Transaction # :nodoc:
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
+ require "active_support/core_ext/digest"
4
+
3
5
module ActiveRecord
4
6
class Transaction
5
7
class Callback # :nodoc:
@@ -23,6 +25,7 @@ def after_rollback
23
25
24
26
def initialize # :nodoc:
25
27
@callbacks = nil
28
+ @uuid = nil
26
29
end
27
30
28
31
# Registers a block to be called before the current transaction is fully committed.
@@ -60,6 +63,11 @@ def after_rollback(&block)
60
63
( @callbacks ||= [ ] ) << Callback . new ( :after_rollback , block )
61
64
end
62
65
66
+ # Returns a UUID for this transaction.
67
+ def uuid
68
+ @uuid ||= Digest ::UUID . uuid_v4
69
+ end
70
+
63
71
protected
64
72
def append_callbacks ( callbacks )
65
73
( @callbacks ||= [ ] ) . concat ( callbacks )
Original file line number Diff line number Diff line change @@ -1650,6 +1650,29 @@ def test_no_automatic_savepoint_for_inner_transaction
1650
1650
end
1651
1651
end if Topic . lease_connection . supports_savepoints?
1652
1652
1653
+ class TransactionUUIDTest < ActiveRecord ::TestCase
1654
+ def test_the_uuid_is_lazily_computed
1655
+ Topic . transaction do
1656
+ transaction = Topic . connection . current_transaction
1657
+ assert_nil transaction . instance_variable_get ( :@uuid )
1658
+ end
1659
+ end
1660
+
1661
+ def test_the_uuid_for_regular_transactions_is_generated_and_memoized
1662
+ Topic . transaction do
1663
+ transaction = Topic . connection . current_transaction
1664
+ uuid = transaction . uuid
1665
+ assert_match ( /\A [[:xdigit:]]{8}-(?:[[:xdigit:]]{4}-){3}[[:xdigit:]]{12}\z / , uuid )
1666
+ assert_equal uuid , transaction . uuid
1667
+ end
1668
+ end
1669
+
1670
+ def test_the_uuid_for_null_transactions_is_the_nil_uuid
1671
+ null_transaction = ActiveRecord ::ConnectionAdapters ::TransactionManager ::NULL_TRANSACTION
1672
+ assert_equal Digest ::UUID . nil_uuid , null_transaction . uuid
1673
+ end
1674
+ end
1675
+
1653
1676
class ConcurrentTransactionTest < ActiveRecord ::TestCase
1654
1677
if ActiveRecord ::Base . lease_connection . supports_transaction_isolation? && !current_adapter? ( :SQLite3Adapter )
1655
1678
self . use_transactional_tests = false
You can’t perform that action at this time.
0 commit comments