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
Merged

Conversation

@eileencodes
Copy link
Member

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

@fxn fxn commented Jun 3, 2019

❤️

Copy link
Member

@jhawthorn jhawthorn left a comment

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

@eileencodes
Copy link
Member Author

@eileencodes eileencodes 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
Copy link
Member Author

@eileencodes eileencodes commented Jun 3, 2019

Updated the guides with connected_to documentation.

@eileencodes
Copy link
Member Author

@eileencodes eileencodes commented Jun 3, 2019

Updated to include a section about migrations.

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

@morgoth morgoth Jun 3, 2019

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

Copy link
Member Author

@eileencodes eileencodes Jun 3, 2019

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

@morgoth morgoth Jun 3, 2019

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

Copy link
Member Author

@eileencodes eileencodes Jun 3, 2019

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

@morgoth morgoth Jun 3, 2019

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

@morgoth morgoth Jun 3, 2019

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

@morgoth morgoth Jun 3, 2019

isn't there replica: true missing?

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

@morgoth morgoth Jun 3, 2019

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

@matthewd matthewd Jun 4, 2019

"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

@eileencodes eileencodes Jun 4, 2019

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
2 checks passed
@eileencodes eileencodes deleted the multi-db-docs branch Jun 4, 2019
eileencodes added a commit that referenced this issue 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

@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
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
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants