跳至内容 跳至搜索

Active Record 数据库 Hash 配置

每个从哈希创建的数据库配置条目都会创建一个 HashConfig 对象。

哈希配置

{ "development" => { "database" => "db_name" } }

变成

#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10
  @env_name="development", @name="primary", @config={database: "db_name"}>

有关更多信息,请参见 ActiveRecord::DatabaseConfigurations

方法
A
C
D
H
I
L
M
N
P
Q
R
S

属性

[R] configuration_hash

类公共方法

new(env_name, name, configuration_hash)

初始化一个新的 HashConfig 对象

选项

  • :env_name - Rails 环境,例如“development”。

  • :name - 数据库配置名称。在标准的两层数据库配置中,这将默认为“primary”。在多数据库三层数据库配置中,这对应于第二层中使用的名称,例如“primary_readonly”。

  • :config - 配置哈希。这是包含数据库适配器、名称和其他重要数据库连接信息的哈希。

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 36
def initialize(env_name, name, configuration_hash)
  super(env_name, name)
  @configuration_hash = configuration_hash.symbolize_keys.freeze
end

实例公共方法

adapter()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 106
def adapter
  configuration_hash[:adapter]
end

checkout_timeout()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 91
def checkout_timeout
  (configuration_hash[:checkout_timeout] || 5).to_f
end

database()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 63
def database
  configuration_hash[:database]
end

default_schema_cache_path()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 117
def default_schema_cache_path
  "db/schema_cache.yml"
end

host()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 55
def host
  configuration_hash[:host]
end

idle_timeout()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 101
def idle_timeout
  timeout = configuration_hash.fetch(:idle_timeout, 300).to_f
  timeout if timeout > 0
end

lazy_schema_cache_path()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 121
def lazy_schema_cache_path
  schema_cache_path || default_schema_cache_path
end

max_queue()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 87
def max_queue
  max_threads * 4
end

max_threads()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 79
def max_threads
  (configuration_hash[:max_threads] || pool).to_i
end

migrations_paths()

数据库配置的迁移路径。如果配置中存在 `migrations_paths` 键,`migrations_paths` 将返回其值。

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 51
def migrations_paths
  configuration_hash[:migrations_paths]
end

min_threads()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 75
def min_threads
  (configuration_hash[:min_threads] || 0).to_i
end

pool()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 71
def pool
  (configuration_hash[:pool] || 5).to_i
end

query_cache()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 83
def query_cache
  configuration_hash[:query_cache]
end

reaping_frequency()

reaping_frequency 主要出于历史原因可配置,但如果有人想要非常低的 `idle_timeout`,它也可能有用。

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 97
def reaping_frequency
  configuration_hash.fetch(:reaping_frequency, 60)&.to_f
end

replica?()

确定数据库配置是否用于副本/只读连接。如果配置中存在 `replica` 键,`replica?` 将返回 `true`。

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 44
def replica?
  configuration_hash[:replica]
end

schema_cache_path()

数据库的模式缓存转储文件路径。如果省略,文件名将从 ENV 中读取或推导出默认值。

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 113
def schema_cache_path
  configuration_hash[:schema_cache_path]
end

schema_dump(format = ActiveRecord.schema_format)

确定是否要转储 schema/结构文件以及要使用的文件名。

如果 configuration_hash[:schema_dump] 设置为 falsenil,则不会转储 schema。

如果设置了配置选项,则将使用该选项。否则,Rails 将根据数据库配置名称生成文件名。

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 137
def schema_dump(format = ActiveRecord.schema_format)
  if configuration_hash.key?(:schema_dump)
    if config = configuration_hash[:schema_dump]
      config
    end
  elsif primary?
    schema_file_type(format)
  else
    "#{name}_#{schema_file_type(format)}"
  end
end