# Execute an SQL statement each time DB connection is set up

**URL:** <https://www.ruby-forum.com/t/execute-an-sql-statement-each-time-db-connection-is-set-up/160718>\
**Category:** Rails\
**Created:** [February 19, 2009, 4:15am UTC](https://www.ruby-forum.com/t/execute-an-sql-statement-each-time-db-connection-is-set-up/160718 "2009-02-19T04:15:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [February 19, 2009, 4:15am UTC](https://www.ruby-forum.com/t/execute-an-sql-statement-each-time-db-connection-is-set-up/160718/1 "2009-02-19T04:15:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [February 19, 2009, 3:20pm UTC](https://www.ruby-forum.com/t/execute-an-sql-statement-each-time-db-connection-is-set-up/160718/2 "2009-02-19T15:20:58Z")

</div>

Alex F.una 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](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [February 19, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/execute-an-sql-statement-each-time-db-connection-is-set-up/160718/3 "2009-02-19T15:33:26Z")

</div>

On Wed, Feb 18, 2009 at 7:15 PM, Alex F.una \<  
removed\_email\_address@domain.invalid\> 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

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [February 19, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/execute-an-sql-statement-each-time-db-connection-is-set-up/160718/4 "2009-02-19T19:23:26Z")

</div>

Conrad T. 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
