Sha256: b63885a78d7c4428d27a8be0e02fc0d887107fbf11bd0ce62fc6411ca692fe8c

Contents?: true

Size: 1.95 KB

Versions: 12

Compression:

Stored size: 1.95 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(path: nil)
    if path
      database_url_from_database_yml(path)
    elsif ENV["DATABASE_URL"]
      ENV["DATABASE_URL"]
    else
      database_url_from_database_yml("config/database.yml")
    end
  end

  private

  def database_url_from_database_yml(path)
    abc = load_activerecord_base_configuration(path: path, env: ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")

    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 load_activerecord_base_configuration(path:, env:)
    require "yaml"
    database_config = YAML.load_file(path)
    env ||= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
simple-sql-0.5.37 lib/simple/sql/config.rb
simple-sql-0.5.36 lib/simple/sql/config.rb
simple-sql-0.5.35 lib/simple/sql/config.rb
simple-sql-0.5.34 lib/simple/sql/config.rb
simple-sql-0.5.33 lib/simple/sql/config.rb
simple-sql-0.5.32 lib/simple/sql/config.rb
simple-sql-0.5.31 lib/simple/sql/config.rb
simple-sql-0.5.30 lib/simple/sql/config.rb
simple-sql-0.5.29 lib/simple/sql/config.rb
simple-sql-0.5.28 lib/simple/sql/config.rb
simple-sql-0.5.27 lib/simple/sql/config.rb
simple-sql-0.5.26 lib/simple/sql/config.rb