Skip to content

Commit

Permalink
Merge pull request #28896 from pschambacher/load_with_shared
Browse files Browse the repository at this point in the history
Added a shared section to config/database.yml that will be loaded for all envs
  • Loading branch information
rafaelfranca committed Apr 27, 2017
1 parent d41e41d commit 92278a5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,8 @@
* Added a shared section to config/database.yml that will be loaded for all environments.

*Pierre Schambacher*


## Rails 5.1.0.rc2 (April 20, 2017) ##

* Namespace error pages' CSS selectors to stop the styles from bleeding into other pages
Expand Down
9 changes: 8 additions & 1 deletion railties/lib/rails/application/configuration.rb
Expand Up @@ -130,7 +130,14 @@ def database_configuration
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.
Expand Down
34 changes: 34 additions & 0 deletions railties/test/application/configuration_test.rb
Expand Up @@ -1407,6 +1407,40 @@ def index
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"

Expand Down

0 comments on commit 92278a5

Please sign in to comment.