跳至内容 跳至搜索

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
K
L
M
N
P
Q
R
S

Attributes

[R] configuration_hash

类公共方法

new(env_name, name, configuration_hash)

初始化一个新的 HashConfig 对象

参数

  • env_name - 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
  validate_configuration!
end

实例公共方法

adapter()

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

checkout_timeout()

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

database()

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

default_schema_cache_path(db_dir = "db")

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

idle_timeout()

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

keepalive()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 125
def keepalive
  keepalive = (configuration_hash[:keepalive] || 600).to_f
  keepalive if keepalive > 0
end

lazy_schema_cache_path()

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

max_age()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 95
def max_age
  v = configuration_hash[:max_age]&.to_i
  if v && v > 0
    v
  else
    Float::INFINITY
  end
end

max_connections()

也别名为: pool
# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 73
def max_connections
  max_connections = configuration_hash.fetch(:max_connections) {
    configuration_hash.fetch(:pool, 5)
  }&.to_i
  max_connections if max_connections && max_connections >= 0
end

max_queue()

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

max_threads()

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 91
def max_threads
  (configuration_hash[:max_threads] || (max_connections || 5).clamp(0, 5)).to_i
end

migrations_paths()

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

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

min_connections()

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

min_threads()

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

pool()

别名为: max_connections

query_cache()

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

replica?()

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

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

schema_cache_path()

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

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

schema_dump(format = schema_format)

确定是否转储 schema/structure 文件以及应使用的文件名。

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

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

# File activerecord/lib/active_record/database_configurations/hash_config.rb, line 172
def schema_dump(format = 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 160
def seeds?
  configuration_hash.fetch(:seeds, primary?)
end