activerecord-transactionable 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/activerecord/transactionable.rb +1 -26
- data/lib/activerecord/transactionable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6440f14017afb9dcd677d1c44f940204353a0f9
|
4
|
+
data.tar.gz: 3cbec64961e635a42d253d7eac90204939f5c5f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40b2881b02f0cecefb459904c9f191284569405529f1128da17e6e11eccff08878c2b8523371a1f74f7156523191d5c278324960311f1b466907128a85447dac
|
7
|
+
data.tar.gz: bd40bf38a1565891b6dd4522207afab7696e3da2da44b6b2e7ffad71d62ae19488ce2768d5be051cbb9ac1ab70f475e4fb93cabe68536e809d144adcdfaf39a3
|
@@ -73,11 +73,8 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
|
|
73
73
|
yield
|
74
74
|
end
|
75
75
|
else
|
76
|
-
puts "what 1 #{__method__}, transaction_open: #{transaction_open}"
|
77
76
|
error_handler_outside_transaction(object: object, transaction_open: transaction_open, **outside_args) do
|
78
|
-
puts "what 1.2 #{__method__}, transaction_open: #{transaction_open}"
|
79
77
|
run_inside_transaction_block(transaction_args: transaction_args, inside_args: inside_args, lock: lock, transaction_open: transaction_open, object: object) do
|
80
|
-
puts "what 1.3 #{__method__}, transaction_open: #{transaction_open}"
|
81
78
|
yield
|
82
79
|
end
|
83
80
|
end
|
@@ -92,17 +89,13 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
|
|
92
89
|
# Note: with_lock will reload object!
|
93
90
|
# Note: with_lock does not accept arguments like transaction does.
|
94
91
|
object.with_lock do
|
95
|
-
puts "what 4.a #{__method__}, transaction_open: #{transaction_open}"
|
96
92
|
error_handler_inside_transaction(object: object, transaction_open: transaction_open, **inside_args) do
|
97
|
-
puts "what 4.a.1 #{__method__}, transaction_open: #{transaction_open}"
|
98
93
|
yield
|
99
94
|
end
|
100
95
|
end
|
101
96
|
else
|
102
97
|
object.transaction(**transaction_args) do
|
103
|
-
puts "what 4.b #{__method__}, transaction_open: #{transaction_open}"
|
104
98
|
error_handler_inside_transaction(object: object, transaction_open: transaction_open, **inside_args) do
|
105
|
-
puts "what 4.b.1 #{__method__}, transaction_open: #{transaction_open}"
|
106
99
|
yield
|
107
100
|
end
|
108
101
|
end
|
@@ -110,9 +103,7 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
|
|
110
103
|
else
|
111
104
|
raise ArgumentError, "No object to lock!" if lock
|
112
105
|
ActiveRecord::Base.transaction(**transaction_args) do
|
113
|
-
puts "what 4.c #{__method__}, transaction_open: #{transaction_open}"
|
114
106
|
error_handler_inside_transaction(object: object, transaction_open: transaction_open, **inside_args) do
|
115
|
-
puts "what 4.c.1 #{__method__}, transaction_open: #{transaction_open}"
|
116
107
|
yield
|
117
108
|
end
|
118
109
|
end
|
@@ -127,7 +118,6 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
|
|
127
118
|
end
|
128
119
|
|
129
120
|
def error_handler_inside_transaction(object: nil, transaction_open:, **args)
|
130
|
-
puts "what 5 #{__method__}, transaction_open: #{transaction_open}"
|
131
121
|
rescued_errors = Array(args[:rescued_errors])
|
132
122
|
prepared_errors = Array(args[:prepared_errors])
|
133
123
|
retriable_errors = Array(args[:retriable_errors])
|
@@ -137,13 +127,11 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
|
|
137
127
|
already_been_added_to_self, needing_added_to_self = rescued_errors.partition {|error_class| prepared_errors.include?(error_class)}
|
138
128
|
local_context = INSIDE_CONTEXT
|
139
129
|
run_block_with_retry(object, local_context, transaction_open, retriable_errors, reraisable_errors, already_been_added_to_self, needing_added_to_self) do
|
140
|
-
puts "what 5.1 #{__method__}, transaction_open: #{transaction_open}"
|
141
130
|
yield
|
142
131
|
end
|
143
132
|
end
|
144
133
|
|
145
134
|
def error_handler_outside_transaction(object: nil, transaction_open:, **args)
|
146
|
-
puts "what 2.1 #{__method__}, transaction_open: #{transaction_open}"
|
147
135
|
rescued_errors = Array(args[:outside_rescued_errors])
|
148
136
|
prepared_errors = Array(args[:outside_prepared_errors])
|
149
137
|
retriable_errors = Array(args[:outside_retriable_errors])
|
@@ -153,15 +141,13 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
|
|
153
141
|
already_been_added_to_self, needing_added_to_self = rescued_errors.partition {|error_class| prepared_errors.include?(error_class)}
|
154
142
|
local_context = OUTSIDE_CONTEXT
|
155
143
|
run_block_with_retry(object, local_context, transaction_open, retriable_errors, reraisable_errors, already_been_added_to_self, needing_added_to_self) do
|
156
|
-
puts "what 2.2 #{__method__}, transaction_open: #{transaction_open}"
|
157
144
|
yield
|
158
145
|
end
|
159
146
|
end
|
160
147
|
|
161
148
|
def run_block_with_retry(object, local_context, transaction_open, retriable_errors, reraisable_errors, already_been_added_to_self, needing_added_to_self)
|
162
|
-
puts "what 3.1 #{__method__}: local_context: #{local_context}, transaction_open: #{transaction_open}"
|
163
149
|
re_try = false
|
164
|
-
|
150
|
+
begin
|
165
151
|
# If the block we yield to here raises an error that is not caught below the `true` will not get hit.
|
166
152
|
# If the error is rescued higher up, like where the transaction in active record
|
167
153
|
# rescues ActiveRecord::Rollback without re-raising, then transaction_wrapper will return nil
|
@@ -171,10 +157,8 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
|
|
171
157
|
result = yield
|
172
158
|
# When in the outside context we need to preserve the inside result so it bubles up unmolested with the "meaningful" result of the transaction.
|
173
159
|
if transaction_open || local_context == OUTSIDE_CONTEXT
|
174
|
-
puts "what 3.2.a preserving: #{result.inspect}, local_context: #{local_context}, transaction_open: #{transaction_open}"
|
175
160
|
result # <= preserve the meaningful return value. Meaning: transaction succeeded, no errors raised
|
176
161
|
else
|
177
|
-
puts "what 3.2.b no errors! local_context: #{local_context}, transaction_open: #{transaction_open}"
|
178
162
|
true # <= make the return value meaningful. Meaning: transaction succeeded, no errors raised
|
179
163
|
end
|
180
164
|
rescue *reraisable_errors => error
|
@@ -183,7 +167,6 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
|
|
183
167
|
# the parent can still be caught and added to self.errors
|
184
168
|
# Also adds the error to the object if there is an object.
|
185
169
|
transaction_error_logger(object: object, error: error, add_to: nil, additional_message: " [#{transaction_open ? 'nested ' : ''}#{local_context} re-raising!]")
|
186
|
-
puts "what 3.2.c reraisable local_context: #{local_context}, transaction_open: #{transaction_open}"
|
187
170
|
raise error
|
188
171
|
rescue *retriable_errors => error
|
189
172
|
# This will re-run the begin block above
|
@@ -191,28 +174,22 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
|
|
191
174
|
# To avoid the infinite recursion, we track the retry state
|
192
175
|
if re_try
|
193
176
|
transaction_error_logger(object: object, error: error, additional_message: " [#{transaction_open ? 'nested ' : ''}#{local_context} 2nd attempt]")
|
194
|
-
puts "what 3.2.e post-retry local_context: #{local_context}, transaction_open: #{transaction_open}"
|
195
177
|
false # <= make the return value meaningful. Meaning is: transaction failed after two attempts
|
196
178
|
else
|
197
179
|
re_try = true
|
198
180
|
# Not adding error to base when retrying, because otherwise the added error may
|
199
181
|
# prevent the subsequent save from working, in a catch-22
|
200
182
|
transaction_error_logger(object: object, error: error, add_to: nil, additional_message: " [#{transaction_open ? 'nested ' : ''}#{local_context} 1st attempt]")
|
201
|
-
puts "what 3.2.d pre-retry local_context: #{local_context}, transaction_open: #{transaction_open}"
|
202
183
|
retry
|
203
184
|
end
|
204
185
|
rescue *already_been_added_to_self => error
|
205
186
|
# ActiveRecord::RecordInvalid, when done correctly, will have already added the error to object.
|
206
|
-
puts "what 3.2.f already: local_context: #{local_context}, transaction_open: #{transaction_open}"
|
207
187
|
transaction_error_logger(object: nil, error: error, additional_message: " [#{transaction_open ? 'nested ' : ''}#{local_context}]")
|
208
188
|
false # <= make the return value meaningful. Meaning is: transaction failed
|
209
189
|
rescue *needing_added_to_self => error
|
210
|
-
puts "what 3.2.g needing: local_context: #{local_context}, transaction_open: #{transaction_open}"
|
211
190
|
transaction_error_logger(object: object, error: error, additional_message: " [#{transaction_open ? 'nested ' : ''}#{local_context}]")
|
212
191
|
false # <= make the return value meaningful. Meaning is: transaction failed
|
213
192
|
end
|
214
|
-
puts "what 3.3 result: #{result.inspect}"
|
215
|
-
result
|
216
193
|
end
|
217
194
|
|
218
195
|
def transaction_error_logger(object:, error:, add_to: :base, additional_message: nil)
|
@@ -223,10 +200,8 @@ module Activerecord # Note lowercase "r" in Activerecord (different namespace th
|
|
223
200
|
# the specific record that caused the ActiveRecord::RecordInvalid error may be out of scope
|
224
201
|
# Ideally you would rewrite the caller to call transaction_wrapper on a single record (even if updates happen on other records)
|
225
202
|
logger.error("[#{self}.transaction_wrapper] #{error.class}: #{error.message}#{additional_message}")
|
226
|
-
puts("[#{self}.transaction_wrapper] #{error.class}: #{error.message}#{additional_message}")
|
227
203
|
else
|
228
204
|
logger.error("[#{self}.transaction_wrapper] On #{object.class} #{error.class}: #{error.message}#{additional_message}")
|
229
|
-
puts("[#{self}.transaction_wrapper] On #{object.class} #{error.class}: #{error.message}#{additional_message}")
|
230
205
|
object.errors.add(add_to, error.message) unless add_to.nil?
|
231
206
|
end
|
232
207
|
end
|