Module: SqlSafetyNet

Defined in:
lib/sql_safety_net.rb,
lib/sql_safety_net/version.rb,
lib/sql_safety_net/formatter.rb,
lib/sql_safety_net/middleware.rb,
lib/sql_safety_net/query_info.rb,
lib/sql_safety_net/cache_store.rb,
lib/sql_safety_net/explain_plan.rb,
lib/sql_safety_net/configuration.rb,
lib/sql_safety_net/query_analysis.rb,
lib/sql_safety_net/connection_adapter.rb,
lib/sql_safety_net/explain_plan/mysql.rb,
lib/sql_safety_net/explain_plan/postgresql.rb

Overview

Root module for the gem.

This module provide access to the singleton configuration object as well as hooks for enabling features.

Since the analysis code is intended only for development mode it is not enabled by default. You can enable it by calling the enable methods in an config/initializers file:

if Rails.env.development?
  SqlSafetyNet.enable_on_cache_store!(ActiveSupport::Cache::Store)
  SqlSafetyNet.enable_on_connection_adapter!(ActiveRecord::Base.connection.class)
  SqlSafetyNet::ExplainPlan.enable_on_connection_adapter!(ActiveRecord::Base.connection.class, :mysql) # assuming MySQL adapter
  Rails.configuration.middleware.use(SqlSafetyNet::Middleware)
end

Or you can simply enable the default configuration by calling:

SqlSafetyNet.enable! if Rails.env.development?

Defined Under Namespace

Modules: CacheStore, ConnectionAdapter, ExplainPlan Classes: Configuration, Formatter, Middleware, QueryAnalysis, QueryInfo

Constant Summary collapse

VERSION =
'2.0.2'.freeze

Class Method Summary collapse

Class Method Details

.configObject

Get the configuration. There is only ever one configuration. The values in the configuration can be changed. If you only need to change them temporarily, see override_config.



60
61
62
# File 'lib/sql_safety_net.rb', line 60

def config
  @config ||= Configuration.new
end

.enable!Object

Enable SQL analysis on your Rails app. This method can be called from your development.rb file. It will enable analysis on the default database connection class and insert middleware into the Rack stack that will add debugging information to responses.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sql_safety_net.rb', line 34

def enable!
  enable_on_cache_store!(ActiveSupport::Cache::Store)
  connection_class = ActiveRecord::Base.connection.class
  enable_on_connection_adapter!(connection_class)
  if connection_class.name.match(/mysql/i)
    ExplainPlan.enable_on_connection_adapter!(connection_class, :mysql)
  elsif connection_class.name.match(/postgres/i)
    ExplainPlan.enable_on_connection_adapter!(connection_class, :postgresql)
  end
  Rails.configuration.middleware.use(Middleware)
end

.enable_on_cache_store!(cache_store_class) ⇒ Object

Enable monitoring on fetches from an ActiveSupport::Cache::Store class. This will allow reporting which queries are in cache blocks in the analysis.



53
54
55
# File 'lib/sql_safety_net.rb', line 53

def enable_on_cache_store!(cache_store_class)
  cache_store_class.send(:include, CacheStore) unless cache_store_class.include?(CacheStore)
end

.enable_on_connection_adapter!(connection_adapter_class) ⇒ Object

Enable SQL analysis on a connection adapter.



47
48
49
# File 'lib/sql_safety_net.rb', line 47

def enable_on_connection_adapter!(connection_adapter_class)
  connection_adapter_class.send(:include, ConnectionAdapter) unless connection_adapter_class.include?(ConnectionAdapter)
end

.override_configObject

Set configuration values within a block. The block given to this method will be yielded to with a clone of the configuration. Any changes to the configuration will only persist within the block.



67
68
69
70
71
72
73
74
75
76
# File 'lib/sql_safety_net.rb', line 67

def override_config
  save_val = config
  begin
    @config = save_val.dup
    @config.style = @config.style.dup
    yield(@config)
  ensure
    @config = save_val
  end
end