# File lib/pghero.rb, line 187 def primary_database databases.values.first end
module PgHero
Constants
- MUTEX
- VERSION
Attributes
Public Class Methods
Source
# File lib/pghero.rb, line 206 def analyze_all(**options) each_database do |database| next if database.replica? database.analyze_tables(**options) end end
Source
# File lib/pghero.rb, line 213 def autoindex_all(create: false, verbose: true) each_database do |database| puts "Autoindexing #{database.id}..." if verbose database.autoindex(create: create) end end
Source
# File lib/pghero.rb, line 191 def capture_query_stats(verbose: false) each_database do |database| next unless database.capture_query_stats? puts "Capturing query stats for #{database.id}..." if verbose database.capture_query_stats(raise_errors: true) end end
Source
# File lib/pghero.rb, line 199 def capture_space_stats(verbose: false) each_database do |database| puts "Capturing space stats for #{database.id}..." if verbose database.capture_space_stats end end
Source
# File lib/pghero.rb, line 227 def clean_query_stats(before: nil) each_database do |database| database.clean_query_stats(before: before) end end
delete previous stats go database by database to use an index stats for old databases are not cleaned up since we can’t use an index
Source
# File lib/pghero.rb, line 233 def clean_space_stats(before: nil) each_database do |database| database.clean_space_stats(before: before) end end
Source
# File lib/pghero.rb, line 110 def config @config ||= file_config || default_config end
Source
# File lib/pghero.rb, line 240 def connection_config(model) model.connection_db_config.configuration_hash end
private
Source
# File lib/pghero.rb, line 173 def databases unless defined?(@databases) # only use mutex on initialization MUTEX.synchronize do # return if another process initialized while we were waiting return @databases if defined?(@databases) @databases = config["databases"].map { |id, c| [id.to_sym, Database.new(id, c)] }.to_h end end @databases end
ensure we only have one copy of databases so there’s only one connection pool per database
Source
# File lib/pghero.rb, line 143 def default_config databases = {} unless ENV["PGHERO_DATABASE_URL"] ActiveRecord::Base.configurations.configs_for(env_name: env, include_replicas_key => true).each do |db| databases[db.send(spec_name_key)] = {"spec" => db.send(spec_name_key)} end end if databases.empty? databases["primary"] = { "url" => ENV["PGHERO_DATABASE_URL"] } end if databases.size == 1 databases.values.first.merge!( "aws_db_instance_identifier" => ENV["PGHERO_DB_INSTANCE_IDENTIFIER"], "gcp_database_id" => ENV["PGHERO_GCP_DATABASE_ID"], "azure_resource_id" => ENV["PGHERO_AZURE_RESOURCE_ID"] ) end { "databases" => databases } end
private
Source
# File lib/pghero.rb, line 97 def explain_enabled? explain_mode.nil? || explain_mode == true || explain_mode == "analyze" end
private
Source
# File lib/pghero.rb, line 102 def explain_mode @config["explain"] end
private
Source
# File lib/pghero.rb, line 115 def file_config unless defined?(@file_config) require "erb" require "yaml" path = config_path config_file_exists = File.exist?(path) config = YAML.safe_load(ERB.new(File.read(path)).result, aliases: true) if config_file_exists config ||= {} @file_config = if config[env] config[env] elsif config["databases"] # preferred format config elsif config_file_exists raise "Invalid config file" else nil end end @file_config end
private
Source
# File lib/pghero.rb, line 250 def include_replicas_key :include_hidden end
private
Source
# File lib/pghero.rb, line 87 def password @password ||= (file_config || {})["password"] || ENV["PGHERO_PASSWORD"] end
use method instead of attr_accessor to ensure this works if variable set after PgHero
is loaded
Source
# File lib/pghero.rb, line 220 def pretty_size(value) ActiveSupport::NumberHelper.number_to_human_size(value, precision: 3) end
Source
Source
# File lib/pghero.rb, line 92 def stats_database_url @stats_database_url ||= (file_config || {})["stats_database_url"] || ENV["PGHERO_STATS_DATABASE_URL"] end
config pattern for github.com/ankane/pghero/issues/424
Source
# File lib/pghero.rb, line 71 def time_zone=(time_zone) @time_zone = time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone.to_s] end
Source
# File lib/pghero.rb, line 81 def username @username ||= (file_config || {})["username"] || ENV["PGHERO_USERNAME"] end
use method instead of attr_accessor to ensure this works if variable set after PgHero
is loaded
Source
# File lib/pghero.rb, line 106 def visualize_url @visualize_url ||= config["visualize_url"] || ENV["PGHERO_VISUALIZE_URL"] || "https://tatiyants.com/pev/#/plans/new" end
Private Class Methods
Source
# File lib/pghero.rb, line 256 def each_database first_error = nil databases.each do |_, database| begin yield database rescue => e puts "#{e.class.name}: #{e.message}" puts first_error ||= e end end raise first_error if first_error true end