A callback for ActiveRecord database connections?

activerecord, database-connection, mysql, ruby-on-rails, ruby-on-rails-4

Solution

The `ConnectionAdapter` defines two callbacks `:checkout` (connect) and `:checkin` (disconnect). You can use it for specific adapter as

ActiveRecord::ConnectionAdapters::MysqlAdapter.set_callback :checkout, :after do
  raw_connection.set_your_variables ...
end

Or you can use `ActiveRecord::Base.connection.class` for whatever adapter is currently declared in `database.yml`

Problem

Is there any way to hook into ActiveRecord connection creation? I want to run some code whenever a connection has just been created. I feel like it might be a way to set a MySQL variable on the connection, since "variables" in database.yml doesn't seem to work for me. (How to turn off MySQL strict mode in Rails)

Original source

Related problems