aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorPierre Schambacher <pschambacher@zendesk.com>2017-04-26 19:00:33 -0700
committerPierre Schambacher <pschambacher@zendesk.com>2017-04-26 20:47:56 -0700
commitdfc361df364c0c01a244355fe39f5871fb5b00f9 (patch)
tree4db548ba9762538e1a9a510079a91a93c1625fd8 /railties
parenteac6f3690fd245b9d72aadcfa365ed16c4ef8b96 (diff)
downloadrails-dfc361df364c0c01a244355fe39f5871fb5b00f9.tar.gz
rails-dfc361df364c0c01a244355fe39f5871fb5b00f9.tar.bz2
rails-dfc361df364c0c01a244355fe39f5871fb5b00f9.zip
Added a shared section to config/database.yml that will be loaded for all environments
Diffstat (limited to 'railties')
-rw-r--r--railties/CHANGELOG.md10
-rw-r--r--railties/lib/rails/application/configuration.rb9
-rw-r--r--railties/test/application/configuration_test.rb34
3 files changed, 49 insertions, 4 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md
index 5463b05b60..e6ef177451 100644
--- a/railties/CHANGELOG.md
+++ b/railties/CHANGELOG.md
@@ -1,8 +1,12 @@
+* Added a shared section to config/database.yml that will be loaded for all environments.
+
+ *Pierre Schambacher*
+
* Namespace error pages' CSS selectors to stop the styles from bleeding into other pages
- when using Turbolinks.
-
+ when using Turbolinks.
+
*Jan Krutisch*
-
+
Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/railties/CHANGELOG.md) for previous changes.
diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb
index 7c49fabba5..27c1572357 100644
--- a/railties/lib/rails/application/configuration.rb
+++ b/railties/lib/rails/application/configuration.rb
@@ -133,7 +133,14 @@ module Rails
config = if yaml && yaml.exist?
require "yaml"
require "erb"
- YAML.load(ERB.new(yaml.read).result) || {}
+ loaded_yaml = YAML.load(ERB.new(yaml.read).result) || {}
+ shared = loaded_yaml.delete("shared")
+ if shared
+ loaded_yaml.each do |_k, values|
+ values.reverse_merge!(shared)
+ end
+ end
+ Hash.new(shared).merge(loaded_yaml)
elsif ENV["DATABASE_URL"]
# Value from ENV['DATABASE_URL'] is set to default database connection
# by Active Record.
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index 07c6afed7f..06767167a9 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -1407,6 +1407,40 @@ module ApplicationTests
assert_match "config/database", err.message
end
+ test "loads database.yml using shared keys" do
+ app_file "config/database.yml", <<-YAML
+ shared:
+ username: bobby
+ adapter: sqlite3
+
+ development:
+ database: 'dev_db'
+ YAML
+
+ app "development"
+
+ ar_config = Rails.application.config.database_configuration
+ assert_equal "sqlite3", ar_config["development"]["adapter"]
+ assert_equal "bobby", ar_config["development"]["username"]
+ assert_equal "dev_db", ar_config["development"]["database"]
+ end
+
+ test "loads database.yml using shared keys for undefined environments" do
+ app_file "config/database.yml", <<-YAML
+ shared:
+ username: bobby
+ adapter: sqlite3
+ database: 'dev_db'
+ YAML
+
+ app "development"
+
+ ar_config = Rails.application.config.database_configuration
+ assert_equal "sqlite3", ar_config["development"]["adapter"]
+ assert_equal "bobby", ar_config["development"]["username"]
+ assert_equal "dev_db", ar_config["development"]["database"]
+ end
+
test "config.action_mailer.show_previews defaults to true in development" do
app "development"