Module: ActiveRecord::ConnectionHandling

Included in:
Base
Defined in:
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb,
activerecord/lib/active_record/connection_handling.rb,
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb,
activerecord/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" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connection_specification_nameObject

Return the specification id from this class otherwise look it up in the parent.


98
99
100
101
102
103
# File 'activerecord/lib/active_record/connection_handling.rb', line 98

def connection_specification_name
  if !defined?(@connection_specification_name) || @connection_specification_name.nil?
    return self == Base ? "primary" : superclass.connection_specification_name
  end
  @connection_specification_name
end

Instance Method Details

#clear_cache!Object

:nodoc:


148
149
150
# File 'activerecord/lib/active_record/connection_handling.rb', line 148

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

#connected?Boolean

Returns true if Active Record is connected.

Returns:

  • (Boolean)

132
133
134
# File 'activerecord/lib/active_record/connection_handling.rb', line 132

def connected?
  connection_handler.connected?(connection_specification_name)
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.


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

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.


119
120
121
# File 'activerecord/lib/active_record/connection_handling.rb', line 119

def connection_config
  connection_pool.spec.config
end

#connection_idObject


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

def connection_id
  ActiveRecord::RuntimeRegistry.connection_id ||= Thread.current.object_id
end

#connection_id=(connection_id) ⇒ Object


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

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

#connection_poolObject


123
124
125
# File 'activerecord/lib/active_record/connection_handling.rb', line 123

def connection_pool
  connection_handler.retrieve_connection_pool(connection_specification_name) 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:  "mysql2",
  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:[email protected]/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.

Raises:

  • (RuntimeError)

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

def establish_connection(spec = nil)
  raise RuntimeError, "Anonymous class is not allowed." unless name

  spec     ||= DEFAULT_ENV.call.to_sym
  resolver =   ConnectionAdapters::ConnectionSpecification::Resolver.new configurations
  # TODO: uses name on establish_connection, for backwards compatibility
  spec     =   resolver.spec(spec, self == Base ? "primary" : name)

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

  remove_connection(spec.name)
  self.connection_specification_name = spec.name
  connection_handler.establish_connection spec
end

#mysql2_connection(config) ⇒ Object

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


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 11

def mysql2_connection(config)
  config = config.symbolize_keys

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

  if Mysql2::Client.const_defined? :FOUND_ROWS
    if config[:flags].kind_of? Array
      config[:flags].push "FOUND_ROWS".freeze
    else
      config[:flags] |= Mysql2::Client::FOUND_ROWS
    end
  end

  client = Mysql2::Client.new(config)
  ConnectionAdapters::Mysql2Adapter.new(client, logger, nil, config)
rescue Mysql2::Error => error
  if error.message.include?("Unknown database")
    raise ActiveRecord::NoDatabaseError
  else
    raise
  end
end

#postgresql_connection(config) ⇒ Object

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


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb', line 22

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.
  valid_conn_param_keys = PGconn.conndefaults_hash.keys + [:requiressl]
  conn_params.slice!(*valid_conn_param_keys)

  # 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(name = nil) ⇒ Object


136
137
138
139
140
141
142
143
144
145
146
# File 'activerecord/lib/active_record/connection_handling.rb', line 136

def remove_connection(name = nil)
  name ||= @connection_specification_name if defined?(@connection_specification_name)
  # if removing a connection that have a pool, we reset the
  # connection_specification_name so it will use the parent
  # pool.
  if connection_handler.retrieve_connection_pool(name)
    self.connection_specification_name = nil
  end

  connection_handler.remove_connection(name)
end

#retrieve_connectionObject


127
128
129
# File 'activerecord/lib/active_record/connection_handling.rb', line 127

def retrieve_connection
  connection_handler.retrieve_connection(connection_specification_name)
end

#sqlite3_connection(config) ⇒ Object


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
41
# File 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb', line 12

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
  else
    raise
  end
end