123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::DatabaseConfigurations::ConnectionUrlResolver

Do not use. This class is for internal use only.

Overview

Expands a connection string into a hash.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(url) ⇒ ConnectionUrlResolver

Example

url = "postgresql://foo:bar@localhost:9000/foo_test?pool=5&timeout=3000"
ConnectionUrlResolver.new(url).to_hash
# => {
  adapter:  "postgresql",
  host:     "localhost",
  port:     9000,
  database: "foo_test",
  username: "foo",
  password: "bar",
  pool:     "5",
  timeout:  "3000"
}
[ GitHub ]

  
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 25

def initialize(url)
  raise "Database URL cannot be empty" if url.blank?
  @uri     = uri_parser.parse(url)
  @adapter = @uri.scheme && @uri.scheme.tr("-", "_")
  @adapter = "postgresql" if @adapter == "postgres"

  if @uri.opaque
    @uri.opaque, @query = @uri.opaque.split("?", 2)
  else
    @query = @uri.query
  end
end

Instance Attribute Details

#uri (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 46

attr_reader :uri

Instance Method Details

#database_from_path (private)

Returns name of the database.

[ GitHub ]

  
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 84

def database_from_path
  if @adapter == "sqlite3"
    # 'sqlite3:/foo' is absolute, because that makes sense. The
    # corresponding relative version, 'sqlite3:foo', is handled
    # elsewhere, as an "opaque".

    uri.path
  else
    # Only SQLite uses a filename as the "database" name; for
    # anything else, a leading slash would be silly.

    uri.path.delete_prefix("/")
  end
end

#query_hash (private)

Converts the query parameters of the URI into a hash.

"localhost?pool=5&reaping_frequency=2"
# => { pool: "5", reaping_frequency: "2" }

returns empty hash if no query present.

"localhost"
# => {}
[ GitHub ]

  
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 61

def query_hash
  Hash[(@query || "").split("&").map { |pair| pair.split("=", 2) }].symbolize_keys
end

#raw_config (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 65

def raw_config
  if uri.opaque
    query_hash.merge(
      adapter: @adapter,
      database: uri.opaque
    )
  else
    query_hash.reverse_merge(
      adapter: @adapter,
      username: uri.user,
      password: uri.password,
      port: uri.port,
      database: database_from_path,
      host: uri.hostname
    )
  end
end

#to_hash

Converts the given URL to a full connection hash.

[ GitHub ]

  
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 39

def to_hash
  config = raw_config.compact_blank
  config.map { |key, value| config[key] = uri_parser.unescape(value) if value.is_a? String }
  config
end

#uri_parser (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/database_configurations/connection_url_resolver.rb', line 48

def uri_parser
  @uri_parser ||= URI::Parser.new
end