5

I am trying to do a simple health check on my postgres database for elixir.

In rails I would do something like: ActiveRecord::Base.verify_active_connections

Is there anything similar on elixir?

Gary
  • 443
  • 9
  • 22
  • I think perhaps you're misunderstanding the Elixir idiom. If you couldn't reach the DB for some reason you want the code to crash--big and immediate so you know you need to check the DB. If you don't want the code to crash then build a supervisor and have it handle a DB failure. Either way, it's sort of not the Elixir way (or really the Erlang way either) to run a "simple health check" on a database. – Onorio Catenacci Nov 05 '15 at 19:52
  • Got it, thanks for the tip – Gary Nov 05 '15 at 22:39

1 Answers1

5

This is the best that I found.

status = try do
  Ecto.Adapters.SQL.query(ProjectName.Repo, "select 1", [])
  :ok
rescue
  DBConnection.ConnectionError -> :error
end

It just sends a select 1 request to the sql server and if we get an exit we return :error else we return :ok

denis.peplin
  • 9,585
  • 3
  • 48
  • 55
Gary
  • 443
  • 9
  • 22