Class: PgMonitor::DBConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_monitor/db_connection.rb

Class Method Summary collapse

Class Method Details

.active_record_present?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/pg_monitor/db_connection.rb', line 13

def self.active_record_present?
  defined?(ActiveRecord::Base)
end

.clear_connection!Object



27
28
29
# File 'lib/pg_monitor/db_connection.rb', line 27

def self.clear_connection!
  @connection = nil
end

.connectionObject



5
6
7
8
9
10
11
# File 'lib/pg_monitor/db_connection.rb', line 5

def self.connection
  if active_record_present?
    ActiveRecord::Base.connection.raw_connection
  else
    @connection ||= PG.connect(connection_params)
  end
end

.connection_paramsObject



17
18
19
20
21
22
23
24
25
# File 'lib/pg_monitor/db_connection.rb', line 17

def self.connection_params
  {
    dbname: ENV.fetch("PG_DATABASE"),
    user: ENV.fetch("PG_USER"),
    password: ENV.fetch("PG_PASSWORD"),
    host: ENV.fetch("PG_HOST"),
    port: ENV.fetch("PG_PORT")
  }
end