diff options
| author | Eileen Uchitelle <eileencodes@gmail.com> | 2019-01-17 17:04:48 -0500 | 
|---|---|---|
| committer | Eileen Uchitelle <eileencodes@gmail.com> | 2019-01-17 17:26:32 -0500 | 
| commit | 83b995206a569d8d08b697ee9f86a64ca1854bcc (patch) | |
| tree | e4eb4f50445e20b701da8f104bd0f2508fd79aee /activerecord/lib/active_record | |
| parent | 69c963c2e8ac56f4832e767c3c83091b9c6f24dd (diff) | |
| download | rails-83b995206a569d8d08b697ee9f86a64ca1854bcc.tar.gz rails-83b995206a569d8d08b697ee9f86a64ca1854bcc.tar.bz2 rails-83b995206a569d8d08b697ee9f86a64ca1854bcc.zip  | |
Fix error message when adapter is not specified
When we added support for multiple databases through a 3-tiered config
and configuration objects this error message got a bit convoluted.
Previously if you had an application with a missing configuation and
multiple databases the error message would look like this:
```
'doesnexist' database is not configured. Available: development,
development, test, test, production, production
(ActiveRecord::AdapterNotSpecified)
```
That's not very descriptive since it duplicates the environments
(because there are multiple databases per environment for this
application).
To fix this I've constructed a bit more readable error message which now
reads like this if you have a multi db app:
```
The `doesntexist` database is not configured for the `production`
environment. (ActiveRecord::AdapterNotSpecified)
Available databases configurations are:
development: primary, primary_readonly
test: primary, primary_readonly
production: primary, primary_readonly
```
And like this if you have a single db app:
```
The `doesntexist` database is not configured for the `production`
environment. (ActiveRecord::AdapterNotSpecified)
Available databases configurations are:
development
test
```
This makes the error message more readable and presents the user all
available options for the database connections.
Diffstat (limited to 'activerecord/lib/active_record')
| -rw-r--r-- | activerecord/lib/active_record/connection_adapters/connection_specification.rb | 21 | 
1 files changed, 20 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/connection_specification.rb index f60d8469cc..9eaf9d9a89 100644 --- a/activerecord/lib/active_record/connection_adapters/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/connection_specification.rb @@ -248,10 +248,29 @@ module ActiveRecord              if db_config                resolve_connection(db_config.config).merge("name" => pool_name.to_s)              else -              raise(AdapterNotSpecified, "'#{env_name}' database is not configured. Available: #{configurations.configurations.map(&:env_name).join(", ")}") +              raise AdapterNotSpecified, <<~MSG +                The `#{env_name}` database is not configured for the `#{ActiveRecord::ConnectionHandling::DEFAULT_ENV.call}` environment. + +                Available databases configurations are: + +                #{build_configuration_sentence} +              MSG              end            end +          def build_configuration_sentence # :nodoc: +            configs = configurations.configs_for(include_replicas: true) + +            configs.group_by(&:env_name).map do |env, config| +              namespaces = config.map(&:spec_name) +              if namespaces.size > 1 +                "#{env}: #{namespaces.join(", ")}" +              else +                env +              end +            end.join("\n") +          end +            # Accepts a hash. Expands the "url" key that contains a            # URL database connection to a full connection            # hash and merges with the rest of the hash.  | 
