Module: ActiveRecord::ConnectionHandling

Defined in:
lib/active_record/connection_adapters/materialize_adapter.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#materialize_connection(config) ⇒ Object

Establishes a connection to the database that’s used by all Active Record objects



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/active_record/connection_adapters/materialize_adapter.rb', line 42

def materialize_connection(config)
  conn_params = config.symbolize_keys

  conn_params.delete_if { |_, v| v.nil? }

  # Map ActiveRecords param names to PGs.
  conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
  conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]

  # Forward only valid config params to PG::Connection.connect.
  valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
  conn_params.slice!(*valid_conn_param_keys)

  conn = PG.connect(conn_params)
  ConnectionAdapters::MaterializeAdapter.new(conn, logger, conn_params, config)
rescue ::PG::Error => error
  if error.message.include?(conn_params[:dbname])
    raise ActiveRecord::NoDatabaseError
  else
    raise
  end
end