1

I'd like my Rails app to run a raw sql command after it establishes the connection to the DB. In which file does that belong? One of the config/initializers?

pca2
  • 165
  • 2
  • 8
  • Curious, what are you trying to do? A little more context would definitely be helpful! – vee Mar 24 '15 at 03:11
  • Run once? Run every time it starts up? – Beartech Mar 24 '15 at 03:17
  • I'd just like it to run each time a connection is established with the DB. Specifically I'm trying to connect to a Vertica DB, which I do have a driver for, however by default Vertica has autocommit off and I need to enable it before attempting any insert/delete statements – pca2 Mar 24 '15 at 03:58
  • How often does a connection happen? Remember Vertica is not an OLTP platform. – Kermit Mar 24 '15 at 14:43

2 Answers2

1

I use monkeypatching to force strict mode for MySQL, the same approach should also work in your case. This code belongs in an initializer.

class ActiveRecord::ConnectionAdapters::Mysql2Adapter

private
  alias_method :configure_connection_without_autocommit, :configure_connection

  def configure_connection
    configure_connection_without_autocommit
    execute "COMMAND_TO_ENABLE_AUTOCOMMIT"
  end
end

For reference, here's the source code for Mysql2Adapter.

Leonid Shevtsov
  • 14,024
  • 9
  • 51
  • 82
0

I think You can write a rake filter, through which you can fire a query before each inbound call. You can read more about this here.

Community
  • 1
  • 1
Saurabh
  • 71,488
  • 40
  • 181
  • 244