Change or disable the default call to ActiveRecord::Base.establish_connection

Is there any way to change or prevent the default call to ActiveRecord::Base.establish_connection?

I was expecting to find it in config/application.rb or an env specific configuration file, but I can’t find it.

What is the proper way to customize the behavior?

I need something similar to this:

if ENV['PROCNAME'] == 'worker-without-db'
  # do not connect to db
  ActiveRecord::Base.establish_connection = "my database"

In this way I could prevent the call to establish_connection for a large number of sidekiq processes that never need DB access (in order to avoid hundreds of useless DB connections).

Active Record shouldn’t be connecting to the database until something happens that requires the connection.

If your processes are consuming connections, perhaps you can track down where it’s being used?

I think that you are correct.

However it would be extremely important for us to have a way to prevent that (by accident)…

Basically most of our background workers only need access to Redis and I was looking for a way to prevent accidental connections to the db. It was interesting if there was an option inside ActiveRecord… Something like .freeze (as a metaphor), but that says to AR to disable all the db connections.

First thing that came to my mind is to set DATABASE_URL environment variable to something dummy that would throw an error on connection attempt.

You could also instead require "rails/all" in config/application.rb require only railties that you actually need (not require active record for worker) but that will mean that you should skip all your models also …