You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Main points:
* Applies our documentation guidelines (names of files, punctuation in comments, etc.)
* AnimalsRecord was introduced before the primary abstract class. Before this
point, stuff related to the primary database is explained first, and then you
go for the extra database. This is the natural order, I think. I reordered
for consistency.
* PrimaryApplicationRecord was missing connects_to.
* Fixes parent class for the example related to PrimaryApplicationRecord.
In Rails 6.1 it's possible to switch connections for one database instead of
528
-
all databases globally.
540
+
Starting from Rails 6.1, it's possible to switch connections for one database
541
+
instead of all databases globally.
529
542
530
543
With granular database connection switching, any abstract connection class
531
544
will be able to switch connections without affecting other connections. This
@@ -534,27 +547,30 @@ while ensuring your `ApplicationRecord` queries go to the primary.
534
547
535
548
```ruby
536
549
AnimalsRecord.connected_to(role::reading) do
537
-
Dog.first # Reads from animals_replica
538
-
Person.first # Reads from primary
550
+
Dog.first # Reads from animals_replica.
551
+
Person.first # Reads from primary.
539
552
end
540
553
```
541
554
542
555
It's also possible to swap connections granularly for shards.
543
556
544
557
```ruby
545
558
AnimalsRecord.connected_to(role::reading, shard::shard_one) do
546
-
Dog.first # Will read from shard_one_replica. If no connection exists for shard_one_replica,
547
-
# a ConnectionNotEstablished error will be raised
548
-
Person.first # Will read from primary writer
559
+
# Will read from shard_one_replica. If no connection exists for shard_one_replica,
560
+
# a ConnectionNotEstablished error will be raised.
561
+
Dog.first
562
+
563
+
# Will read from primary writer.
564
+
Person.first
549
565
end
550
566
```
551
567
552
568
To switch only the primary database cluster use `ApplicationRecord`:
553
569
554
570
```ruby
555
571
ApplicationRecord.connected_to(role::reading, shard::shard_one) do
556
-
Person.first # Reads from primary_shard_one_replica
557
-
Dog.first # Reads from animals_primary
572
+
Person.first # Reads from primary_shard_one_replica.
573
+
Dog.first # Reads from animals_primary.
558
574
end
559
575
```
560
576
@@ -619,13 +635,17 @@ There are some important things to be aware of with this option:
619
635
620
636
### Schema Caching
621
637
622
-
If you want to load a schema cache for each database you must set a `schema_cache_path` in each database configuration and set `config.active_record.lazily_load_schema_cache = true` in your application configuration. Note that this will lazily load the cache when the database connections are established.
638
+
If you want to load a schema cache for each database you must set
639
+
`schema_cache_path` in each database configuration and set
640
+
`config.active_record.lazily_load_schema_cache = true` in your application
641
+
configuration. Note that this will lazily load the cache when the database
642
+
connections are established.
623
643
624
644
## Caveats
625
645
626
646
### Load Balancing Replicas
627
647
628
-
Rails also doesn't support automatic load balancing of replicas. This is very
629
-
dependent on your infrastructure. We may implement basic, primitive load balancing
630
-
in the future, but for an application at scale this should be something your application
631
-
handles outside of Rails.
648
+
Rails doesn't support automatic load balancing of replicas. This is very
649
+
dependent on your infrastructure. We may implement basic, primitive load
650
+
balancing in the future, but for an application at scale this should be
651
+
something your application handles outside of Rails.
0 commit comments