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?
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.
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