Navigation Menu

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

eileencodes
Copy link
Member

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
Copy link
Member

fxn commented Jun 3, 2019

❤️

Copy link
Member

@jhawthorn jhawthorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@eileencodes
Copy link
Member Author

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

Ah yes I knew I was forgetting something.

@eileencodes
Copy link
Member Author

Updated the guides with connected_to documentation.

@eileencodes
Copy link
Member Author

Updated to include a section about migrations.

database: my_primary_database
user: root_readonly
adapter: mysql
animals:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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

```ruby
class AnimalsBase < ApplicationRecord
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't there replica: true missing?

database: my_animals_database
user: animals_readonly
adapter: mysql
migrations_paths: db/animals_migrate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, fixed.

This file documents how to use multiple databases, what features are
supported, what features are coming soon, and caveats.
@eileencodes eileencodes merged commit f614be3 into rails:master Jun 4, 2019
@eileencodes eileencodes deleted the multi-db-docs branch June 4, 2019 14:46
eileencodes added a commit that referenced this pull request Jun 14, 2019
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MyCookieResovler -> MyCookieResolver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants