File tree Expand file tree Collapse file tree 3 files changed +73
-6
lines changed
lib/active_record/connection_adapters/abstract Expand file tree Collapse file tree 3 files changed +73
-6
lines changed Original file line number Diff line number Diff line change
1
+ * Deprecate ` set_state ` method in ` TransactionState `
2
+
3
+ Deprecated the ` set_state ` method in favor of setting the state via specific methods. If you need to mark the
4
+ state of the transaction you can now use ` rollback! ` , ` commit! ` or ` nullify! ` instead of
5
+ ` set_state(:rolledback) ` , ` set_state(:committed) ` , or ` set_state(nil) ` .
6
+
7
+ * Eileen M. Uchitelle* , * Aaron Patterson*
8
+
1
9
* Deprecate delegating to ` arel ` in ` Relation ` .
2
10
3
11
* Ryuta Kamizono*
Original file line number Diff line number Diff line change 1
1
module ActiveRecord
2
2
module ConnectionAdapters
3
3
class TransactionState
4
- VALID_STATES = Set . new ( [ :committed , :rolledback , nil ] )
5
-
6
4
def initialize ( state = nil )
7
5
@state = state
8
6
end
@@ -24,10 +22,33 @@ def completed?
24
22
end
25
23
26
24
def set_state ( state )
27
- unless VALID_STATES . include? ( state )
25
+ ActiveSupport ::Deprecation . warn ( <<-MSG . squish )
26
+ The set_state method is deprecated and will be removed in
27
+ Rails 5.2. Please use rollback! or commit! to set transaction
28
+ state directly.
29
+ MSG
30
+ case state
31
+ when :rolledback
32
+ rollback!
33
+ when :committed
34
+ commit!
35
+ when nil
36
+ nullify!
37
+ else
28
38
raise ArgumentError , "Invalid transaction state: #{ state } "
29
39
end
30
- @state = state
40
+ end
41
+
42
+ def rollback!
43
+ @state = :rolledback
44
+ end
45
+
46
+ def commit!
47
+ @state = :committed
48
+ end
49
+
50
+ def nullify!
51
+ @state = nil
31
52
end
32
53
end
33
54
@@ -57,7 +78,7 @@ def add_record(record)
57
78
end
58
79
59
80
def rollback
60
- @state . set_state ( :rolledback )
81
+ @state . rollback!
61
82
end
62
83
63
84
def rollback_records
@@ -72,7 +93,7 @@ def rollback_records
72
93
end
73
94
74
95
def commit
75
- @state . set_state ( :committed )
96
+ @state . commit!
76
97
end
77
98
78
99
def before_commit_records
Original file line number Diff line number Diff line change @@ -725,6 +725,44 @@ def test_transactions_state_from_commit
725
725
assert transaction . state . committed?
726
726
end
727
727
728
+ def test_set_state_method_is_deprecated
729
+ connection = Topic . connection
730
+ transaction = ActiveRecord ::ConnectionAdapters ::TransactionManager . new ( connection ) . begin_transaction
731
+
732
+ transaction . commit
733
+
734
+ assert_deprecated do
735
+ transaction . state . set_state ( :rolledback )
736
+ end
737
+ end
738
+
739
+ def test_mark_transaction_state_as_committed
740
+ connection = Topic . connection
741
+ transaction = ActiveRecord ::ConnectionAdapters ::TransactionManager . new ( connection ) . begin_transaction
742
+
743
+ transaction . rollback
744
+
745
+ assert_equal :committed , transaction . state . commit!
746
+ end
747
+
748
+ def test_mark_transaction_state_as_rolledback
749
+ connection = Topic . connection
750
+ transaction = ActiveRecord ::ConnectionAdapters ::TransactionManager . new ( connection ) . begin_transaction
751
+
752
+ transaction . commit
753
+
754
+ assert_equal :rolledback , transaction . state . rollback!
755
+ end
756
+
757
+ def test_mark_transaction_state_as_nil
758
+ connection = Topic . connection
759
+ transaction = ActiveRecord ::ConnectionAdapters ::TransactionManager . new ( connection ) . begin_transaction
760
+
761
+ transaction . commit
762
+
763
+ assert_equal nil , transaction . state . nullify!
764
+ end
765
+
728
766
def test_transaction_rollback_with_primarykeyless_tables
729
767
connection = ActiveRecord ::Base . connection
730
768
connection . create_table ( :transaction_without_primary_keys , force : true , id : false ) do |t |
You can’t perform that action at this time.
0 commit comments