123456789_123456789_123456789_123456789_123456789_

Module: ActiveRecord::ConnectionAdapters

Relationships & Source Files
Namespace Children
Modules:
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Defined in: activerecord/lib/active_record/connection_adapters.rb,
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb,
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb,
activerecord/lib/active_record/connection_adapters/column.rb,
activerecord/lib/active_record/connection_adapters/deduplicable.rb,
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb,
activerecord/lib/active_record/connection_adapters/pool_config.rb,
activerecord/lib/active_record/connection_adapters/pool_manager.rb,
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb,
activerecord/lib/active_record/connection_adapters/schema_cache.rb,
activerecord/lib/active_record/connection_adapters/sql_type_metadata.rb,
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb,
activerecord/lib/active_record/connection_adapters/statement_pool.rb,
activerecord/lib/active_record/connection_adapters/trilogy_adapter.rb,
activerecord/lib/active_record/connection_adapters/abstract/connection_handler.rb,
activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb,
activerecord/lib/active_record/connection_adapters/abstract/database_limits.rb,
activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb,
activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb,
activerecord/lib/active_record/connection_adapters/abstract/quoting.rb,
activerecord/lib/active_record/connection_adapters/abstract/savepoints.rb,
activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb,
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb,
activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb,
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb,
activerecord/lib/active_record/connection_adapters/abstract/transaction.rb,
activerecord/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb,
activerecord/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb,
activerecord/lib/active_record/connection_adapters/mysql/column.rb,
activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb,
activerecord/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb,
activerecord/lib/active_record/connection_adapters/mysql/quoting.rb,
activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb,
activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb,
activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb,
activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb,
activerecord/lib/active_record/connection_adapters/mysql/type_metadata.rb,
activerecord/lib/active_record/connection_adapters/mysql2/database_statements.rb,
activerecord/lib/active_record/connection_adapters/postgresql/column.rb,
activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb,
activerecord/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid.rb,
activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb,
activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb,
activerecord/lib/active_record/connection_adapters/postgresql/schema_creation.rb,
activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb,
activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb,
activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb,
activerecord/lib/active_record/connection_adapters/postgresql/type_metadata.rb,
activerecord/lib/active_record/connection_adapters/postgresql/utils.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/bytea.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/date.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/date_time.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/decimal.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/enum.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/inet.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/interval.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/oid.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/uuid.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/vector.rb,
activerecord/lib/active_record/connection_adapters/postgresql/oid/xml.rb,
activerecord/lib/active_record/connection_adapters/sqlite3/column.rb,
activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb,
activerecord/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb,
activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb,
activerecord/lib/active_record/connection_adapters/sqlite3/schema_creation.rb,
activerecord/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb,
activerecord/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb,
activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb,
activerecord/lib/active_record/connection_adapters/trilogy/database_statements.rb

Constant Summary

Class Method Summary

Class Method Details

.register(name, class_name, path = class_name.underscore)

Registers a custom database adapter.

Can also be used to define aliases.

Example

ActiveRecord::ConnectionAdapters.register("megadb", "MegaDB::ActiveRecordAdapter", "mega_db/active_record_adapter")

ActiveRecord::ConnectionAdapters.register("mysql", "ActiveRecord::ConnectionAdapters::TrilogyAdapter", "active_record/connection_adapters/trilogy_adapter")
[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters.rb', line 22

def register(name, class_name, path = class_name.underscore)
  @adapters[name.to_s] = [class_name, path]
end

.resolve(adapter_name)

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters.rb', line 26

def resolve(adapter_name) # :nodoc:
  # Require the adapter itself and give useful feedback about
  #   1. Missing adapter gems.
  #   2. Incorrectly registered adapters.
  #   3. Adapter gems' missing dependencies.
  class_name, path_to_adapter = @adapters[adapter_name.to_s]

  unless class_name
    # To provide better error messages for adapters expecting the pre-7.2 adapter registration API, we attempt
    # to load the adapter file from the old location which was required by convention, and then raise an error
    # describing how to upgrade the adapter to the new API.
    legacy_adapter_path = "active_record/connection_adapters/#{adapter_name}_adapter"
    legacy_adapter_connection_method_name = "#{adapter_name}_connection".to_sym

    begin
      require legacy_adapter_path
      # If we reach here it means we found the found a file that may be the legacy adapter and should raise.
      if ActiveRecord::ConnectionHandling.method_defined?(legacy_adapter_connection_method_name)
        # If we find the connection method then we care certain it is a legacy adapter.
        deprecation_message = <<~MSG.squish
          Database configuration specifies '#{adapter_name}' adapter but that adapter has not been registered.
          Rails 7.2 has changed the way Active Record database adapters are loaded. The adapter needs to be
          updated to register itself rather than being loaded by convention.
          Ensure that the adapter in the Gemfile is at the latest version. If it is, then the adapter may need to
          be modified.
          See:
          https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters.html#method-c-register
        MSG

        exception_message = <<~MSG.squish
          Database configuration specifies '#{adapter_name}' adapter but that adapter has not been registered.
          Ensure that the adapter in the Gemfile is at the latest version. If it is, then the adapter may need to
          be modified.
        MSG
      else
        # If we do not find the connection method we are much less certain it is a legacy adapter. Even though the
        # file exists in the location defined by convenntion, it does not necessarily mean that file is supposed
        # to define the adapter the legacy way. So raise an error that explains both possibilities.
        deprecation_message = <<~MSG.squish
          Database configuration specifies nonexistent '#{adapter_name}' adapter.
          Available adapters are: #{@adapters.keys.sort.join(", ")}.
          Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary
          adapter gem to your Gemfile if it's not in the list of available adapters.
          Rails 7.2 has changed the way Active Record database adapters are loaded. Ensure that the adapter in
          the Gemfile is at the latest version. If it is up to date, the adapter may need to be modified.
          See:
          https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters.html#method-c-register
        MSG

        exception_message = <<~MSG.squish
          Database configuration specifies nonexistent '#{adapter_name}' adapter.
          Available adapters are: #{@adapters.keys.sort.join(", ")}.
          Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary
          adapter gem to your Gemfile and that it is at its latest version. If it is up to date, the adapter may
          need to be modified.
        MSG
      end

      ActiveRecord.deprecator.warn(deprecation_message)
      raise AdapterNotFound, exception_message
    rescue LoadError => error
      # The adapter was not found in the legacy location so fall through to the error handling for a missing adapter.
    end

    raise AdapterNotFound, <<~MSG.squish
      Database configuration specifies nonexistent '#{adapter_name}' adapter.
      Available adapters are: #{@adapters.keys.sort.join(", ")}.
      Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary
      adapter gem to your Gemfile if it's not in the list of available adapters.
    MSG
  end

  unless Object.const_defined?(class_name)
    begin
      require path_to_adapter
    rescue LoadError => error
      # We couldn't require the adapter itself.
      if error.path == path_to_adapter
        # We can assume here that a non-builtin adapter was specified and the path
        # registered by the adapter gem is incorrect.
        raise LoadError, "Error loading the '#{adapter_name}' Active Record adapter. Ensure that the path registered by the adapter gem is correct. #{error.message}", error.backtrace
      else
        # Bubbled up from the adapter require. Prefix the exception message
        # with some guidance about how to address it and reraise.
        raise LoadError, "Error loading the '#{adapter_name}' Active Record adapter. Missing a gem it depends on? #{error.message}", error.backtrace
      end
    end
  end

  begin
    Object.const_get(class_name)
  rescue NameError => error
    raise AdapterNotFound, "Could not load the #{class_name} Active Record adapter (#{error.message})."
  end
end