41

I am writing code that will migrate some data from one database to another, over-writing some data in the destination. It uses ActiveRecord, since it's associated with a Rails app using AR already.

Since some data will be over-written, I'd like to provide a confirmation prompt that tells the user the actual connection dictionary/spec used for the destination database connection. you know, adapter, host, username, password, database, the things you'd list in database.yml.

I can take the model for the stuff I'm writing to and ask for SomeModel.connection.... But there seems to be no API at all to get the actual connection spec out of a live connection object.

Really? Am I missing something? Any other ideas, even undocumented api?

4 Answers 4

104

Similar to the way that you can call connection on a model, you can make a call to connection on ActiveRecord::Base.

ActiveRecord::Base.connection_config

Look at the docs for ActiveRecord::Base, as there are other methods that allow you to get/set attributes about the connection.

Update

The connection_config became deprecated and will be removed from Rails 6.2. connection_db_config should be used instead:

ActiveRecord::Base.connection_db_config
4
  • Yes, I know how to do this. It gives me roughly the same type of action back that calling someModel.connection gives me. I'd rather call someModel.connection, because then I know I have the right connection in case the app is using different ones for different models. But in either case, this IS my question, not an answer to it -- I can see no way to get host/port/database/etc out of the object returned by #connection. That IS my question. And I have looked at the docs, yes.
    – jrochkind
    Dec 29, 2011 at 22:07
  • Ha, okay, while you didn't answer my question, you DID lead me to it. I guess I hadn't looked carefully enough at ActiveRecord::Base, I was concentrating on ConnectionAdapter and such. #connection_config is what I want, and you can call it on a specific model, SomeModel.connection_config
    – jrochkind
    Dec 29, 2011 at 22:10
  • 1
    Well I'm glad that you were able to find a solution. When you said: 'But there seems to be no API at all to get the actual connection spec out of a live connection object.', I was interpreted it as you wanting to get at the connection statistics without calling it on a model class. I'll update my answer to reflect the proper solution.
    – Batkins
    Dec 29, 2011 at 22:24
  • 1
    Yep. It's safer to call SpecificModel.connection_config though, as a specific model may or may not have the same connection config as the ActiveRecord::Base superclass. (by default it does, but it's possible for it to be different)
    – jrochkind
    Dec 29, 2011 at 22:38
12

The answer is SomeModel.connection_config

1
  • I prefer this one as it's a bit easy to type. (y)
    – Saim
    Nov 25, 2018 at 7:38
0

For Rails 6 and up, when you configure many databases for one environment, for example

development:
  primary:
    <config for primary goes here>

  replica:
    <config for replica goes here>

Then you can get an array of configurations for the development environment like this:

ActiveRecord::Base.configurations.configurations.select { |x| x.env_name == 'development' }

You will get back an array with two configurations, one is @name="primary" and one is @name="replica".

0

Note: for rails 7 up it will be:

ActiveRecord::Base.connection_db_config

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.