Add support to `connected_to` and `connects_to` for horizontal sharding #38531
+636
−52
Conversation
ddfc85f
to
836b107
ada5c8e
to
0b167e4
0bc6b8c
to
1ebffba
...d/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb
Outdated
Show resolved
Hide resolved
...d/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb
Show resolved
Hide resolved
...d/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb
Show resolved
Hide resolved
1ebffba
to
92aed8f
Great work on this! |
Applications can now connect to multiple shards and switch between their shards in an application. Note that the shard swapping is still a manual process as this change does not include an API for automatic shard swapping. Usage: Given the following configuration: ```yaml production: primary: database: my_database primary_shard_one: database: my_database_shard_one ``` Connect to multiple shards: ```ruby class ApplicationRecord < ActiveRecord::Base self.abstract_class = true connects_to shards: { default: { writing: :primary }, shard_one: { writing: :primary_shard_one } } ``` Swap between shards in your controller / model code: ```ruby ActiveRecord::Base.connected_to(shard: :shard_one) do # Read from shard one end ``` The horizontal sharding API also supports read replicas. See guides for more details. This PR also moves some no-doc'd methods into the private namespace as they were unnecessarily public. We've updated some error messages and documentation. Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
92aed8f
to
384e7d1
```ruby | ||
ActiveRecord::Base.connected_to(shard: :default) do | ||
@id = Record.create! # creates a record in shard one |
yogeshjain999
Nov 3, 2020
Should the comment mention # creates a record in default shard
?
Should the comment mention # creates a record in default shard
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Applications can now connect to multiple shards and switch between
their shards in an application. Note that the shard swapping is
still a manual process as this change does not include an API for
automatic shard swapping.
Usage:
Given the following configuration:
Connect to multiple shards:
Swap between shards in your controller / model code:
The horizontal sharding API also supports read replicas. See
guides for more details.
This PR also moves some no-doc'd methods into the private namespace as
they were unnecessarily public. We've updated some error messages and
documentation.
cc / @eileencodes @tenderlove @rafaelfranca @matthewd