Alex Fortuna
2009-Feb-19 03:15 UTC
Execute an SQL statement each time DB connection is set up
Hi everyone. I need to execute a "SET SQL_MODE=''STRICT_ALL_TABLES''" after my application''s DB connection is established by AcriveRecord. This is important since I want the DB itself be more strict about logical data integrity. Which is the proper place within application code from where I can issue this query? Tried to make an ActiveRecord::Base.connection.execute() in an initializer, but it only runs once after server startup. At next request the connection is re-created, but initializers aren''t invoked. Alex -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2009-Feb-19 14:19 UTC
Re: Execute an SQL statement each time DB connection is set up
Alex Fortuna wrote:> I need to execute a "SET SQL_MODE=''STRICT_ALL_TABLES''" after my > application''s DB connection is established by AcriveRecord. This is > important since I want the DB itself be more strict about logical data > integrity. > > Which is the proper place within application code from where I can issue > this query? > > > Tried to make an ActiveRecord::Base.connection.execute() in an > initializer, but it only runs once after server startup. At next request > the connection is re-created, but initializers aren''t invoked.In 2.2.2 there doesn''t seem to be a callback when a connection is established & re-established, so try an initializer like ActiveRecord::ConnectionAdapters::MysqlAdaptor.class_eval do private def connect_with_strict connect_without_strict execute "SET SQL_MODE=''STRICT_ALL_TABLES''" end alias_method_chain :connect, :strict end -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Taylor
2009-Feb-19 14:32 UTC
Re: Execute an SQL statement each time DB connection is set up
On Wed, Feb 18, 2009 at 7:15 PM, Alex Fortuna < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi everyone. > > I need to execute a "SET SQL_MODE=''STRICT_ALL_TABLES''" after my > application''s DB connection is established by AcriveRecord. This is > important since I want the DB itself be more strict about logical data > integrity. > > Which is the proper place within application code from where I can issue > this query? > > > Tried to make an ActiveRecord::Base.connection.execute() in an > initializer, but it only runs once after server startup. At next request > the connection is re-created, but initializers aren''t invoked. > >Hi Alex, STRICT_ALL_TABLES is an option that one would set on a database. Thus, why don''t you set it when you create the database? -Conrad --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alex Fortuna
2009-Feb-19 18:23 UTC
Re: Execute an SQL statement each time DB connection is set up
Conrad Taylor wrote:> Hi Alex, STRICT_ALL_TABLES is an option that one would set on a > database. > Thus, why don''t you set it when you create the database? >Conrad, AFAIK, SQL_MODE applies to _connection_, not DB itself. I.e. it''s not the same as, for example, DB engine or collation mode. Consider (from ''script/server'' log):> SQL (0.2ms) SET NAMES ''utf8''Well, what I need is almost similar. What Mark proposed above seems closer to what I need. It has never been a problem in less sophisticated environments. Just issue a bunch of "SET *" command after every connection and feel good. Alex -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---