module DbCharmerEnvUrls

Constants

VERSION

Public Class Methods

database_configurations() click to toggle source

Extracts database configs from the ENV where ENV key ends it _DATABASE_URL

@example Matching ENV variable names

SLAVE_DATABASE_URL, CRIMSON_DATABASE_URL

@example Usage

ENV["SLAVE_DATABASE_URL"] = "postgres://localhost:5432/name"
DbCharmerEnvUrls.database_configurations
{
  "slave" => {
    "adapter" => "postgres",
    "host" => "localhost",
    "port" => 5432,
    "database" => "name"
  }
}

@return [Hash] Hash of database configurations whose keys are extracted from the ENV and whose values are hashes of database configs

# File lib/db_charmer_env_urls.rb, line 23
def self.database_configurations
  base = ActiveRecord::Base
  ENV.inject({}) do |acc, (key, url)|
    if matches = key.match(/\A(\w+)_DATABASE_URL\z/)
      config = base::ConnectionSpecification::Resolver.new(url, base.configurations).spec.config
      acc[matches[1].downcase] = config.stringify_keys
    end
    acc
  end
end