Module: ActiveRecord::ConnectionHandling

Included in:
Base
Defined in:
lib/active_record/connection_adapters/mysql_adapter.rb,
lib/active_record/connection_handling.rb,
lib/active_record/connection_adapters/mysql2_adapter.rb,
lib/active_record/connection_adapters/sqlite3_adapter.rb,
lib/active_record/connection_adapters/postgresql_adapter.rb

Overview

:nodoc:

Defined Under Namespace

Classes: MergeAndResolveDefaultUrlConfig

Constant Summary collapse

RAILS_ENV =
-> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"] || ENV["RACK_ENV"] }
DEFAULT_ENV =
-> { RAILS_ENV.call || "default_env" }
VALID_CONN_PARAMS =
[:host, :hostaddr, :port, :dbname, :user, :password, :connect_timeout,
:client_encoding, :options, :application_name, :fallback_application_name,
:keepalives, :keepalives_idle, :keepalives_interval, :keepalives_count,
:tty, :sslmode, :requiressl, :sslcompression, :sslcert, :sslkey,
:sslrootcert, :sslcrl, :requirepeer, :krbsrvname, :gsslib, :service]

Instance Method Summary collapse

Instance Method Details

#clear_cache!Object

:nodoc:



125
126
127
# File 'lib/active_record/connection_handling.rb', line 125

def clear_cache! # :nodoc:
  connection.schema_cache.clear!
end

#connected?Boolean

Returns true if Active Record is connected.

Returns:

  • (Boolean)


117
118
119
# File 'lib/active_record/connection_handling.rb', line 117

def connected?
  connection_handler.connected?(self)
end

#connectionObject

Returns the connection currently associated with the class. This can also be used to “borrow” the connection to do database work unrelated to any of the specific Active Records.



86
87
88
# File 'lib/active_record/connection_handling.rb', line 86

def connection
  retrieve_connection
end

#connection_configObject

Returns the configuration of the associated connection as a hash:

ActiveRecord::Base.connection_config
# => {pool: 5, timeout: 5000, database: "db/development.sqlite3", adapter: "sqlite3"}

Please use only for reading.



104
105
106
# File 'lib/active_record/connection_handling.rb', line 104

def connection_config
  connection_pool.spec.config
end

#connection_idObject



90
91
92
# File 'lib/active_record/connection_handling.rb', line 90

def connection_id
  ActiveRecord::RuntimeRegistry.connection_id
end

#connection_id=(connection_id) ⇒ Object



94
95
96
# File 'lib/active_record/connection_handling.rb', line 94

def connection_id=(connection_id)
  ActiveRecord::RuntimeRegistry.connection_id = connection_id
end

#connection_poolObject



108
109
110
# File 'lib/active_record/connection_handling.rb', line 108

def connection_pool
  connection_handler.retrieve_connection_pool(self) or raise ConnectionNotEstablished
end

#establish_connection(spec = nil) ⇒ Object

Establishes the connection to the database. Accepts a hash as input where the :adapter key must be specified with the name of a database adapter (in lower-case) example for regular databases (MySQL, Postgresql, etc):

ActiveRecord::Base.establish_connection(
  adapter:  "mysql",
  host:     "localhost",
  username: "myuser",
  password: "mypass",
  database: "somedatabase"
)

Example for SQLite database:

ActiveRecord::Base.establish_connection(
  adapter:  "sqlite3",
  database: "path/to/dbfile"
)

Also accepts keys as strings (for parsing from YAML for example):

ActiveRecord::Base.establish_connection(
  "adapter"  => "sqlite3",
  "database" => "path/to/dbfile"
)

Or a URL:

ActiveRecord::Base.establish_connection(
  "postgres://myuser:mypass@localhost/somedatabase"
)

In case ActiveRecord::Base.configurations is set (Rails automatically loads the contents of config/database.yml into it), a symbol can also be given as argument, representing a key in the configuration hash:

ActiveRecord::Base.establish_connection(:production)

The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError may be returned on an error.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_record/connection_handling.rb', line 47

def establish_connection(spec = nil)
  spec     ||= DEFAULT_ENV.call.to_sym
  resolver =   ConnectionAdapters::ConnectionSpecification::Resolver.new configurations
  spec     =   resolver.spec(spec)

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

  remove_connection
  connection_handler.establish_connection self, spec
end

#mysql2_connection(config) ⇒ Object

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_record/connection_adapters/mysql2_adapter.rb', line 9

def mysql2_connection(config)
  config = config.symbolize_keys

  config[:username] = 'root' if config[:username].nil?

  if Mysql2::Client.const_defined? :FOUND_ROWS
    config[:flags] = Mysql2::Client::FOUND_ROWS
  end

  client = Mysql2::Client.new(config)
  options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
  ConnectionAdapters::Mysql2Adapter.new(client, logger, options, config)
rescue Mysql2::Error => error
  if error.message.include?("Unknown database")
    raise ActiveRecord::NoDatabaseError.new(error.message, error)
  else
    raise
  end
end

#mysql_connection(config) ⇒ Object

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_record/connection_adapters/mysql_adapter.rb', line 21

def mysql_connection(config)
  config = config.symbolize_keys
  host     = config[:host]
  port     = config[:port]
  socket   = config[:socket]
  username = config[:username] ? config[:username].to_s : 'root'
  password = config[:password].to_s
  database = config[:database]

  mysql = Mysql.init
  mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslca] || config[:sslkey]

  default_flags = Mysql.const_defined?(:CLIENT_MULTI_RESULTS) ? Mysql::CLIENT_MULTI_RESULTS : 0
  default_flags |= Mysql::CLIENT_FOUND_ROWS if Mysql.const_defined?(:CLIENT_FOUND_ROWS)
  options = [host, username, password, database, port, socket, default_flags]
  ConnectionAdapters::MysqlAdapter.new(mysql, logger, options, config)
rescue Mysql::Error => error
  if error.message.include?("Unknown database")
    raise ActiveRecord::NoDatabaseError.new(error.message, error)
  else
    raise
  end
end

#postgresql_connection(config) ⇒ Object

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 30

def postgresql_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 PGconn.connect.
  conn_params.keep_if { |k, _| VALID_CONN_PARAMS.include?(k) }

  # The postgres drivers don't allow the creation of an unconnected PGconn object,
  # so just pass a nil connection object for the time being.
  ConnectionAdapters::PostgreSQLAdapter.new(nil, logger, conn_params, config)
end

#remove_connection(klass = self) ⇒ Object



121
122
123
# File 'lib/active_record/connection_handling.rb', line 121

def remove_connection(klass = self)
  connection_handler.remove_connection(klass)
end

#retrieve_connectionObject



112
113
114
# File 'lib/active_record/connection_handling.rb', line 112

def retrieve_connection
  connection_handler.retrieve_connection(self)
end

#sqlite3_connection(config) ⇒ Object

sqlite3 adapter reuses sqlite_connection.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_record/connection_adapters/sqlite3_adapter.rb', line 11

def sqlite3_connection(config)
  # Require database.
  unless config[:database]
    raise ArgumentError, "No database file specified. Missing argument: database"
  end

  # Allow database path relative to Rails.root, but only if the database
  # path is not the special path that tells sqlite to build a database only
  # in memory.
  if ':memory:' != config[:database]
    config[:database] = File.expand_path(config[:database], Rails.root) if defined?(Rails.root)
    dirname = File.dirname(config[:database])
    Dir.mkdir(dirname) unless File.directory?(dirname)
  end

  db = SQLite3::Database.new(
    config[:database].to_s,
    :results_as_hash => true
  )

  db.busy_timeout(ConnectionAdapters::SQLite3Adapter.type_cast_config_to_integer(config[:timeout])) if config[:timeout]

  ConnectionAdapters::SQLite3Adapter.new(db, logger, nil, config)
rescue Errno::ENOENT => error
  if error.message.include?("No such file or directory")
    raise ActiveRecord::NoDatabaseError.new(error.message, error)
  else
    raise
  end
end