Forum: Ruby on Rails Execute an SQL statement each time DB connection is set up

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb5ac46e606a18c60e08dee578294e1a?d=identicon&s=25 Alex Fortuna (dadooda)
on 2009-02-19 04:15
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2009-02-19 15:20
(Received via mailing list)
Alex Fortuna wrote:

> 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
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2009-02-19 15:33
(Received via mailing list)
On Wed, Feb 18, 2009 at 7:15 PM, Alex Fortuna <
rails-mailing-list@andreas-s.net> wrote:

>
>
> 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
Bb5ac46e606a18c60e08dee578294e1a?d=identicon&s=25 Alex Fortuna (dadooda)
on 2009-02-19 19:23
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
This topic is locked and can not be replied to.