Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - Test whether database connection is possible [closed]

I am looking to implement a way to test the database connection for our application.

We are currently having connectivity issues and therefore want to be able to check connectivity without having to login.

Is there a way to display a page stating database connection is up or database connection is down dependent upon whether connection fails. If any additional information is needed please let me know.

Only beginning to learn the ropes so apologies if I'm short on detail.

like image 217
user3385136 Avatar asked May 11 '15 14:05

user3385136


1 Answers

You can check if the connection is possible by running the following script:

require './config/environment.rb' # Assuming the script is located in the root of the rails app
begin
  ActiveRecord::Base.establish_connection # Establishes connection
  ActiveRecord::Base.connection # Calls connection object
  puts "CONNECTED!" if ActiveRecord::Base.connected? 
  puts "NOT CONNECTED!" unless ActiveRecord::Base.connected?
rescue
  puts "NOT CONNECTED!"
end
like image 109
davidb Avatar answered Sep 30 '22 06:09

davidb