LoginSignup
7
5

More than 5 years have passed since last update.

ARProxy でクエリの実行箇所を SQL コメントとして仕込むやつ

Last updated at Posted at 2015-05-09

こんなかんじでイケるというメモ

config/initializers/arproxy.rb
RAILS_ROOT = Rails.root.to_s
class QueryTracer < Arproxy::Base
  def execute(sql, name=nil)
    line = caller.find {|line| line.start_with?(RAILS_ROOT) and not line.start_with?(__FILE__) }
    sql << " /* #{line} */"
    super(sql, name)
  end
end

Arproxy.configure do |config|
  config.adapter = "mysql2" # A DB Apdapter name which is used in your database.yml
  config.use QueryTracer
end
Arproxy.enable!

ログにこんな感じで出る

[DEBUG] SELECT `filter_sets`.id FROM `filter_sets` WHERE `filter_sets`.`project_id` = 525 /* /home/sonots/myapp/app/models/report.rb:79:in `remove_unavailable_filter_set'

遅そうなので development でしか有効化してない。

EDIT: slow なクエリにだけ仕込むようにすれば特に問題にならなそう

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5