Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document multiple databases in Rails #36389

Merged
merged 1 commit into from Jun 4, 2019

Conversation

Projects
None yet
6 participants
@eileencodes
Copy link
Member

commented Jun 3, 2019

This file documents how to use multiple databases, what features are
supported, what features are coming soon, and caveats.

Do you think I'm missing anything important?

cc/ @tenderlove, @matthewd, @rafaelfranca, @jhawthorn

@rails-bot rails-bot bot added the docs label Jun 3, 2019

@eileencodes eileencodes added this to the 6.0.0 milestone Jun 3, 2019

@fxn

This comment has been minimized.

Copy link
Member

commented Jun 3, 2019

❤️

@jhawthorn
Copy link
Member

left a comment

Great! Would it make sense to have a section explaining how to use connected_to(role: ...)?

@eileencodes

This comment has been minimized.

Copy link
Member Author

commented Jun 3, 2019

Would it make sense to have a section explaining how to use connected_to(role: ...)?

Ah yes I knew I was forgetting something.

@eileencodes eileencodes force-pushed the eileencodes:multi-db-docs branch from 49a94ef to b0d1d7f Jun 3, 2019

@eileencodes

This comment has been minimized.

Copy link
Member Author

commented Jun 3, 2019

Updated the guides with connected_to documentation.

@eileencodes

This comment has been minimized.

Copy link
Member Author

commented Jun 3, 2019

Updated to include a section about migrations.

database: my_primary_database
user: root_readonly
adapter: mysql
animals:

This comment has been minimized.

Copy link
@morgoth

morgoth Jun 3, 2019

Member

Shouldn't it include example of migrations_paths config as well?

This comment has been minimized.

Copy link
@eileencodes

eileencodes Jun 3, 2019

Author Member

Added migrations_paths, however I think we don't actually need them anymore. But dealing with that is for another day 😄

need to create a new abstract class and connect to the animals databases.

```ruby
class AnimalsBase < ApplicationRecord

This comment has been minimized.

Copy link
@morgoth

morgoth Jun 3, 2019

Member

For simplicity shouldn't it inherit from ActiveRecord::Base?

This comment has been minimized.

Copy link
@eileencodes

eileencodes Jun 3, 2019

Author Member

Simplicity of the app or of the example? If you don't inherit from ApplicationRecord you'll end up having to rewrite everything you rely on for all your models that is in ApplicationRecord.

This comment has been minimized.

Copy link
@morgoth

morgoth Jun 3, 2019

Member

simplicity of example ;-)
if it inherits from ApplicationRecord, then self.abstract_class = true is redundant.
Anyway, not a big deal - I guess it's quite clear what is going on in both versions

@eileencodes eileencodes force-pushed the eileencodes:multi-db-docs branch from a047405 to a7a626b Jun 3, 2019

need to create a new abstract class and connect to the animals databases.

```ruby
class AnimalsBase < ApplicationRecord

This comment has been minimized.

Copy link
@morgoth

morgoth Jun 3, 2019

Member

simplicity of example ;-)
if it inherits from ApplicationRecord, then self.abstract_class = true is redundant.
Anyway, not a big deal - I guess it's quite clear what is going on in both versions

database: my_primary_database
user: root
adapter: mysql
primary_replica:

This comment has been minimized.

Copy link
@morgoth

morgoth Jun 3, 2019

Member

isn't there replica: true missing?

database: my_animals_database
user: animals_readonly
adapter: mysql
migrations_paths: db/animals_migrate

This comment has been minimized.

Copy link
@morgoth

morgoth Jun 3, 2019

Member

If it's replica, does it also need migrations_paths?

## Activating automatic connection switching

Finally, in order to use multiple databases in your application you'll need to activate
the middleware for automatic switching.

This comment has been minimized.

Copy link
@matthewd

matthewd Jun 4, 2019

Member

"in order to use the read-only replica", maybe?

"to use multiple databases" sounds a bit too generic: that would equally describe e.g. a primary + animals setup, which doesn't need any switching.

This comment has been minimized.

Copy link
@eileencodes

eileencodes Jun 4, 2019

Author Member

Good call, fixed.

Document multiple databases in Rails
This file documents how to use multiple databases, what features are
supported, what features are coming soon, and caveats.

@eileencodes eileencodes force-pushed the eileencodes:multi-db-docs branch from a7a626b to 8574c3e Jun 4, 2019

@eileencodes eileencodes merged commit f614be3 into rails:master Jun 4, 2019

2 checks passed

buildkite/rails Build #61496 passed (8 minutes, 54 seconds)
Details
codeclimate All good!
Details

@eileencodes eileencodes deleted the eileencodes:multi-db-docs branch Jun 4, 2019

eileencodes added a commit that referenced this pull request Jun 14, 2019

Merge pull request #36389 from eileencodes/multi-db-docs
Document multiple databases in Rails

First, the database name for the primary and replica should be the same because they contain
the same data. Second, the username for the primary and replica should be different, and the
replica user's permissions should be to to read and not write.

This comment has been minimized.

Copy link
@vtamara

vtamara Jun 25, 2019

to to read -> to read

```ruby
config.active_record.database_selector = { delay: 2.seconds }
config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
config.active_record.database_resolver_context = MyCookieResovler

This comment has been minimized.

Copy link
@vtamara

vtamara Jun 25, 2019

MyCookieResovler -> MyCookieResolver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.