aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/tasks
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2018-03-20 13:47:34 -0400
committereileencodes <eileencodes@gmail.com>2018-03-21 16:54:24 -0400
commit4e663c1e8d1e643783f157f904b5276e62f65589 (patch)
treec258273b4cbf8451f32d13692f8a0ffa183b6cdc /activerecord/lib/active_record/tasks
parentbb9e5540c8fd3f48d461362c652cb86c17d9c5f3 (diff)
downloadrails-4e663c1e8d1e643783f157f904b5276e62f65589.tar.gz
rails-4e663c1e8d1e643783f157f904b5276e62f65589.tar.bz2
rails-4e663c1e8d1e643783f157f904b5276e62f65589.zip
Refactor configs_for and friends
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.
Diffstat (limited to 'activerecord/lib/active_record/tasks')
-rw-r--r--activerecord/lib/active_record/tasks/database_tasks.rb9
1 files changed, 8 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb
index e2c292ee56..5787660148 100644
--- a/activerecord/lib/active_record/tasks/database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/database_tasks.rb
@@ -134,6 +134,13 @@ module ActiveRecord
end
end
+ def for_each
+ databases = Rails.application.config.load_database_yaml
+ ActiveRecord::DatabaseConfigurations.configs_for(Rails.env, databases) do |spec_name, _|
+ yield spec_name
+ end
+ end
+
def create_current(environment = env)
each_current_configuration(environment) { |configuration|
create configuration
@@ -327,7 +334,7 @@ module ActiveRecord
environments << "test" if environment == "development"
environments.each do |env|
- ActiveRecord::Base.configs_for(env) do |spec_name, configuration|
+ ActiveRecord::DatabaseConfigurations.configs_for(env) do |spec_name, configuration|
yield configuration, spec_name, env
end
end