Bradley
2011-Sep-13 15:29 UTC
Changing adapters in ActiveRecord does not change the generated sql
If I establish a new connection with a new adapter (ie. switch from mysql to postgresl) the generated sql is still mysql specific. I tried all sorts of reset methods on AR::Base but to no avail. There must be something being memoized on ActiveRecord base that has to do with the adapter and it doesn''t get cleared when you establish a connection with a new adapter. I know this isn''t standard behaviour, in fact the only reason I need this is because I''m writing a multi-tenancy gem<https://github.com/bradrobertson/apartment>for rails that supports many different database adapters. My tests run through these adapters, but the 2nd adapter to be tested always fails with invalid sql. Here''s an example: ActiveRecord::Base.establish_connection :adapter => ''mysql2'', :database => ''my_test_db'', :username => ''root'', :password => '''' User.scoped.to_sql #=> "SELECT `users`.* FROM `users`" ActiveRecord::Base.establish_connection :adapter => ''postgresql'', :database => ''my_test_db'', :username => ''root'', :password => '''' User.scoped.to_sql #=> "SELECT `users`.* FROM `users`" These back ticks on column names are mysql specific and are invalid for postgresql, so it throws an error. Does anyone know how to fully clear out whatever it is that is caching this sql being generated?? I searched through the source and was able to unset @quoted_table_name on the user model, this regenerated the table name properly, however the query now looks like: User.scoped.to_sql #=> "SELECT \"users\".* FROM `users`" Still not perfect. And I also don''t want to have to go through each model and unset a bunch of vars. Any simpler way to reload these? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/F2lliz4HkEEJ. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Sep-13 16:42 UTC
Re: Changing adapters in ActiveRecord does not change the generated sql
On Sep 13, 4:29 pm, Bradley <bradleyrobert...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Does anyone know how to fully clear out whatever it is that is caching this > sql being generated?? > > I searched through the source and was able to unset @quoted_table_name on > the user model, this regenerated the table name properly, however the query > now looks like: > > User.scoped.to_sql #=> "SELECT \"users\".* FROM `users`" > > Still not perfect. And I also don''t want to have to go through each model > and unset a bunch of vars. Any simpler way to reload these?You could try calling reset_column_information (although it looks like this doesn''t clear out quoted table name) Fred -- 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.
Bradley
2011-Sep-14 13:18 UTC
Re: Changing adapters in ActiveRecord does not change the generated sql
ya that doesn''t appear to help. especially if I''m just selecting * -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/35PzkaJrThgJ. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bradley
2011-Sep-15 17:28 UTC
Re: Changing adapters in ActiveRecord does not change the generated sql
I started looking into the reload!<http://api.rubyonrails.org/classes/Object.html#method-i-reload-21>method as I figured maybe manually reloading the classes in between testing adapters would help. In the dev console doing a reload appears to be similar to resetting the quoted_table_name, I get the same: User.scoped.to_sql #=> "SELECT \"users\".* FROM `users`" So it''s caching that table_name (in the FROM clause) somewhere else (that isn''t reloaded) Does anyone have any insight into this? I''m digging into the source but I''m not having a ton of luck. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/QHbYq5RnCFgJ. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bradley
2011-Sep-24 15:24 UTC
Re: Changing adapters in ActiveRecord does not change the generated sql
I''ve found out something very interesting. If I''m first connected to Postgresql, then switch to Mysql, I can reset both table and column names properly using Model.reset_column_information and Model.reset_table_name This gives me the proper backquoted columns/table names for Mysql. User.scoped.to_sql #=> "SELECT `users`.* FROM `users`" If however, I switch back to Postgresql, and run the same commands, it doesn''t actually reset the column information properly, only the table_name. User.scoped.to_sql #=> "SELECT \"users\".* FROM `users`" I''ve found that it actually doesn''t matter in which order this happens. The first switch to one adapter allows me to reset properly. The second switch however does NOT. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/TI3RzzCOCqQJ. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.