Skip to content

Commit

Permalink
fix double splat bug
Browse files Browse the repository at this point in the history
ActiveRecord::Base.transaction expects a hash, but the lib using
single splat.

With Ruby 3 or above, this caused errors such as:

ArgumentError:
  wrong number of arguments (given 1, expected 0)
  # gems/3.0.0/gems/activerecord-6.1.7.6/lib/active_record/transactions.rb:208:in `transaction'
  # gems/3.0.0/gems/transaction_retry-1.0.3/lib/transaction_retry/active_record/base.rb:32:in `transaction_with_retry'
  • Loading branch information
nathanstitt committed Aug 24, 2023
1 parent b1c9fee commit 9184c88
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### 1.2.0 - 2023-08-24
* Forked and renamed to OpenStaxTransactionRetry
* Bug with calling
* Fix bug with calling overloaded transaction with a hash


### 1.1.0 - 2019-06-17
Expand Down
11 changes: 7 additions & 4 deletions lib/open_stax_transaction_retry/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ class << self

module ClassMethods
# rubocop:todo Metrics/PerceivedComplexity
def transaction_with_retry(*objects, &block) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def transaction_with_retry(**objects, &block) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
retry_count = 0

opts = if objects.last.is_a? Hash
opts = if objects.is_a? Hash
objects
elsif objects.is_a?(Array) && objects.last.is_a?(Hash)
objects.last
else
{}
end

retry_on = opts.delete(:retry_on)
retry_on = opts.delete(:retry_on) || OpenStaxTransactionRetry.retry_on
max_retries = opts.delete(:max_retries) || OpenStaxTransactionRetry.max_retries
before_retry = opts.delete(:before_retry) || OpenStaxTransactionRetry.before_retry

begin
transaction_without_retry(*objects, &block)
transaction_without_retry(**objects, &block)
rescue ::ActiveRecord::TransactionIsolationConflict, *retry_on => e
raise if retry_count >= max_retries
raise if tr_in_nested_transaction?
Expand Down

0 comments on commit 9184c88

Please sign in to comment.