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?
simply tells you that a connection has already been opened. It may not necessarily still be active. To check whether the connection is currently active, call ActiveRecord::Base.
ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.
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
The answer is SomeModel.connection_config
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