跳至内容 跳至搜索

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 - The Rails 环境,例如“development”。

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

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

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 38
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 107
def adapter
  configuration_hash[:adapter]&.to_s
end

checkout_timeout()

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

database()

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

default_schema_cache_path(db_dir = "db")

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 117
def default_schema_cache_path(db_dir = "db")
  if primary?
    File.join(db_dir, "schema_cache.yml")
  else
    File.join(db_dir, "#{name}_schema_cache.yml")
  end
end

host()

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

idle_timeout()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 102
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 125
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 88
def max_queue
  max_threads * 4
end

max_threads()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 80
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 52
def migrations_paths
  configuration_hash[:migrations_paths]
end

min_threads()

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

pool()

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

query_cache()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 84
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 98
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 46
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)

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

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

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

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 149
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

seeds?()

确定 db:prepare 任务是否应该从 db/seeds.rb 种子数据库。

如果配置中存在 seeds 键,seeds? 将返回其值。否则,它将对主数据库返回 true,对所有其他配置返回 false

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 137
def seeds?
  configuration_hash.fetch(:seeds, primary?)
end