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)
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
Also, if you need to configure your model after connection has been made and column information was retrieved, you can redefine load_schema!
class method in model.
See: https://github.com/rails/rails/pull/31681#issuecomment-357113030
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With