提交 dde03a92 编写于 作者: E eileencodes

Introduce InvalidConfigurationError

In our app at work we had a faked config like this:

```
{ "foo" => :bar, "bar" => { "adapter" => "memory" } }
```

This config is invalid. You can't say for foo env just have a symbol,
nor would this work if you had fa foo env with just a string. A
configuration must be a url or an adapter or a database. Otherwise it's
invalid and we can't parse it.

When this was just yaml turned into hashes you could get away with
passing whatever. It wouldn't work but it wouldn't blow up either.

Now that we're using objects we were returning `nil` for these but that
just means we either blow up on `for_current_env` or compact the
`nil`'s.

I think it's a better user experience to not build the configs and raise
an appropriate error.

This is also an invalid config because if you do pass a string here it
should be a URL.

```
{ "foo" => "bar", "bar" => { "adapter" => "memory" } }
```
上级 4f1bd290
......@@ -9,6 +9,8 @@ module ActiveRecord
# objects (either a HashConfig or UrlConfig) that are constructed from the
# application's database configuration hash or URL string.
class DatabaseConfigurations
class InvalidConfigurationError < StandardError; end
attr_reader :configurations
delegate :any?, to: :configurations
......@@ -146,17 +148,19 @@ def build_db_config_from_raw_config(env_name, spec_name, config)
build_db_config_from_string(env_name, spec_name, config)
when Hash
build_db_config_from_hash(env_name, spec_name, config.stringify_keys)
else
raise InvalidConfigurationError, "'{ #{env_name} => #{config} }' is not a valid configuration. Expected '#{config}' to be a URL string or a Hash."
end
end
def build_db_config_from_string(env_name, spec_name, config)
url = config
uri = URI.parse(url)
if uri&.scheme
if uri.scheme
ActiveRecord::DatabaseConfigurations::UrlConfig.new(env_name, spec_name, url)
else
raise InvalidConfigurationError, "'{ #{env_name} => #{config} }' is not a valid configuration. Expected '#{config}' to be a URL string or a Hash."
end
rescue URI::InvalidURIError
ActiveRecord::DatabaseConfigurations::HashConfig.new(env_name, spec_name, config)
end
def build_db_config_from_hash(env_name, spec_name, config)
......
......@@ -28,6 +28,22 @@ def resolve_spec(spec, config)
resolver.resolve(spec, spec)
end
def test_invalid_string_config
config = { "foo" => "bar" }
assert_raises ActiveRecord::DatabaseConfigurations::InvalidConfigurationError do
resolve_config(config)
end
end
def test_invalid_symbol_config
config = { "foo" => :bar }
assert_raises ActiveRecord::DatabaseConfigurations::InvalidConfigurationError do
resolve_config(config)
end
end
def test_resolver_with_database_uri_and_current_env_symbol_key
ENV["DATABASE_URL"] = "postgres://localhost/foo"
config = { "not_production" => { "adapter" => "not_postgres", "database" => "not_foo" } }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册