Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

shivrajbadu
I followed some tutorials on new release Rails 6.0.0.beta3 and trying to implement connection switching multiple database this way:

database.yml
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: root
socket: /var/run/mysqld/mysqld.sock

development:
  main:
    <<: *default
    database: r6_multidb_development

  main_replica:
    <<: *default
    database: r6_multidb_development_copy
    replica: true

Article Model
class Article < ApplicationRecord
  connect_to database: { writing: :main, reading: :main_replica }
end

Both the databases r6_multidb_development and r6_multidb_development_copy have different records, i was expecting when record is created records should inserted on r6_multidb_development Database and when retrieving record it should from r6_multidb_development_copy database. But in both cases when record is inserted and retrieved it is happening from main configuration that is r6_multidb_development database.

I believe write to the database should happen from r6_multidb_development and read should happen from r6_multidb_development_copy. I would appreciate if anybody figure out this issue.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

Eileen M. Uchitelle
Hey!

I've implemented multiple databases in Rails 6, but I didn't implement multiple writers. Rails doesn't know what your replica configuration is going to be like so for purposes of local development you should use the same database name for your primary and replica, and then have different user names. In production you should also use the same name (they are the same data you're accessing) with different usernames (one set to readonly) but they shouldn't be in the same place.

Hope that helps,
Eileen

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

Eileen M. Uchitelle
Ok after reading this over a few times I've realized that you wrote broken code on purpose to prove the switching was working.

Did you turn on the middleware that will automatically swap based on HTTP verb? If you didn't your POST/GET requests won't switch connections automatically.

If you're in a model/controller/test you need to wrap it in a `connected_to` block like this

```
ActiveRecord::Base.connected_to(role: :reading) do
  # read from your replica
end
```

Rails can't know if your replica is up to date so we didn't implement always select from the replica, you have to tell it you want that.

On Wednesday, April 24, 2019 at 8:04:45 AM UTC-4, eileencodes wrote:
Hey!

I've implemented multiple databases in Rails 6, but I didn't implement multiple writers. Rails doesn't know what your replica configuration is going to be like so for purposes of local development you should use the same database name for your primary and replica, and then have different user names. In production you should also use the same name (they are the same data you're accessing) with different usernames (one set to readonly) but they shouldn't be in the same place.

Hope that helps,
Eileen

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

shivrajbadu
I guess middleware should turn on when we write connect_to blog on the model in order to switch read and write access, isn't that?
```
class Article < ApplicationRecord
  connect_to database: { writing: :main, reading: :main_replica }
end
```
There is an Articles controller with defined CRUD with Get and Post HTTP verb, here are similar code that I have in articles_controller:
```
def index
   @articles = Article.all
end

def create
    @article = Article.new(article_params)
    @article.save
    redirect_to @article
end

``` 

On Wednesday, April 24, 2019 at 6:00:07 PM UTC+5:45, eileencodes wrote:
Ok after reading this over a few times I've realized that you wrote broken code on purpose to prove the switching was working.

Did you turn on the middleware that will automatically swap based on HTTP verb? If you didn't your POST/GET requests won't switch connections automatically.

If you're in a model/controller/test you need to wrap it in a `connected_to` block like this

```
ActiveRecord::Base.connected_to(role: :reading) do
  # read from your replica
end
```

Rails can't know if your replica is up to date so we didn't implement always select from the replica, you have to tell it you want that.

On Wednesday, April 24, 2019 at 8:04:45 AM UTC-4, eileencodes wrote:
Hey!

I've implemented multiple databases in Rails 6, but I didn't implement multiple writers. Rails doesn't know what your replica configuration is going to be like so for purposes of local development you should use the same database name for your primary and replica, and then have different user names. In production you should also use the same name (they are the same data you're accessing) with different usernames (one set to readonly) but they shouldn't be in the same place.

Hope that helps,
Eileen

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

shivrajbadu
In reply to this post by Eileen M. Uchitelle

Hi Eileen, yes this way I had already tested and it works, but by keeping different usernames for same database doesn't mean there are two database. But I would like to keep two database one is for Post/Put and other database just for GET and reading large queries no matter both the database are same just name is changed, is this possible with connects_to() ? 

On Wednesday, April 24, 2019 at 5:49:45 PM UTC+5:45, eileencodes wrote:
Hey!

I've implemented multiple databases in Rails 6, but I didn't implement multiple writers. Rails doesn't know what your replica configuration is going to be like so for purposes of local development you should use the same database name for your primary and replica, and then have different user names. In production you should also use the same name (they are the same data you're accessing) with different usernames (one set to readonly) but they shouldn't be in the same place.

Hope that helps,
Eileen

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

shivrajbadu
In reply to this post by Eileen M. Uchitelle

Also I have added connected_to() inside action but I did not get the desired result from r6_multidb_development_copy, may be I am missing something.

def index
   
ActiveRecord::Base.connected_to(role: :reading) do
     
@articles = Article.all
   
end
end



On Wednesday, April 24, 2019 at 6:00:07 PM UTC+5:45, eileencodes wrote:
Ok after reading this over a few times I've realized that you wrote broken code on purpose to prove the switching was working.

Did you turn on the middleware that will automatically swap based on HTTP verb? If you didn't your POST/GET requests won't switch connections automatically.

If you're in a model/controller/test you need to wrap it in a `connected_to` block like this

```
ActiveRecord::Base.connected_to(role: :reading) do
  # read from your replica
end
```

Rails can't know if your replica is up to date so we didn't implement always select from the replica, you have to tell it you want that.

On Wednesday, April 24, 2019 at 8:04:45 AM UTC-4, eileencodes wrote:
Hey!

I've implemented multiple databases in Rails 6, but I didn't implement multiple writers. Rails doesn't know what your replica configuration is going to be like so for purposes of local development you should use the same database name for your primary and replica, and then have different user names. In production you should also use the same name (they are the same data you're accessing) with different usernames (one set to readonly) but they shouldn't be in the same place.

Hope that helps,
Eileen

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

Eileen M. Uchitelle
In reply to this post by shivrajbadu
The middleware needs to be configured in your environment so that you can set the replica delay. See the docs here https://github.com/rails/rails/blob/713cee01a5391b1ca56e25883c6c172ae59d7020/activerecord/lib/active_record/middleware/database_selector.rb#L33-L36. This behavior s by design, and it should not turn on when you call connect_to.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

shivrajbadu
Thank you Eileen, it is working as expected and i understand now.

On Wednesday, April 24, 2019 at 6:46:40 PM UTC+5:45, eileencodes wrote:
The middleware needs to be configured in your environment so that you can set the replica delay. See the docs here <a href="https://github.com/rails/rails/blob/713cee01a5391b1ca56e25883c6c172ae59d7020/activerecord/lib/active_record/middleware/database_selector.rb#L33-L36" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fblob%2F713cee01a5391b1ca56e25883c6c172ae59d7020%2Factiverecord%2Flib%2Factive_record%2Fmiddleware%2Fdatabase_selector.rb%23L33-L36\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJ7cA-9sROOMRifc2AatpKwo_2sg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fblob%2F713cee01a5391b1ca56e25883c6c172ae59d7020%2Factiverecord%2Flib%2Factive_record%2Fmiddleware%2Fdatabase_selector.rb%23L33-L36\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJ7cA-9sROOMRifc2AatpKwo_2sg&#39;;return true;">https://github.com/rails/rails/blob/713cee01a5391b1ca56e25883c6c172ae59d7020/activerecord/lib/active_record/middleware/database_selector.rb#L33-L36. This behavior s by design, and it should not turn on when you call connect_to.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

Eileen M. Uchitelle
I'm curious what tutorial you followed, I'm wondering if it's incorrect and we need to fix something. I also need to write docs in Rails itself before the 6 final is released.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Connection switching to support multiple databases is not working in Rails 6.0.0.beta3 version

shivrajbadu
Here is the tutorial i followed: http://bit.ly/2vm74Nx

Please also let me know if any changes i need to make in my code or if have any suggestions, thank you.

On Thursday, April 25, 2019 at 5:40:05 PM UTC+5:45, eileencodes wrote:
I'm curious what tutorial you followed, I'm wondering if it's incorrect and we need to fix something. I also need to write docs in Rails itself before the 6 final is released.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.