跳至內容 跳至搜尋

Active Record 資料庫 雜湊 設定

對於每個從雜湊建立的資料庫設定項目,都會建立一個 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」。

  • 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]` 設定為 `false` 或 `nil`,則不會傾印結構。

如果設定了設定選項,將使用該選項。否則 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