Skip to content

Commit

Permalink
Improved config pattern - fixes #424
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Sep 10, 2022
1 parent 60973c9 commit 8ad27c7
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions lib/pghero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ def password
@password ||= config["password"] || ENV["PGHERO_PASSWORD"]
end

# config pattern for https://github.com/ankane/pghero/issues/424
def stats_database_url
@stats_database_url ||= config["stats_database_url"] || ENV["PGHERO_STATS_DATABASE_URL"]
@stats_database_url ||= (file_config || {})["stats_database_url"] || ENV["PGHERO_STATS_DATABASE_URL"]
end

def config
@config ||= begin
@config ||= file_config || default_config
end

# private
def file_config
unless defined?(@file_config)
require "erb"
require "yaml"

Expand All @@ -102,40 +108,48 @@ def config
config = YAML.load(ERB.new(File.read(path)).result) if config_file_exists
config ||= {}

if config[env]
config[env]
elsif config["databases"] # preferred format
config
elsif config_file_exists
raise "Invalid config file"
else
databases = {}

if !ENV["PGHERO_DATABASE_URL"] && spec_supported?
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
@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

if databases.empty?
databases["primary"] = {
"url" => ENV["PGHERO_DATABASE_URL"] || connection_config(ActiveRecord::Base)
}
end
@file_config
end

if databases.size == 1
databases.values.first.merge!(
"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
# private
def default_config
databases = {}

{
"databases" => databases
}
if !ENV["PGHERO_DATABASE_URL"] && spec_supported?
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"] || connection_config(ActiveRecord::Base)
}
end

if databases.size == 1
databases.values.first.merge!(
"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

# ensure we only have one copy of databases
Expand Down

0 comments on commit 8ad27c7

Please # to comment.