Skip to content

Commit 6289080

Browse files
committed
Document configuring non-primary db with env var
This documents the behaviour added in rails#36773 which allows setting a database connection URL in an environment variable named "#{name.upcase}_DATABASE_URL".
1 parent 951ea8f commit 6289080

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

guides/source/active_record_multiple_databases.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ production:
8686
replica: true
8787
```
8888

89+
NOTE: In the same way that it's possible to [configure](configuring.html#configuring-a-database)
90+
the primary database by setting the connection string in the `DATABASE_URL` environment variable,
91+
it's possible to configure other databases using an environment variable prefixed with the
92+
configuration key in uppercase. For example, setting:
93+
`ANIMALS_DATABASE_URL="mysql2://username:password@host/database"` overrides the `animals`
94+
configuration in the "production" environment.
95+
8996
When using multiple databases, there are a few important settings.
9097

9198
First, the database name for `primary` and `primary_replica` should be the same because they contain

railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml.tt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ test:
4949
# production:
5050
# url: <%%= ENV["MY_APP_DATABASE_URL"] %>
5151
#
52+
# The connection URL for non-primary databases can also be configured using a
53+
# similar environment variable prefixed with the configuration key in uppercase.
54+
# For example:
55+
#
56+
# CACHE_DATABASE_URL="mysql2://cacheuser:cachepass@localhost/cachedatabase"
57+
#
5258
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
5359
# for a full overview on how database connection configuration can be specified.
5460
#

railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ test:
8181
# production:
8282
# url: <%%= ENV["MY_APP_DATABASE_URL"] %>
8383
#
84+
# The connection URL for non-primary databases can also be configured using a
85+
# similar environment variable prefixed with the configuration key in uppercase.
86+
# For example:
87+
#
88+
# CACHE_DATABASE_URL="postgres://cacheuser:cachepass@localhost/cachedatabase"
89+
#
8490
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
8591
# for a full overview on how database connection configuration can be specified.
8692
#

railties/lib/rails/generators/rails/app/templates/config/databases/trilogy.yml.tt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ test:
5151
# production:
5252
# url: <%%= ENV["MY_APP_DATABASE_URL"] %>
5353
#
54+
# The connection URL for non-primary databases can also be configured using a
55+
# similar environment variable prefixed with the configuration key in uppercase.
56+
# For example:
57+
#
58+
# CACHE_DATABASE_URL="trilogy://cacheuser:cachepass@localhost/cachedatabase"
59+
#
5460
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
5561
# for a full overview on how database connection configuration can be specified.
5662
#

0 commit comments

Comments
 (0)