Sha256: 234c79f8e621941224e6708f6ff18b27bf6953a6ac49eecaff31a6e28d3cd223

Contents?: true

Size: 972 Bytes

Versions: 1

Compression:

Stored size: 972 Bytes

Contents

require_relative 'pg_reconnect/version'

require 'active_record'
require 'active_record/connection_adapters/postgresql_adapter'

class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

  RECONNECTABLE_METHODS = [
    :execute,
    :exec_query,
    :exec_insert,
    :exec_update,
    :exec_delete,
    :query
  ].freeze

  RECONNECTABLE_METHODS.each do |method|
    unsafe_method = "__#{method}_without_reconnection__"

    alias_method unsafe_method, method

    private unsafe_method

    define_method method do |*args, &block|
      begin
        if reconnection_required?
          @reconnection_required = false
          reconnect!
        end
        send(unsafe_method, *args, &block)
      rescue ActiveRecord::StatementInvalid, ActiveRecord::ConnectionNotEstablished => ex
        @reconnection_required = ex.cause.is_a? PG::ConnectionBad
        raise ex
      end
    end
  end

  def reconnection_required?
    @reconnection_required ||= false
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record-pg_reconnect-1.0.1 lib/active_record/pg_reconnect.rb