activerecord-transactionable 3.0.2 → 3.0.4
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
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +133 -0
- data/LICENSE.md +11 -0
- data/README.md +412 -215
- data/lib/activerecord/transactionable/result.rb +1 -1
- data/lib/activerecord/transactionable/version.rb +7 -1
- data/lib/activerecord/transactionable.rb +93 -35
- data/sig/activerecord/transactionable.rbs +8 -0
- data.tar.gz.sig +0 -0
- metadata +228 -73
- metadata.gz.sig +0 -0
- data/CODE_OF_CONDUCT.md +0 -84
- data/CONTRIBUTING.md +0 -26
- data/LICENSE +0 -21
- data/SECURITY.md +0 -14
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module Activerecord
|
|
4
4
|
module Transactionable
|
|
5
|
-
|
|
5
|
+
# Version namespace for this gem.
|
|
6
|
+
module Version
|
|
7
|
+
# Current gem version.
|
|
8
|
+
VERSION = "3.0.4"
|
|
9
|
+
end
|
|
10
|
+
# Current gem version exposed at the traditional constant location.
|
|
11
|
+
VERSION = Version::VERSION # Traditional Constant Location
|
|
6
12
|
end
|
|
7
13
|
end
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# External gems
|
|
4
|
+
require "version_gem"
|
|
3
5
|
require "active_model"
|
|
4
6
|
require "active_record"
|
|
5
|
-
# apparently needed for Rails 4.0 compatibility with rspec, when
|
|
6
|
-
# this gem is loaded before the rails gem by bundler, as will happen when you
|
|
7
|
-
# keep your Gemfile sorted alphabetically.
|
|
8
7
|
require "active_record/validations"
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
# This gem
|
|
11
10
|
require "activerecord/transactionable/result"
|
|
11
|
+
require_relative "transactionable/version"
|
|
12
12
|
|
|
13
13
|
# Note lowercase "r" in Activerecord (different namespace than rails' module)
|
|
14
14
|
module Activerecord
|
|
@@ -54,10 +54,6 @@ module Activerecord
|
|
|
54
54
|
INSIDE_CONTEXT = "inside"
|
|
55
55
|
OUTSIDE_CONTEXT = "outside"
|
|
56
56
|
|
|
57
|
-
def transaction_wrapper(**args, &block)
|
|
58
|
-
self.class.transaction_wrapper(object: self, **args, &block)
|
|
59
|
-
end
|
|
60
|
-
|
|
61
57
|
module ClassMethods
|
|
62
58
|
def transaction_wrapper(object: nil, **args)
|
|
63
59
|
lock = args.delete(:lock)
|
|
@@ -67,13 +63,13 @@ module Activerecord
|
|
|
67
63
|
transaction_open = ActiveRecord::Base.connection.transaction_open?
|
|
68
64
|
unless args.keys.empty?
|
|
69
65
|
raise ArgumentError,
|
|
70
|
-
|
|
66
|
+
"#{self} does not know how to handle arguments: #{args.keys.inspect}"
|
|
71
67
|
end
|
|
72
68
|
if ERRORS_TO_DISALLOW_INSIDE_TRANSACTION.detect do |error|
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
inside_args.values.flatten.uniq.include?(error)
|
|
70
|
+
end
|
|
75
71
|
raise ArgumentError,
|
|
76
|
-
|
|
72
|
+
"#{self} should not rescue #{ERRORS_TO_DISALLOW_INSIDE_TRANSACTION.inspect} inside a transaction: #{inside_args.keys.inspect}"
|
|
77
73
|
end
|
|
78
74
|
|
|
79
75
|
if transaction_open
|
|
@@ -84,10 +80,18 @@ module Activerecord
|
|
|
84
80
|
logger.warn("[#{self}.transaction_wrapper] Opening a nested transaction. Setting #{REQUIRES_NEW}: true")
|
|
85
81
|
end
|
|
86
82
|
end
|
|
87
|
-
error_handler_outside_transaction(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
83
|
+
error_handler_outside_transaction(
|
|
84
|
+
object: object,
|
|
85
|
+
transaction_open: transaction_open,
|
|
86
|
+
**outside_args
|
|
87
|
+
) do |outside_is_retry|
|
|
88
|
+
run_inside_transaction_block(
|
|
89
|
+
transaction_args: transaction_args,
|
|
90
|
+
inside_args: inside_args,
|
|
91
|
+
lock: lock,
|
|
92
|
+
transaction_open: transaction_open,
|
|
93
|
+
object: object
|
|
94
|
+
) do |is_retry|
|
|
91
95
|
# regardless of the retry being inside or outside the transaction, it is still a retry.
|
|
92
96
|
yield outside_is_retry || is_retry
|
|
93
97
|
end
|
|
@@ -102,13 +106,17 @@ module Activerecord
|
|
|
102
106
|
# NOTE: with_lock will reload object!
|
|
103
107
|
# Note: with_lock does not accept arguments like transaction does.
|
|
104
108
|
object.with_lock do
|
|
105
|
-
error_handler_inside_transaction(
|
|
106
|
-
|
|
109
|
+
error_handler_inside_transaction(
|
|
110
|
+
object: object, transaction_open: transaction_open, **inside_args,
|
|
111
|
+
&block
|
|
112
|
+
)
|
|
107
113
|
end
|
|
108
114
|
else
|
|
109
115
|
object.transaction(**transaction_args) do
|
|
110
|
-
error_handler_inside_transaction(
|
|
111
|
-
|
|
116
|
+
error_handler_inside_transaction(
|
|
117
|
+
object: object, transaction_open: transaction_open, **inside_args,
|
|
118
|
+
&block
|
|
119
|
+
)
|
|
112
120
|
end
|
|
113
121
|
end
|
|
114
122
|
else
|
|
@@ -139,8 +147,17 @@ module Activerecord
|
|
|
139
147
|
prepared_errors.include?(error_class)
|
|
140
148
|
end
|
|
141
149
|
local_context = INSIDE_CONTEXT
|
|
142
|
-
run_block_with_retry(
|
|
143
|
-
|
|
150
|
+
run_block_with_retry(
|
|
151
|
+
object,
|
|
152
|
+
local_context,
|
|
153
|
+
transaction_open,
|
|
154
|
+
retriable_errors,
|
|
155
|
+
reraisable_errors,
|
|
156
|
+
already_been_added_to_self,
|
|
157
|
+
needing_added_to_self,
|
|
158
|
+
num_retry_attempts,
|
|
159
|
+
&block
|
|
160
|
+
)
|
|
144
161
|
end
|
|
145
162
|
|
|
146
163
|
def error_handler_outside_transaction(transaction_open:, object: nil, **args, &block)
|
|
@@ -155,8 +172,17 @@ module Activerecord
|
|
|
155
172
|
prepared_errors.include?(error_class)
|
|
156
173
|
end
|
|
157
174
|
local_context = OUTSIDE_CONTEXT
|
|
158
|
-
run_block_with_retry(
|
|
159
|
-
|
|
175
|
+
run_block_with_retry(
|
|
176
|
+
object,
|
|
177
|
+
local_context,
|
|
178
|
+
transaction_open,
|
|
179
|
+
retriable_errors,
|
|
180
|
+
reraisable_errors,
|
|
181
|
+
already_been_added_to_self,
|
|
182
|
+
needing_added_to_self,
|
|
183
|
+
num_retry_attempts,
|
|
184
|
+
&block
|
|
185
|
+
)
|
|
160
186
|
end
|
|
161
187
|
|
|
162
188
|
def run_block_with_retry(object, local_context, transaction_open, retriable_errors, reraisable_errors, already_been_added_to_self, needing_added_to_self, num_retry_attempts)
|
|
@@ -173,7 +199,7 @@ module Activerecord
|
|
|
173
199
|
# We pass the retry state along to yield so that the code implementing
|
|
174
200
|
# the transaction_wrapper can switch behavior on a retry
|
|
175
201
|
# (e.g. create => find)
|
|
176
|
-
result = yield (re_try == false ? re_try : attempt)
|
|
202
|
+
result = yield ((re_try == false) ? re_try : attempt)
|
|
177
203
|
# When in the outside context we need to preserve the inside result so it bubbles up unmolested with the "meaningful" result of the transaction.
|
|
178
204
|
if result.is_a?(Activerecord::Transactionable::Result)
|
|
179
205
|
result # <= preserve the meaningful return value
|
|
@@ -185,8 +211,14 @@ module Activerecord
|
|
|
185
211
|
# if that is in the intended behavior, and this way a specific child of StandardError can be reraised while
|
|
186
212
|
# the parent can still be caught and added to self.errors
|
|
187
213
|
# Also adds the error to the object if there is an object.
|
|
188
|
-
transaction_error_logger(
|
|
189
|
-
|
|
214
|
+
transaction_error_logger(
|
|
215
|
+
object: object,
|
|
216
|
+
error: e,
|
|
217
|
+
result: nil,
|
|
218
|
+
attempt: attempt,
|
|
219
|
+
add_to: nil,
|
|
220
|
+
additional_message: " [#{"nested " if transaction_open}#{local_context} re-raising!]"
|
|
221
|
+
)
|
|
190
222
|
raise e
|
|
191
223
|
rescue *retriable_errors => e
|
|
192
224
|
# This will re-run the begin block above
|
|
@@ -194,27 +226,45 @@ module Activerecord
|
|
|
194
226
|
# To avoid the infinite recursion, we track the retry state
|
|
195
227
|
if attempt >= num_retry_attempts
|
|
196
228
|
result = Activerecord::Transactionable::Result.new(false, context: local_context, transaction_open: transaction_open, error: e, attempt: attempt, type: "retriable") # <= make the return value meaningful. Meaning is: transaction failed after <attempt> attempts
|
|
197
|
-
transaction_error_logger(
|
|
198
|
-
|
|
229
|
+
transaction_error_logger(
|
|
230
|
+
object: object,
|
|
231
|
+
error: e,
|
|
232
|
+
result: result,
|
|
233
|
+
additional_message: " [#{"nested " if transaction_open}#{local_context}]"
|
|
234
|
+
)
|
|
199
235
|
result
|
|
200
236
|
else
|
|
201
237
|
re_try = true
|
|
202
238
|
# Not adding error to base when retrying, because otherwise the added error may
|
|
203
239
|
# prevent the subsequent save from working, in a catch-22
|
|
204
|
-
transaction_error_logger(
|
|
205
|
-
|
|
240
|
+
transaction_error_logger(
|
|
241
|
+
object: object,
|
|
242
|
+
error: e,
|
|
243
|
+
result: nil,
|
|
244
|
+
attempt: attempt,
|
|
245
|
+
add_to: nil,
|
|
246
|
+
additional_message: " [#{"nested " if transaction_open}#{local_context}]"
|
|
247
|
+
)
|
|
206
248
|
retry
|
|
207
249
|
end
|
|
208
250
|
rescue *already_been_added_to_self => e
|
|
209
251
|
# ActiveRecord::RecordInvalid, when done correctly, will have already added the error to object.
|
|
210
252
|
result = Activerecord::Transactionable::Result.new(false, context: local_context, transaction_open: transaction_open, error: e, attempt: attempt, type: "already_added") # <= make the return value meaningful. Meaning is: transaction failed
|
|
211
|
-
transaction_error_logger(
|
|
212
|
-
|
|
253
|
+
transaction_error_logger(
|
|
254
|
+
object: nil,
|
|
255
|
+
error: e,
|
|
256
|
+
result: result,
|
|
257
|
+
additional_message: " [#{"nested " if transaction_open}#{local_context}]"
|
|
258
|
+
)
|
|
213
259
|
result
|
|
214
260
|
rescue *needing_added_to_self => e
|
|
215
261
|
result = Activerecord::Transactionable::Result.new(false, context: local_context, transaction_open: transaction_open, error: e, attempt: attempt, type: "needing_added") # <= make the return value meaningful. Meaning is: transaction failed
|
|
216
|
-
transaction_error_logger(
|
|
217
|
-
|
|
262
|
+
transaction_error_logger(
|
|
263
|
+
object: object,
|
|
264
|
+
error: e,
|
|
265
|
+
result: result,
|
|
266
|
+
additional_message: " [#{"nested " if transaction_open}#{local_context}]"
|
|
267
|
+
)
|
|
218
268
|
result
|
|
219
269
|
end
|
|
220
270
|
end
|
|
@@ -233,5 +283,13 @@ module Activerecord
|
|
|
233
283
|
end
|
|
234
284
|
end
|
|
235
285
|
end
|
|
286
|
+
|
|
287
|
+
def transaction_wrapper(**args, &block)
|
|
288
|
+
self.class.transaction_wrapper(object: self, **args, &block)
|
|
289
|
+
end
|
|
236
290
|
end
|
|
237
291
|
end
|
|
292
|
+
|
|
293
|
+
Activerecord::Transactionable::Version.class_eval do
|
|
294
|
+
extend VersionGem::Basic
|
|
295
|
+
end
|
data.tar.gz.sig
ADDED
|
Binary file
|