Sha256: 4fcfb043fc70427fd718b3740dba677f0c779e4587284e636f4f1caf14f86d4d

Contents?: true

Size: 847 Bytes

Versions: 8

Compression:

Stored size: 847 Bytes

Contents

require 'pg'
module RedshiftPG
  class Connection
    attr_reader :config

    SQL_TO_PG_KEY_MAP = {
      'username' => 'user',
      'database' => 'dbname'
    }

    PG_INCLUDE_KEYS = %w(host dbname port user password)

    def initialize(config)
      @config = config
    end

    def reconnect_on_failure(&block)
      begin
        return yield
      rescue PG::UnableToSend, PG::ConnectionBad
        pg_connection.reset
        return yield
      end
    end

    def pg_connection
      @pg_connection ||= connect!
    end

    private
    def connect!
      ::PG.connect(pg_config)
    end

    def pg_config
      kvs = @config.map do |k, v|
        converted_key = SQL_TO_PG_KEY_MAP.fetch(k, k)
        next unless PG_INCLUDE_KEYS.include?(converted_key)
        [converted_key, v]
      end.compact
      Hash[kvs]
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
aleph_analytics-0.1.0 lib/redshift_pg/connection.rb
aleph_analytics-0.0.6 lib/redshift_pg/connection.rb
aleph_analytics-0.0.5 lib/redshift_pg/connection.rb
aleph_analytics-0.0.4 lib/redshift_pg/connection.rb
aleph_analytics-0.0.3 lib/redshift_pg/connection.rb
aleph_analytics-0.0.2 lib/redshift_pg/connection.rb
aleph_analytics-0.0.1.alpha lib/redshift_pg/connection.rb
aleph_analytics-0.0.0.alpha lib/redshift_pg/connection.rb