Skip to content

Commit 3dd44a7

Browse files
authored
Merge pull request rails#42456 from ghiculescu/patch-1
Multi DB docs improvements
2 parents 9a2c639 + 668c5df commit 3dd44a7

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

guides/source/active_record_multiple_databases.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ First, the database name for the `primary` and `primary_replica` should be the s
9797
the same data. This is also the case for `animals` and `animals_replica`.
9898

9999
Second, the username for the writers and replicas should be different, and the
100-
replica user's permissions should be set to only read and not write.
100+
replica user's database permissions should be set to only read and not write.
101101

102102
When using a replica database, you need to add a `replica: true` entry to the replica in the
103103
`database.yml`. This is because Rails otherwise has no way of knowing which one is a replica
104-
and which one is the writer.
104+
and which one is the writer. Rails will not run certain tasks, such as migrations, against replicas.
105105

106106
Lastly, for new writer databases, you need to set the `migrations_paths` to the directory
107107
where you will store migrations for that database. We'll look more at `migrations_paths`
@@ -328,6 +328,16 @@ using the connection specification name. This means that if you pass an unknown
328328
like `connected_to(role: :nonexistent)` you will get an error that says
329329
`ActiveRecord::ConnectionNotEstablished (No connection pool for 'ActiveRecord::Base' found for the 'nonexistent' role.)`
330330

331+
If you want Rails to ensure any queries performed are read only, pass `prevent_writes: true`.
332+
This just prevents queries that look like writes from being sent to the database.
333+
You should also configure your replica database to run in readonly mode.
334+
335+
```ruby
336+
ActiveRecord::Base.connected_to(role: :reading, prevent_writes: true) do
337+
# Rails will check each query to ensure it's a read query
338+
end
339+
```
340+
331341
## Horizontal sharding
332342

333343
Horizontal sharding is when you split up your database to reduce the number of rows on each

0 commit comments

Comments
 (0)