aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2018-03-16 13:20:04 -0400
committereileencodes <eileencodes@gmail.com>2018-03-21 13:53:24 -0400
commit1756094b22bf81f15ffdfdb5208075b58c45296f (patch)
treec9345a24b464e1aefc9d7343edf1df8fe6ae69c4 /activerecord
parent627ef06331b5c6c30e6100bfdaa38dbeb18c037a (diff)
downloadrails-1756094b22bf81f15ffdfdb5208075b58c45296f.tar.gz
rails-1756094b22bf81f15ffdfdb5208075b58c45296f.tar.bz2
rails-1756094b22bf81f15ffdfdb5208075b58c45296f.zip
Add DatabaseConfig Struct and associated methods
Passing around and parsing hashes is easy if you know that it's a two tier config and each key will be named after the environment and each value will be the config for that environment key. This falls apart pretty quickly with three-tier configs. We have no idea what the second tier will be named (we know the first is primary but we don't know the second), we have no easy way of figuring out how deep a hash we have without iterating over it, and we'd have to do this a lot throughout the code since it breaks all of Active Record's assumptions regarding configurations. These methods allow us to pass around objects instead. This will allow us to more easily parse the configs for the rake tasks. Evenually I'd like to replace the Active Record connection management that passes around config hashes to use these methods as well but that's much farther down the road. `walk_configs` takes an environment, specification name, and a config and turns them into DatabaseConfig struct objects so we can ask the configs questions like: ``` db_config.spec_name => animals db_config.env_name => development db_config.config { :adapter => mysql etc } ``` `db_configs` loops through all given configurations and returns an array of DatabaseConfig structs for each config in the yaml file. and lastly `configs_for` takes an environment and either returns the spec name and config if a block is given or returns an array of DatabaseConfig structs just for the given environment.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/core.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index e1a0b2ecf8..b6b4c76569 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -60,6 +60,45 @@ module ActiveRecord
@@configurations
end
+ DatabaseConfig = Struct.new(:env_name, :spec_name, :config) # :nodoc
+
+ # Given an env, spec and config creates DatabaseConfig structs with
+ # each attribute set.
+ def self.walk_configs(env_name, spec_name, config) # :nodoc:
+ if config["database"] || env_name == "default"
+ DatabaseConfig.new(env_name, spec_name, config)
+ else
+ config.each_pair.map do |spec_name, sub_config|
+ walk_configs(env_name, spec_name, sub_config)
+ end
+ end
+ end
+
+ # Walks all the configs passed in and returns an array
+ # of DatabaseConfig structs for each configuration.
+ def self.db_configs(configs = configurations) # :nodoc:
+ configs.each_pair.flat_map do |env_name, config|
+ walk_configs(env_name, "primary", config)
+ end
+ end
+
+ # Collects the configs for the environment passed in.
+ #
+ # If a block is given returns the specification name and configuration
+ # otherwise returns an array of DatabaseConfig structs for the environment.
+ def self.configs_for(environment, configs = configurations, &blk) # :nodoc:
+ env_with_configs = db_configs(configs).select do |db_config|
+ db_config.env_name == environment
+ end
+
+ if block_given?
+ env_with_configs.each do |env_with_config|
+ yield env_with_config.spec_name, env_with_config.config
+ end
+ else
+ env_with_configs
+ end
+ end
##
# :singleton-method:
# Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling