Project

sql_origin

0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Ever wonder where a SQL query comes from? This gem lets you add abbreviated backtraces to those queries, either in the query log, or as a comment in the query itself.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 4.0
 Project Readme

SQL:Origin

Adds backtraces to your SQL queries and query logs, so you know where your queries are coming from. This only works with Rails, and is only tested with Rails 4.0.

Why do I want this?

Simple. To turn this

Without backtrace logging

into this.

With backtrace logging

So now, you needn't wonder where that odd-looking or broken SQL query is coming from.

It can also turn this

Reading mysql slow query log from /usr/local/mysql/data/mysqld51-apple-slow.log
Count: 1  Time=4.32s (4s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT "events".* FROM "events" WHERE "events"."bug_id" = ?

Count: 3  Time=2.53s (7s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT "deploys".* FROM "deploys" WHERE "deploys"."id" = ?

Count: 3  Time=2.13s (6s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT * FROM "slugs" WHERE (LOWER("slugs"."slug") = LOWER(?) AND "slugs"."scope" IS NULL AND "slugs"."sluggable_type" = ?) LIMIT 1

into this.

Reading mysql slow query log from /usr/local/mysql/data/mysqld51-apple-slow.log
Count: 1  Time=4.32s (4s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT "events".* FROM "events" WHERE "events"."bug_id" = ? /* app/models/project.rb:125:in `_callback_after_617' */

Count: 3  Time=2.53s (7s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT "deploys".* FROM "deploys" WHERE "deploys"."id" = ? /* app/controllers/projects_controller.rb:359:in `require_or_load' */

Count: 3  Time=2.13s (6s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT * FROM "slugs" WHERE (LOWER("slugs"."slug") = LOWER(?) AND "slugs"."scope" IS NULL AND "slugs"."sluggable_type" = ?) LIMIT 1 /* app/models/observers/bug_observer.rb:23:in `create_open_event' */

Installation

To use, add SQL:Origin to your Gemfile:

gem 'sql_origin'

If you would like to add three-line backtraces below every SQL query in your Rails log, add

SQLOrigin.append_to_log

somewhere in your Rails initialization (e.g., application.rb or a config/initializer file).

If you would like to add a one-line backtrace comment to every SQL query, add

SQLOrigin.append_to_query

somewhere in your Rails initialization.

It would be typical to enable append_to_log for development and test, and append_to_query for production, in order to keep production logs small.

Backtrace Filtering

By default, files not under your Rails root, and files under vendor, are filtered from your backtrace. If you need to filter other files, add them to {SQLOrigin::LIBRARY_PATHS}:

SQLOrigin::LIBRARY_PATHS << 'config/initializers/active_record_hacks.rb'