From 43d83e96c922af78129d1ee83b95866121d7cf81 Mon Sep 17 00:00:00 2001 From: Omri Gabay Date: Sat, 24 Oct 2020 18:09:37 -0700 Subject: [PATCH] Add option to mute multiple database yaml warning Adds an option to silence the warning that database configurations can throw when it's unparsable. --- .../dummy/config/environments/development.rb | 4 ++++ .../dummy/config/environments/development.rb | 4 ++++ activerecord/CHANGELOG.md | 5 +++++ activerecord/lib/active_record/core.rb | 6 +++++ .../lib/active_record/tasks/database_tasks.rb | 4 +++- .../test/application/configuration_test.rb | 22 +++++++++++++++++++ 6 files changed, 44 insertions(+), 1 deletion(-) diff --git a/actionmailbox/test/dummy/config/environments/development.rb b/actionmailbox/test/dummy/config/environments/development.rb index 6ecb772203..4bad40905a 100644 --- a/actionmailbox/test/dummy/config/environments/development.rb +++ b/actionmailbox/test/dummy/config/environments/development.rb @@ -46,6 +46,10 @@ Rails.application.configure do # Highlight code that triggered database queries in logs. config.active_record.verbose_query_logs = true + # Show a warning when Rails couldn't parse your database.yml + # for multiple databases. + config.active_record.suppress_multiple_database_warning = false + # Debug mode disables concatenation and preprocessing of assets. # This option may cause significant delays in view rendering with a large # number of complex assets. diff --git a/actiontext/test/dummy/config/environments/development.rb b/actiontext/test/dummy/config/environments/development.rb index 6ecb772203..4bad40905a 100644 --- a/actiontext/test/dummy/config/environments/development.rb +++ b/actiontext/test/dummy/config/environments/development.rb @@ -46,6 +46,10 @@ Rails.application.configure do # Highlight code that triggered database queries in logs. config.active_record.verbose_query_logs = true + # Show a warning when Rails couldn't parse your database.yml + # for multiple databases. + config.active_record.suppress_multiple_database_warning = false + # Debug mode disables concatenation and preprocessing of assets. # This option may cause significant delays in view rendering with a large # number of complex assets. diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index ccc19a5d36..1749d554df 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +* Allow users to silence the "Rails couldn't infer whether you are using multiple databases..." + message using `config.active_record.suppress_multiple_database_warning`. + + *Omri Gabay* + * Connections can be granularly switched for abstract classes when `connected_to` is called. This change allows `connected_to` to switch a `role` and/or `shard` for a single abstract class instead of all classes globally. Applications that want to use the new feature need to set `config.active_record.legacy_connection_handling` to `false` in their application configuration. diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 41be2701cb..69b89afe51 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -131,6 +131,12 @@ module ActiveRecord # potentially cause memory bloat. mattr_accessor :warn_on_records_fetched_greater_than, instance_writer: false + ## + # :singleton-method: + # Show a warning when Rails couldn't parse your database.yml + # for multiple databases. + mattr_accessor :suppress_multiple_database_warning, instance_writer: false, default: false + mattr_accessor :maintain_test_schema, instance_accessor: false class_attribute :belongs_to_required_by_default, instance_accessor: false diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 650650f00b..99aacf226b 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -154,7 +154,9 @@ module ActiveRecord 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." + unless ActiveRecord::Base.suppress_multiple_database_warning + $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 diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 557398da2c..8a897b8fcf 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -1626,6 +1626,11 @@ module ApplicationTests assert_not ActiveRecord::Base.verbose_query_logs end + test "config.active_record.suppress_multiple_database_warning is false by default in development" do + app "development" + assert_not ActiveRecord::Base.suppress_multiple_database_warning + end + test "config.annotations wrapping SourceAnnotationExtractor::Annotation class" do make_basic_app do |application| application.config.annotations.register_extensions("coffee") do |tag| @@ -1865,6 +1870,23 @@ module ApplicationTests assert_equal({}, Rails.application.config.load_database_yaml) end + test "setup_initial_database_yaml does not print a warning if config.active_record.suppress_multiple_database_warning is true" do + app_file "config/database.yml", <<-YAML + <%= Rails.env %>: + username: bobby + adapter: sqlite3 + database: 'dev_db' + YAML + add_to_config <<-RUBY + config.active_record.suppress_multiple_database_warning = true + RUBY + app "development" + + assert_silent do + ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml + end + end + test "raises with proper error message if no database configuration found" do FileUtils.rm("#{app_path}/config/database.yml") err = assert_raises RuntimeError do