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

Support AR::Relation (add AR::Relation#using_readonly/using_writable) #15

Closed
wants to merge 8 commits into from

Conversation

winebarrel
Copy link

@winebarrel winebarrel commented Aug 6, 2017

The connection used depends on the timing of relation evaluation.

SwitchPoint.readonly_all!
Article.with_writable { p Article.all }  # Read from db-blog-master
p Article.with_writable { Article.all }  # Read from db-blog-slave

I extend the AR::Relation to add the following method:

Article.using_readonly.first # Read from db-blog-slave
Article.where(id: 1).using_writable.update_all(title: 'new title') # Write to db-blog-master

Please review 🙏

@winebarrel winebarrel force-pushed the add_ar_relation_method branch 2 times, most recently from 4d4af0a to f92a27d Compare August 6, 2017 05:41
@winebarrel winebarrel changed the title Support AR::Relation (add AR::Relation#with_readonly/with_writable) Support AR::Relation (add AR::Relation#using_readonly/using_writable) Aug 6, 2017
@eagletmt
Copy link
Owner

eagletmt commented Aug 6, 2017

I tried this branch and found a strange behavior.

[5] pry(#<RSpec::ExampleGroups::SwitchPointRelation::WithReadonly::UsingTake>)> Book.all.to_a
=> [#<Book:0x00000000049eeef0 id: 99>]  # OK. It connects to readonly DB.
[6] pry(#<RSpec::ExampleGroups::SwitchPointRelation::WithReadonly::UsingTake>)> Book.using_writable.to_a
=> [#<Book:0x0000000004999d10 id: 1>]   # OK. It connects to writable DB.
[7] pry(#<RSpec::ExampleGroups::SwitchPointRelation::WithReadonly::UsingTake>)> rel = Book.using_writable; rel.using_readonly.to_a
=> [#<Book:0x0000000004948a78 id: 99>]  # OK. It connects to readonly DB.
[8] pry(#<RSpec::ExampleGroups::SwitchPointRelation::WithReadonly::UsingTake>)> rel = Book.using_writable; rel.to_a; rel.using_readonly.to_a
=> [#<Book:0x0000000004911e60 id: 1>]   # NG. Why does it connect to writable DB?

Is the last behavior intentional?

@winebarrel
Copy link
Author

Is the last behavior intentional?

Sorry, it is a bug.
Fixed 02335b4

README.md Outdated
Article.where(id: 1).using_writable.update_all(title: 'new title') # Write to db-blog-master
```

Note that `AR::Relation` extension support is Rails version 4 or higher.
Copy link
Owner

Choose a reason for hiding this comment

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

"extension is only supported in Rails 4.0 or higher"

Copy link
Author

@winebarrel winebarrel Aug 22, 2017

Choose a reason for hiding this comment

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

Thanks! fixed 22b0a39

module Relation
attr_writer :proc_to_give_switch_point_mode

def exec_queries(*args, &block)
Copy link
Owner

Choose a reason for hiding this comment

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

def exec_queries(*)

Copy link
Author

Choose a reason for hiding this comment

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

Thanks! fixed 1afdd17

@winebarrel winebarrel closed this Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants