mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #36814 from eileencodes/introduce-invalid-configuration-error
Introduce InvalidConfigurationError
This commit is contained in:
commit
f3c68c59ed
2 changed files with 23 additions and 3 deletions
|
@ -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 @@ module ActiveRecord
|
|||
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 @@ module ActiveRecord
|
|||
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" } }
|
||||
|
|
Loading…
Reference in a new issue