Hi, I''ve got a simple problem. I''ve got a collection of models, Customer, Product and Shop, and they''re all using the database configured for the ActiveRecord class. However, for one particular task -- generating a report -- I want to be able to set those models to use a database on a different server. The secondary database is simply a copy of the first, so the schemas and data is identical to the main database, but I want to use this secondary server so that the load of producing reports doesn''t affect the main database server. I''ve tried the following, but it doesn''t seem to have any effect: def build_report: Customer.establish_connection( :adapter => "mysql", :host => "hostname", :username => "user", :password => "password", :database => "schema" ) @customer = Customer.find(@criteria[:customer_id]) ..... <report logic here> end Is it possible to achieve what I want to do this way, or do I need to look along other lines? Many thanks, Michaël -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michaël wrote:> Hi, > > I''ve got a simple problem. I''ve got a collection of models, Customer, > Product and Shop, and they''re all using the database configured for > the ActiveRecord class. However, for one particular task -- generating > a report -- I want to be able to set those models to use a database on > a different server. The secondary database is simply a copy of the > first, so the schemas and data is identical to the main database, but > I want to use this secondary server so that the load of producing > reports doesn''t affect the main database server.I think you would be better advised to use your DB server''s clustering/load-balancing features for this. It should not be your application''s concern. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 19, 6:08 pm, Michaël <michael.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > I''ve tried the following, but it doesn''t seem to have any effect: > > def build_report: > > Customer.establish_connection( > :adapter => "mysql", > :host => "hostname", > :username => "user", > :password => "password", > :database => "schema" > ) > > @customer = Customer.find(@criteria[:customer_id]) > > ..... <report logic here> > > end > > Is it possible to achieve what I want to do this way, or do I need to > look along other lines? >this would only affect queries done against the customers table. queries to any other tables would still go to the ''normal'' database. Is that what''s happening? Fred> Many thanks, > Michaël-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 July, 19:32, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 19, 6:08 pm, Michaël <michael.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hi, > > I''ve tried the following, but it doesn''t seem to have any effect: > > > def build_report: > > > Customer.establish_connection( > > :adapter => "mysql", > > :host => "hostname", > > :username => "user", > > :password => "password", > > :database => "schema" > > ) > > > @customer = Customer.find(@criteria[:customer_id]) > > > ..... <report logic here> > > > end > > > Is it possible to achieve what I want to do this way, or do I need to > > look along other lines? > > this would only affect queries done against the customers table. > queries to any other tables would still go to the ''normal'' database. > Is that what''s happening? > > FredThat''s actually what I want to achieve, but I''ve done the above and it doesn''t seem to work. I know that reports only use information from the Customers, Products and Shop models, so I only want to change the database connection for these three while generating the report, and then switch it back after its done. As far as load balancing is concerned, my ultimate goal is to push that to the database level and have the database server handle that, but I need to implement something quickly as an intermediate step. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michaël wrote: [...]> As far as load balancing is concerned, my ultimate goal is to push > that to the database level and have the database server handle that, > but I need to implement something quickly as an intermediate step.Then do it on the DB side now. It probably won''t take any more time than all the research you''re already doing to do this on the app side, and you won''t have to throw the work away later. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michaël wrote:> I want to be able to set those models to use a database on a > different server. > ... > I''ve tried the following, but it doesn''t seem to have any effect:I do this for switching between a legacy db and a new db. It works fine. But I was doing it a slightly different way and it. I think the simplest way is to define a new database in your config/database.yml file. You should be able to pass in all of the parameters inline too but that tends not to be DRY as you will almost certainly need to refer to it more than once. So having it in one place in the database.yml is nice and only once. Something like this: myotherdb: adapter: mysql username: myusername password: mypassword database: myotherdatabase Then to always use it for a model then refer to it by name using establish_connection. Like this: class User < ActiveRecord::Base establish_connection "myotherdb" ... end Since you only want to change to it sometimes dynamically depending upon what you are doing then you can call establish_connection inline and switch over from one db to the other. Since you are manually explicitly changing db''s out from under activerecord then don''t forget to switch back to the standard one afterward. Not doing that was a source of bugs in my own projects. The symptom was that sometimes, depending upon the program flow, it would end up talking to the wrong database. def build_report ActiveRecord::Base.establish_connection("myotherdb") if ! ENV[''RAILS_ENV''] == ''test'' ... do your other db stuff here ... ActiveRecord::Base.establish_connection(ENV["RAILS_ENV"]) if ! ENV[''RAILS_ENV''] == ''test'' end Note the use of ENV["RAILS_ENV"] to select the right database when returning. That assumes that you are using the standard "production" and "development" names. Note the avoidance of this switch when running tests. Two gotchas that I tripped over when first setting this up. Also, an unrelated side remark, ruby isn''t python and you don''t need to end lines with colons, as you had done in your example. :-) Bob P.S. Sorry if the thread of this message is broken. I had a personal snafu and needed to recreate the message from parts. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 July, 20:30, Bob Proulx <b...-5cAygf9QrE/QT0dZR+AlfA@public.gmane.org> wrote:> Micha l wrote: > > I want to be able to set those models to use a database on a > > different server. > > ... > > I''ve tried the following, but it doesn''t seem to have any effect: > > I do this for switching between a legacy db and a new db. It works > fine. But I was doing it a slightly different way and it. > > I think the simplest way is to define a new database in your > config/database.yml file. You should be able to pass in all of the > parameters inline too but that tends not to be DRY as you will almost > certainly need to refer to it more than once. So having it in one > place in the database.yml is nice and only once. Something like this: > > myotherdb: > adapter: mysql > username: myusername > password: mypassword > database: myotherdatabase > > Then to always use it for a model then refer to it by name using > establish_connection. Like this: > > class User < ActiveRecord::Base > establish_connection "myotherdb" > ... > end > > Since you only want to change to it sometimes dynamically depending > upon what you are doing then you can call establish_connection inline > and switch over from one db to the other. Since you are manually > explicitly changing db''s out from under activerecord then don''t forget > to switch back to the standard one afterward. Not doing that was a > source of bugs in my own projects. The symptom was that sometimes, > depending upon the program flow, it would end up talking to the wrong > database. > > def build_report > ActiveRecord::Base.establish_connection("myotherdb") if ! ENV[''RAILS_ENV''] == ''test'' > ... do your other db stuff here ... > ActiveRecord::Base.establish_connection(ENV["RAILS_ENV"]) if ! ENV[''RAILS_ENV''] == ''test'' > end > > Note the use of ENV["RAILS_ENV"] to select the right database when > returning. That assumes that you are using the standard "production" > and "development" names. Note the avoidance of this switch when > running tests. Two gotchas that I tripped over when first setting > this up.Thanks for the clarification. This is exactly what I''ve done, and it''s working now. I was struggling because there was a rogue backgroundrb task running for reports generation that still had the primary server configured for its database connection.> > Also, an unrelated side remark, ruby isn''t python and you don''t need > to end lines with colons, as you had done in your example. :-) >I just noticed that :) It''s not in my code fortunately. That''s what comes of trying to manage too many projects in different languages. Thanks for all the help, guys. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.