The goal is to configure all 3 default environments ('development', 'test', 'production') from the ENV
hash. How can I tell Rails to establish the following mapping, where main
is defined in config/database.yml
below?
'development' -> main
'test' -> main
'production' -> main
DB configs are consolidated to main
in config/database.yml
like so:
defaults: &defaults
adapter: postgresql
encoding: utf8
port: 5432
timeout: 10000
main:
<<: *defaults
url: <%= ENV['MAIN_DATABASE_URL'] %>
pool: <%= ENV['MAIN_DATABASE_POOL'] %>
In :development
& :test
, gem 'dotenv-rails', groups: [:development, :test]
loads the runtime env from .env
files. In :production
the ENV is set via other means.
Alas, when I do rails s
, this errors:
.../activerecord-5.2.0/lib/active_record/connection_adapters/connection_specification.rb:260:in `resolve_symbol_connection':
'development' database is not configured. Available: ["defaults", "main"] (ActiveRecord::AdapterNotSpecified)
And the docs on DB configuration don't have an answer.
CodePudding user response:
If you add an anchor to main
, e.g. &main
:
main: &main
<<: *defaults
url: <%= ENV['MAIN_DATABASE_URL'] %>
pool: <%= ENV['MAIN_DATABASE_POOL'] %>
You can use it to define your environments:
development: *main
test: *main
production: *main
or, just like you did with defaults
:
development:
<<: *main
test:
<<: *main
production:
<<: *main
The latter allows you to add additional key-value pairs or to override existing ones.