aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/tasks
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2019-06-26 14:25:11 -0400
committereileencodes <eileencodes@gmail.com>2019-06-27 09:54:25 -0400
commitdf6b0de7d9522c46c140c556ee61e09df14ef97a (patch)
treeb343408225debc541b0ef56f3c24312e5d7f4935 /activerecord/lib/active_record/tasks
parentb65f88652f835093030bf65b9e31e727ba58a6de (diff)
downloadrails-df6b0de7d9522c46c140c556ee61e09df14ef97a.tar.gz
rails-df6b0de7d9522c46c140c556ee61e09df14ef97a.tar.bz2
rails-df6b0de7d9522c46c140c556ee61e09df14ef97a.zip
Load initial database.yml once, and warn if we can't create tasks
For multiple databases we attempt to generate the tasks by reading the database.yml before the Rails application is booted. This means that we need to strip out ERB since it could be reading Rails configs. In some cases like https://github.com/rails/rails/issues/36540 the ERB is too complex and we can't overwrite with the DummyCompilier we used in https://github.com/rails/rails/pull/35497. For the complex causes we simply issue a warning that says we couldn't infer the database tasks from the database.yml. While working on this I decided to update the code to only load the database.yml once initially so that we avoid having to issue the same warning multiple times. Note that this had no performance impact in my testing and is merely for not having to save the error off somewhere. Also this feels cleaner. Note that this will not break running tasks that exist, it will just mean that tasks for multi-db like `db:create:other_db` will not be generated. If the database.yml is actually unreadable it will blow up during normal rake task calls. Fixes #36540
Diffstat (limited to 'activerecord/lib/active_record/tasks')
-rw-r--r--activerecord/lib/active_record/tasks/database_tasks.rb13
1 files changed, 11 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb
index aecc9350e8..a78bebf764 100644
--- a/activerecord/lib/active_record/tasks/database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/database_tasks.rb
@@ -141,10 +141,19 @@ module ActiveRecord
end
end
- def for_each
+ def setup_initial_database_yaml
return {} unless defined?(Rails)
- databases = Rails.application.config.load_database_yaml
+ begin
+ Rails.application.config.load_database_yaml
+ rescue
+ $stderr.puts "Rails couldn't infer whether you are using multiple databases from your database.yml and can't generate the tasks for the non-primary databases. If you'd like to use this feature, please simplify your ERB."
+
+ {}
+ end
+ end
+
+ def for_each(databases)
database_configs = ActiveRecord::DatabaseConfigurations.new(databases).configs_for(env_name: Rails.env)
# if this is a single database application we don't want tasks for each primary database