Sha256: f87da6b165b2bdc0ee2e4cc8249a8574a620d6c52f686423741d0c55ce4c32dc

Contents?: true

Size: 1.44 KB

Versions: 44

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require_dependency "renalware"

module Renalware
  module TransactionRetry
    extend ActiveSupport::Concern

    included do
      # Retry a transaction automatically when an ActiveRecord::PreparedStatementCacheExpired error
      # is raised. This handles a specific scenario where there is a migration run during a deploy
      # that changes one or more columns used in a cached PostgreSQL prepared statement.
      # We rescue it here and retry (once) the txn to prevent users getting an error when they try
      # to select/update data moments after the deploy.
      #
      # Do not use this for transactions with side-effects unless it is acceptable
      # for these side-effects to occasionally happen twice.
      #
      # Note we define this method in the included block (not the class_methods block as you would
      # expect) becuase in the latter case a client calling #transaction would get the including
      # class's transaction method, not the overriding one defined here. Using 'included' allows
      # use to get around this. See comments in
      # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/concern.rb
      #
      def self.transaction(*args, &block)
        retried ||= false
        super
      rescue ActiveRecord::PreparedStatementCacheExpired
        if retried
          raise
        else
          retried = true
          retry
        end
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.1.0 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.167 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.166 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.165 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.164 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.163 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.162 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.161 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.160 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.159 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.158 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.157 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.156 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.155 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.153 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.152 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.151 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.149 app/models/concerns/renalware/transaction_retry.rb
renalware-core-2.0.148 app/models/concerns/renalware/transaction_retry.rb