Class: Datadog::Tracing::Contrib::ActiveRecord::Vendor::ConnectionAdapters::ConnectionSpecification::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/contrib/active_record/vendor/connection_specification.rb

Overview

Builds a ConnectionSpecification from user input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configurations) ⇒ Resolver

Accepts a hash two layers deep, keys on the first layer represent environments such as “production”. Keys must be strings.



135
136
137
# File 'lib/datadog/tracing/contrib/active_record/vendor/connection_specification.rb', line 135

def initialize(configurations)
  @configurations = configurations
end

Instance Attribute Details

#configurationsObject (readonly)

:nodoc:



131
132
133
# File 'lib/datadog/tracing/contrib/active_record/vendor/connection_specification.rb', line 131

def configurations
  @configurations
end

Instance Method Details

#resolve(config) ⇒ Object

Returns a hash with database connection information.

Examples

Full hash Configuration.

configurations = { "production" => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3" } }
Resolver.new(configurations).resolve(:production)
# => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3"}

Initialized with URL configuration strings.

configurations = { "production" => "postgresql://localhost/foo" }
Resolver.new(configurations).resolve(:production)
# => { "host" => "localhost", "database" => "foo", "adapter" => "postgresql" }


155
156
157
158
159
160
161
162
163
# File 'lib/datadog/tracing/contrib/active_record/vendor/connection_specification.rb', line 155

def resolve(config)
  if config
    resolve_connection config
  elsif env = ConnectionHandling::RAILS_ENV.call
    resolve_symbol_connection env.to_sym
  else
    raise AdapterNotSpecified
  end
end

#resolve_allObject

Expands each key in @configurations hash into fully resolved hash



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/datadog/tracing/contrib/active_record/vendor/connection_specification.rb', line 166

def resolve_all
  config = configurations.dup

  if env = ConnectionHandling::DEFAULT_ENV.call
    env_config = config[env] if config[env].is_a?(Hash) && !(config[env].key?("adapter") || config[env].key?("url"))
  end

  config.reject! { |k, v| v.is_a?(Hash) && !(v.key?("adapter") || v.key?("url")) }
  config.merge! env_config if env_config

  config.each do |key, value|
    config[key] = resolve(value) if value
  end

  config
end

#spec(config) ⇒ Object

Returns an instance of ConnectionSpecification for a given adapter. Accepts a hash one layer deep that contains all connection information.

Example

config = { "production" => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3" } }
spec = Resolver.new(config).spec(:production)
spec.adapter_method
# => "sqlite3_connection"
spec.config
# => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3" }

Raises:

  • (AdapterNotSpecified)


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/datadog/tracing/contrib/active_record/vendor/connection_specification.rb', line 195

def spec(config)
  spec = resolve(config).symbolize_keys

  raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)

  # Require the adapter itself and give useful feedback about
  #   1. Missing adapter gems and
  #   2. Adapter gems' missing dependencies.
  path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter"
  begin
    require path_to_adapter
  rescue LoadError => e
    # We couldn't require the adapter itself. Raise an exception that
    # points out config typos and missing gems.
    if e.path == path_to_adapter
      # We can assume that a non-builtin adapter was specified, so it's
      # either misspelled or missing from Gemfile.
      raise e.class, "Could not load the '#{spec[:adapter]}' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile.", e.backtrace

    # Bubbled up from the adapter require. Prefix the exception message
    # with some guidance about how to address it and reraise.
    else
      raise e.class, "Error loading the '#{spec[:adapter]}' Active Record adapter. Missing a gem it depends on? #{e.class.name} #{e.message}", e.backtrace
    end
  end

  adapter_method = "#{spec[:adapter]}_connection"

  unless ::ActiveRecord::Base.respond_to?(adapter_method)
    raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
  end

  ConnectionSpecification.new(spec.delete(:name) || "primary", spec, adapter_method)
end