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.
@@ -39,7 +39,7 @@ module Activerecord
39
39
  end
40
40
 
41
41
  def to_s(skip_error: nil)
42
- to_h(skip_error: skip_error).to_s
42
+ to_h(skip_error: skip_error).map { |key, value| "#{key.inspect}=>#{value.inspect}" }.join(", ").prepend("{").concat("}")
43
43
  end
44
44
  end
45
45
  end
@@ -2,6 +2,12 @@
2
2
 
3
3
  module Activerecord
4
4
  module Transactionable
5
- VERSION = "3.0.2"
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
- require "activerecord/transactionable/version"
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
- "#{self} does not know how to handle arguments: #{args.keys.inspect}"
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
- inside_args.values.flatten.uniq.include?(error)
74
- end
69
+ inside_args.values.flatten.uniq.include?(error)
70
+ end
75
71
  raise ArgumentError,
76
- "#{self} should not rescue #{ERRORS_TO_DISALLOW_INSIDE_TRANSACTION.inspect} inside a transaction: #{inside_args.keys.inspect}"
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(object: object, transaction_open: transaction_open,
88
- **outside_args) do |outside_is_retry|
89
- run_inside_transaction_block(transaction_args: transaction_args, inside_args: inside_args, lock: lock,
90
- transaction_open: transaction_open, object: object) do |is_retry|
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(object: object, transaction_open: transaction_open, **inside_args,
106
- &block)
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(object: object, transaction_open: transaction_open, **inside_args,
111
- &block)
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(object, local_context, transaction_open, retriable_errors, reraisable_errors,
143
- already_been_added_to_self, needing_added_to_self, num_retry_attempts, &block)
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(object, local_context, transaction_open, retriable_errors, reraisable_errors,
159
- already_been_added_to_self, needing_added_to_self, num_retry_attempts, &block)
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(object: object, error: e, result: nil, attempt: attempt, add_to: nil,
189
- additional_message: " [#{transaction_open ? "nested " : ""}#{local_context} re-raising!]")
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(object: object, error: e, result: result,
198
- additional_message: " [#{transaction_open ? "nested " : ""}#{local_context}]")
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(object: object, error: e, result: nil, attempt: attempt, add_to: nil,
205
- additional_message: " [#{transaction_open ? "nested " : ""}#{local_context}]")
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(object: nil, error: e, result: result,
212
- additional_message: " [#{transaction_open ? "nested " : ""}#{local_context}]")
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(object: object, error: e, result: result,
217
- additional_message: " [#{transaction_open ? "nested " : ""}#{local_context}]")
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
@@ -0,0 +1,8 @@
1
+ module Activerecord
2
+ module Transactionable
3
+ module Version
4
+ VERSION: String
5
+ end
6
+ VERSION: String
7
+ end
8
+ end
data.tar.gz.sig ADDED
Binary file