Module: SQLOrigin

Defined in:
lib/sql_origin.rb,
lib/sql_origin/hooks.rb

Overview

Container module for all SQL:Origin methods.

Defined Under Namespace

Modules: LogHook, LogSubscriber, QueryAppendHook

Constant Summary collapse

LIBRARY_PATHS =

Paths or path prefixes that are filtered from the backtrace.

%w(
  vendor
)

Class Method Summary collapse

Class Method Details

.append_to_logObject

Enables SQL:Origin backtrace logging to the Rails log.



23
24
25
26
27
28
29
30
31
# File 'lib/sql_origin.rb', line 23

def self.append_to_log
  %w( PostgreSQLAdapter MysqlAdapter Mysql2Adapter OracleAdapter SQLiteAdapter SQLite3Adapter ).each do |name|
    adapter = ActiveRecord::ConnectionAdapters.const_get(name.to_sym) rescue nil
    if adapter
      adapter.send :include, SQLOrigin::LogHook
    end
  end
  ActiveRecord::LogSubscriber.send :include, SQLOrigin::LogSubscriber
end

.append_to_queryObject

Enables SQL:Origin backtrace logging to SQL query comments.



35
36
37
38
39
40
41
42
# File 'lib/sql_origin.rb', line 35

def self.append_to_query
  %w( PostgreSQLAdapter MysqlAdapter Mysql2Adapter OracleAdapter SQLiteAdapter ).each do |name|
    adapter = ActiveRecord::ConnectionAdapters.const_get(name.to_sym) rescue nil
    if adapter
      adapter.send :include, SQLOrigin::QueryAppendHook
    end
  end
end

.filtered_backtraceArray<String>

Returns The backtrace less library paths.

Returns:

  • (Array<String>)

    The backtrace less library paths.



11
12
13
14
15
16
17
18
19
# File 'lib/sql_origin.rb', line 11

def self.filtered_backtrace
  caller.map do |line|
    line.sub /^#{Regexp.escape Rails.root.to_s}\//, ''
  end.select do |line|
    !line.starts_with?("/") &&
        !line.starts_with?("(") &&
        LIBRARY_PATHS.none? { |path| line.starts_with?(path) }
  end
end