Sha256: 84c523461a375de3857c6080ee89c41eb769f5b11ee7577770e31bc6c1f5f9e8

Contents?: true

Size: 1.71 KB

Versions: 7

Compression:

Stored size: 1.71 KB

Contents

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity

# module to determine database configuration
module Simple::SQL::Config
  extend self

  # parse a DATABASE_URL, return PG::Connection settings.
  def parse_url(url)
    expect! url => /^postgres(ql)?s?:\/\//

    require "uri"
    uri = URI.parse(url)
    raise ArgumentError, "Invalid URL #{url}" unless uri.hostname && uri.path

    config = {
      dbname: uri.path.sub(%r{^/}, ""),
      host:   uri.hostname
    }
    config[:port] = uri.port if uri.port
    config[:user] = uri.user if uri.user
    config[:password] = uri.password if uri.password
    config[:sslmode] = uri.scheme == "postgress" || uri.scheme == "postgresqls" ? "require" : "prefer"
    config
  end

  # determines the database_url from either the DATABASE_URL environment setting
  # or a config/database.yml file.
  def determine_url
    ENV["DATABASE_URL"] || database_url_from_database_yml
  end

  private

  def database_url_from_database_yml
    abc = read_database_yml
    username, password, host, port, database = abc.values_at "username", "password", "host", "port", "database"

    URI::Generic.build(
      scheme: "postgres",
      userinfo: [username, (":" if password), password].join,
      host: host || "localhost",
      port: port,
      path: "/#{database}"
    ).to_s
  end

  def read_database_yml
    require "yaml"
    database_config = YAML.load_file "config/database.yml"
    env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"

    database_config[env] ||
      database_config["defaults"] ||
      raise("Invalid or missing database configuration in config/database.yml for #{env.inspect} environment")
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
simple-sql-0.5.21 lib/simple/sql/config.rb
simple-sql-0.5.20 lib/simple/sql/config.rb
simple-sql-0.5.19 lib/simple/sql/config.rb
simple-sql-0.5.18 lib/simple/sql/config.rb
simple-sql-0.5.17 lib/simple/sql/config.rb
simple-sql-0.5.16 lib/simple/sql/config.rb
simple-sql-0.5.15 lib/simple/sql/config.rb