Skip to content

Commit

Permalink
Raise explicit error on env var conflicts (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjlarose authored Dec 9, 2020
1 parent aa627cf commit 2cc5b86
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

## Unreleased

### Bug fixes

...

### Changes

* Raise explicit error on environment variable conflicts ([#293](https://github.com/railsconfig/config/issues/293))

## 2.2.2

### Bug fixes
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ Settings.add_source!("#{Rails.root}/config/settings/local.yml")
Settings.reload!
```

> Note: this is an example usage, it is easier to just use the default local files `settings.local.yml,
settings/#{Rails.env}.local.yml and environments/#{Rails.env}.local.yml` for your developer specific settings.
> Note: this is an example usage, it is easier to just use the default local
> files `settings.local.yml`, `settings/#{Rails.env}.local.yml` and
> `environments/#{Rails.env}.local.yml` for your developer specific settings.
You also have the option to add a raw hash as a source. One use case might be storing settings in the database or in environment variables that overwrite what is in the YML files.

Expand Down Expand Up @@ -419,6 +420,21 @@ ENV['Settings.section.server'] = 'google.com'

It won't work with arrays, though.

It is considered an error to use environment variables to simutaneously assign a "flat" value and a multi-level value to a key.

```ruby
# Raises an error when settings are loaded
ENV['BACKEND_DATABASE'] = 'development'
ENV['BACKEND_DATABASE_USER'] = 'postgres'
```

Instead, specify keys of equal depth in the environment variable names:

```ruby
ENV['BACKEND_DATABASE_NAME'] = 'development'
ENV['BACKEND_DATABASE_USER'] = 'postgres'
```

### Working with Heroku

Heroku uses ENV object to store sensitive settings. You cannot upload such files to Heroku because it's ephemeral filesystem gets recreated from the git sources on each instance refresh. To use config with Heroku just set the `use_env` var to `true` as mentioned above.
Expand Down
5 changes: 5 additions & 0 deletions lib/config/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def reload_env!
h[key] ||= {}
}

unless leaf.is_a?(Hash)
conflicting_key = (prefix + keys[0...-1]).join(separator)
raise "Environment variable #{variable} conflicts with variable #{conflicting_key}"
end

leaf[keys.last] = Config.env_parse_values ? __value(value) : value
end

Expand Down
18 changes: 18 additions & 0 deletions spec/config_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,23 @@
expect(config.new_var).to eq('value')
end

context 'and env variable names conflict with new namespaces' do
it 'should throw a descriptive error message' do
ENV['Settings.backend_database'] = 'development'
ENV['Settings.backend_database.user'] = 'postgres'

expected_message = 'Environment variable Settings.backend_database.user '\
'conflicts with variable Settings.backend_database'
expect { config }.to raise_error(RuntimeError, expected_message)
end
end

context 'and env variable names conflict with existing namespaces' do
it 'should allow overriding the namespace' do
ENV['Settings.databases'] = 'new databases'

expect(config.databases).to eq('new databases')
end
end
end
end

0 comments on commit 2cc5b86

Please sign in to comment.