-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves the configs_for and DatabaseConfig struct into it's own file. I was considering doing this in a future refactoring but our set up forced me to move it now. You see there are `mattr_accessor`'s on the Core module that have default settings. For example the `schema_format` defaults to Ruby. So if I call `configs_for` or any methods in the Core module it will reset the `schema_format` to `:ruby`. By moving it to it's own class we can keep the logic contained and avoid this unfortunate issue. The second change here does a double loop over the yaml files. Bear with me... Our tests dictate that we need to load an environment before our rake tasks because we could have something in an environment that the database.yml depends on. There are side-effects to this and I think there's a deeper bug that needs to be fixed but that's for another issue. The gist of the problem is when I was creating the dynamic rake tasks if the yaml that that rake task is calling evaluates code (like erb) that calls the environment configs the code will blow up because the environment is not loaded yet. To avoid this issue we added a new method that simply loads the yaml and does not evaluate the erb or anything in it. We then use that yaml to create the task name. Inside the task name we can then call `load_config` and load the real config to actually call the code internal to the task. I admit, this is gross, but refactoring can't all be pretty all the time and I'm working hard with `@tenderlove` to refactor much more of this code to get to a better place re connection management and rake tasks.
- Loading branch information
1 parent
bb9e554
commit 4e663c1
Showing
7 changed files
with
101 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecord | ||
module DatabaseConfigurations # :nodoc: | ||
class DatabaseConfig | ||
attr_reader :env_name, :spec_name, :config | ||
|
||
def initialize(env_name, spec_name, config) | ||
@env_name = env_name | ||
@spec_name = spec_name | ||
@config = config | ||
end | ||
end | ||
|
||
# Selects the config for the specified environment and specification name | ||
# | ||
# For example if passed :development, and :animals it will select the database | ||
# under the :development and :animals configuration level | ||
def self.config_for_env_and_spec(environment, specification_name, configs = ActiveRecord::Base.configurations) # :nodoc: | ||
configs_for(environment, configs).find do |db_config| | ||
db_config.spec_name == specification_name | ||
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(env, configs = ActiveRecord::Base.configurations, &blk) # :nodoc: | ||
env_with_configs = db_configs(configs).select do |db_config| | ||
db_config.env_name == env | ||
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 | ||
|
||
# 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 = ActiveRecord::Base.configurations) # :nodoc: | ||
configs.each_pair.flat_map do |env_name, config| | ||
walk_configs(env_name, "primary", config) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters