class PgHero::Database

Attributes

config[R]
id[R]

Public Class Methods

name() click to toggle source
# File lib/pghero/database.rb, line 154
def self.name
  "PgHero::Connection::Database#{object_id}"
end
new(id, config) click to toggle source
# File lib/pghero/database.rb, line 23
def initialize(id, config)
  @id = id
  @config = config || {}

  # preload model to ensure only one connection pool
  # this doesn't actually start any connections
  @adapter_checked = false
  @connection_model = build_connection_model
end

Public Instance Methods

aws_access_key_id() click to toggle source
# File lib/pghero/database.rb, line 70
def aws_access_key_id
  config["aws_access_key_id"] || PgHero.config["aws_access_key_id"] || ENV["PGHERO_ACCESS_KEY_ID"] || ENV["AWS_ACCESS_KEY_ID"]
end
aws_db_instance_identifier() click to toggle source

environment variable is only used if no config file

# File lib/pghero/database.rb, line 83
def aws_db_instance_identifier
  @aws_db_instance_identifier ||= config["aws_db_instance_identifier"] || config["db_instance_identifier"]
end
aws_region() click to toggle source
# File lib/pghero/database.rb, line 78
def aws_region
  config["aws_region"] || PgHero.config["aws_region"] || ENV["PGHERO_REGION"] || ENV["AWS_REGION"] || (defined?(Aws) && Aws.config[:region]) || "us-east-1"
end
aws_secret_access_key() click to toggle source
# File lib/pghero/database.rb, line 74
def aws_secret_access_key
  config["aws_secret_access_key"] || PgHero.config["aws_secret_access_key"] || ENV["PGHERO_SECRET_ACCESS_KEY"] || ENV["AWS_SECRET_ACCESS_KEY"]
end
azure_resource_id() click to toggle source

environment variable is only used if no config file

# File lib/pghero/database.rb, line 93
def azure_resource_id
  @azure_resource_id ||= config["azure_resource_id"]
end
cache_hit_rate_threshold() click to toggle source
# File lib/pghero/database.rb, line 41
def cache_hit_rate_threshold
  (config["cache_hit_rate_threshold"] || PgHero.config["cache_hit_rate_threshold"] || PgHero.cache_hit_rate_threshold).to_i
end
capture_query_stats?() click to toggle source
# File lib/pghero/database.rb, line 37
def capture_query_stats?
  config["capture_query_stats"] != false
end
explain_timeout_sec() click to toggle source
# File lib/pghero/database.rb, line 57
def explain_timeout_sec
  (config["explain_timeout_sec"] || PgHero.config["explain_timeout_sec"] || PgHero.explain_timeout_sec).to_f
end
filter_data() click to toggle source

must check keys for booleans

# File lib/pghero/database.rb, line 98
def filter_data
  unless defined?(@filter_data)
    @filter_data =
      if config.key?("filter_data")
        config["filter_data"]
      elsif PgHero.config.key?("filter_data")
        PgHero.config.key?("filter_data")
      else
        PgHero.filter_data
      end

    if @filter_data
      begin
        require "pg_query"
      rescue LoadError
        raise Error, "pg_query required for filter_data"
      end
    end
  end

  @filter_data
end
gcp_database_id() click to toggle source

environment variable is only used if no config file

# File lib/pghero/database.rb, line 88
def gcp_database_id
  @gcp_database_id ||= config["gcp_database_id"]
end
index_bloat_bytes() click to toggle source

defaults to 100 megabytes

# File lib/pghero/database.rb, line 66
def index_bloat_bytes
  (config["index_bloat_bytes"] || PgHero.config["index_bloat_bytes"] || 104857600).to_i
end
long_running_query_sec() click to toggle source
# File lib/pghero/database.rb, line 61
def long_running_query_sec
  (config["long_running_query_sec"] || PgHero.config["long_running_query_sec"] || PgHero.long_running_query_sec).to_i
end
name() click to toggle source
# File lib/pghero/database.rb, line 33
def name
  @name ||= @config["name"] || id.titleize
end
slow_query_calls() click to toggle source
# File lib/pghero/database.rb, line 53
def slow_query_calls
  (config["slow_query_calls"] || PgHero.config["slow_query_calls"] || PgHero.slow_query_calls).to_i
end
slow_query_ms() click to toggle source
# File lib/pghero/database.rb, line 49
def slow_query_ms
  (config["slow_query_ms"] || PgHero.config["slow_query_ms"] || PgHero.slow_query_ms).to_i
end
total_connections_threshold() click to toggle source
# File lib/pghero/database.rb, line 45
def total_connections_threshold
  (config["total_connections_threshold"] || PgHero.config["total_connections_threshold"] || PgHero.total_connections_threshold).to_i
end

Private Instance Methods

build_connection_model() click to toggle source

just return the model do not start a connection

# File lib/pghero/database.rb, line 140
def build_connection_model
  url = config["url"]

  # resolve spec
  if !url && config["spec"]
    config_options = {env_name: PgHero.env, PgHero.spec_name_key => config["spec"], PgHero.include_replicas_key => true}
    resolved = ActiveRecord::Base.configurations.configs_for(**config_options)
    raise Error, "Spec not found: #{config["spec"]}" unless resolved
    url = resolved.configuration_hash
  end

  url = url.dup

  Class.new(PgHero::Connection) do
    def self.name
      "PgHero::Connection::Database#{object_id}"
    end

    case url
    when String
      url = "#{url}#{url.include?("?") ? "&" : "?"}connect_timeout=5" unless url.include?("connect_timeout=")
    when Hash
      url[:connect_timeout] ||= 5
    end
    establish_connection url if url
  end
end
connection_model() click to toggle source

check adapter lazily

# File lib/pghero/database.rb, line 124
def connection_model
  unless @adapter_checked
    # rough check for Postgres adapter
    # keep this message generic so it's useful
    # when empty url set in Docker image pghero.yml
    unless @connection_model.connection_db_config.adapter.to_s.match?(/postg/i)
      raise Error, "Invalid connection URL"
    end
    @adapter_checked = true
  end

  @connection_model
end