@@ -97,11 +97,11 @@ First, the database name for the `primary` and `primary_replica` should be the s
97
97
the same data. This is also the case for `animals` and `animals_replica`.
98
98
99
99
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.
101
101
102
102
When using a replica database, you need to add a `replica : true` entry to the replica in the
103
103
` 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.
105
105
106
106
Lastly, for new writer databases, you need to set the `migrations_paths` to the directory
107
107
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
328
328
like ` connected_to(role: :nonexistent) ` you will get an error that says
329
329
` ActiveRecord::ConnectionNotEstablished (No connection pool for 'ActiveRecord::Base' found for the 'nonexistent' role.) `
330
330
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
+
331
341
## Horizontal sharding
332
342
333
343
Horizontal sharding is when you split up your database to reduce the number of rows on each
0 commit comments