Class: Shiba::ActiveRecordIntegration

Inherits:
Object
  • Object
show all
Defined in:
lib/shiba/activerecord_integration.rb

Overview

Integrates ActiveRecord with the Query Watcher by setting up the query log path, and the connection options for the explain command, which it runs when the process exits.

SHIBA_DIR, SHIBA_QUERY_LOG_NAME and SHIBA_DEBUG=true environment variables may be set.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/shiba/activerecord_integration.rb', line 13

def path
  @path
end

#watcherObject (readonly)

Returns the value of attribute watcher.



13
14
15
# File 'lib/shiba/activerecord_integration.rb', line 13

def watcher
  @watcher
end

Class Method Details

.connection_optionsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shiba/activerecord_integration.rb', line 29

def self.connection_options
    cx = ActiveRecord::Base.connection.raw_connection
    if cx.respond_to?(:query_options)
      # mysql
      c = cx.query_options.merge(server: 'mysql')
    else
      # postgres
      c = { host: cx.host, database: cx.db, username: cx.user, password: cx.pass, port: cx.port, server: 'postgres' }
    end

    {
      'host' =>     c[:host],
      'database' => c[:database],
      'username' => c[:username],
      'password' => c[:password],
      'port' =>     c[:port],
      'server' =>   c[:server],
    }
end

.install!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/shiba/activerecord_integration.rb', line 15

def self.install!
  return false if @installed
  if defined?(Rails.env) && Rails.env.production?
    Rails.logger.error("Shiba watcher is not intended to run in production, stopping install.")
    return false
  end

  ActiveSupport.on_load(:active_record) do
    Shiba::ActiveRecordIntegration.start_watcher
  end

  @installed = true
end