@@ -58,10 +58,10 @@ Let's add a replica for the first configuration, and a second database called an
58
58
replica for that as well. To do this we need to change our ` database.yml` from a 2-tier
59
59
to a 3-tier config.
60
60
61
- If a primary configuration is provided this will be used as the "default" configuration. If
62
- there is no configuration named "primary" Rails will use the first configuration for an
63
- environment. The default configurations will use the default Rails filenames. For example
64
- primary configurations will use `schema.rb` for the schema file whereas all other entries
61
+ If a primary configuration is provided, it will be used as the "default" configuration. If
62
+ there is no configuration named ` "primary"`, Rails will use the first configuration as default
63
+ for each environment. The default configurations will use the default Rails filenames. For example,
64
+ primary configurations will use `schema.rb` for the schema file, whereas all the other entries
65
65
will use `[CONFIGURATION_NAMESPACE]_schema.rb` for the filename.
66
66
67
67
` ` ` yaml
@@ -91,19 +91,19 @@ production:
91
91
replica: true
92
92
` ` `
93
93
94
- When using multiple databases there are a few important settings.
94
+ When using multiple databases, there are a few important settings.
95
95
96
96
First, the database name for the `primary` and `primary_replica` should be the same because they contain
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
100
replica user's permissions should be set to only read and not write.
101
101
102
- When using a replica database you need to add a `replica : true` entry to the replica in the
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
104
and which one is the writer.
105
105
106
- Lastly, for new writer databases you need to set the `migrations_paths` to the directory
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`
108
108
later on in this guide.
109
109
@@ -161,7 +161,7 @@ clients have a limit to the number of open connections there can be and if you d
161
161
multiply the number of connections you have since Rails uses the model class name for the
162
162
connection specification name.
163
163
164
- Now that we have the ` database.yml ` and the new model set up it's time to create the databases.
164
+ Now that we have the ` database.yml ` and the new model set up, it's time to create the databases.
165
165
Rails 6.0 ships with all the rails tasks you need to use multiple databases in Rails.
166
166
167
167
You can run ` bin/rails -T ` to see all the commands you're able to run. You should see the following:
@@ -192,7 +192,7 @@ rails db:schema:load:primary # Loads a database schema file (either
192
192
```
193
193
194
194
Running a command like ` bin/rails db:create ` will create both the primary and animals databases.
195
- Note that there is no command for creating the users and you'll need to do that manually
195
+ Note that there is no command for creating the database users, and you'll need to do that manually
196
196
to support the readonly users for your replicas. If you want to create just the animals
197
197
database you can run ` bin/rails db:create:animals ` .
198
198
@@ -213,7 +213,7 @@ $ bin/rails generate migration CreateDogs name:string --database animals
213
213
```
214
214
215
215
If you are using Rails generators, the scaffold and model generators will create the abstract
216
- class for you. Simply pass the database key to the command line
216
+ class for you. Simply pass the database key to the command line.
217
217
218
218
``` bash
219
219
$ bin/rails generate scaffold Dog name:string --database animals
@@ -243,7 +243,7 @@ add this to the abstract class after you're done.
243
243
Rails will only generate the new class once. It will not be overwritten by new scaffolds
244
244
or deleted if the scaffold is deleted.
245
245
246
- If you already have an abstract class and its name differs from ` AnimalsRecord ` you can pass
246
+ If you already have an abstract class and its name differs from ` AnimalsRecord ` , you can pass
247
247
the ` --parent ` option to indicate you want a different abstract class:
248
248
249
249
``` bash
@@ -255,11 +255,11 @@ use a different parent class.
255
255
256
256
## Activating automatic connection switching
257
257
258
- Finally, in order to use the read-only replica in your application you'll need to activate
258
+ Finally, in order to use the read-only replica in your application, you'll need to activate
259
259
the middleware for automatic switching.
260
260
261
261
Automatic switching allows the application to switch from the writer to replica or replica
262
- to writer based on the HTTP verb and whether there was a recent write.
262
+ to writer based on the HTTP verb and whether there was a recent write by the requesting user .
263
263
264
264
If the application is receiving a POST, PUT, DELETE, or PATCH request the application will
265
265
automatically write to the writer database. For the specified time after the write, the
371
371
` ` `
372
372
373
373
Then models can swap connections manually via the `connected_to` API. If
374
- using sharding both a `role` and `shard` must be passed :
374
+ using sharding, both a `role` and a `shard` must be passed :
375
375
376
376
` ` ` ruby
377
377
ActiveRecord::Base.connected_to(role: :writing, shard: :default) do
@@ -401,7 +401,7 @@ all databases globally. To use this feature you must first set
401
401
configuration. The majority of applications should not need to make any other
402
402
changes since the public APIs have the same behavior.
403
403
404
- With `legacy_connection_handling` set to false, any abstract connection class
404
+ With `legacy_connection_handling` set to ` false` , any abstract connection class
405
405
will be able to switch connections without affecting other connections. This
406
406
is useful for switching your `AnimalsRecord` queries to read from the replica
407
407
while ensuring your `ApplicationRecord` queries go to the primary.
@@ -458,6 +458,6 @@ will split the joins for you.
458
458
459
459
# ## Schema Cache
460
460
461
- If you use a schema cache and multiple databases you'll need to write an initializer
461
+ If you use a schema cache and multiple databases, you'll need to write an initializer
462
462
that loads the schema cache from your app. This wasn't an issue we could resolve in
463
463
time for Rails 6.0 but hope to have it in a future version soon.
0 commit comments