All Projects → brunofacca → Active Record Query Trace

brunofacca / Active Record Query Trace

Licence: mit
Rails plugin that logs/displays a backtrace of all SQL queries executed by Active Record

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Active Record Query Trace

active record-updated at
Touch `updated_at` by default with calls to `update_all` and `update_column(s)`
Stars: ✭ 27 (-96.56%)
Mutual labels:  activerecord, ruby-on-rails
activerecord-cockroachdb-adapter
CockroachDB adapter for ActiveRecord.
Stars: ✭ 90 (-88.54%)
Mutual labels:  activerecord, ruby-on-rails
prefixed ids
Friendly Prefixed IDs for your Ruby on Rails models
Stars: ✭ 159 (-79.75%)
Mutual labels:  activerecord, ruby-on-rails
Octopus
Database Sharding for ActiveRecord
Stars: ✭ 2,496 (+217.96%)
Mutual labels:  activerecord, ruby-on-rails
LocalSupport
A directory of local support services and volunteer opportunities
Stars: ✭ 60 (-92.36%)
Mutual labels:  activerecord, ruby-on-rails
activerecord-crate-adapter
Ruby on Rails ActiveRecord adapter for CrateDB
Stars: ✭ 27 (-96.56%)
Mutual labels:  activerecord, ruby-on-rails
nxt state machine
A simple but powerful state machine implementation.
Stars: ✭ 14 (-98.22%)
Mutual labels:  activerecord, ruby-on-rails
Type scopes
Automatic scopes for ActiveRecord models.
Stars: ✭ 24 (-96.94%)
Mutual labels:  activerecord, ruby-on-rails
filtered
Filters ActiveRecord queries in a nice way
Stars: ✭ 28 (-96.43%)
Mutual labels:  activerecord, ruby-on-rails
rails cursor pagination
Add cursor pagination to your ActiveRecord backed application
Stars: ✭ 21 (-97.32%)
Mutual labels:  activerecord, ruby-on-rails
Active record Events
Manage timestamps in ActiveRecord models
Stars: ✭ 109 (-86.11%)
Mutual labels:  activerecord, ruby-on-rails
Database validations
Database validations for ActiveRecord
Stars: ✭ 274 (-65.1%)
Mutual labels:  activerecord, ruby-on-rails
Model probe
Schema introspection for ActiveModel
Stars: ✭ 58 (-92.61%)
Mutual labels:  activerecord, ruby-on-rails
Ransack
Object-based searching.
Stars: ✭ 5,020 (+539.49%)
Mutual labels:  activerecord, ruby-on-rails
Goldiloader
Just the right amount of Rails eager loading
Stars: ✭ 1,074 (+36.82%)
Mutual labels:  activerecord, ruby-on-rails
activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (-97.32%)
Mutual labels:  activerecord, ruby-on-rails
Filterrific
Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
Stars: ✭ 810 (+3.18%)
Mutual labels:  activerecord, ruby-on-rails
squint
Search PostgreSQL jsonb and hstore columns
Stars: ✭ 26 (-96.69%)
Mutual labels:  activerecord, ruby-on-rails
Elasticsearch Rails
Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Stars: ✭ 2,896 (+268.92%)
Mutual labels:  activerecord, ruby-on-rails
Database consistency
The tool to find inconsistency between models schema and database constraints.
Stars: ✭ 418 (-46.75%)
Mutual labels:  activerecord, ruby-on-rails

Displays a backtrace for each query in Rails' development console and log. Allows you to track down where queries are executed in your application. Useful for performance optimizations and for finding where to start when making changes to a large application.

When enabled, every query will be logged like:

D, [2019-03-03T19:50:41.061115 #25560] DEBUG -- : User Load (0.1ms)  SELECT "users".* FROM "users"
D, [2019-03-03T19:50:41.062492 #25560] DEBUG -- : Query Trace:
      app/models/concerns/is_active.rb:11:in `active?'
      app/models/user.rb:67:in `active?'
      app/decorators/concerns/status_methods.rb:42:in `colored_status'
      app/views/shared/companies/_user.html.slim:28:in `block in _app_views_users_html_slim___2427456029761612502_70304705622200'
      app/views/shared/companies/_user.html.slim:27:in `_app_views_users_html_slim___2427456029761612502_70304705622200'

Requirements

  • Ruby >= 2.4;
  • Rails 4.2, 5.2, or 6.

Usage

  1. Add the following to your Gemfile:

    group :development do
      gem 'active_record_query_trace'
    end
    
  2. Create an initializer such as config/initializers/active_record_query_trace.rb to enable the gem. If you want to customize how the gem behaves, you can add any combination of the following options to the initializer as well.

    if Rails.env.development?
      ActiveRecordQueryTrace.enabled = true
      # Optional: other gem config options go here
    end
    
  3. Restart the Rails development server.

Options

Backtrace level

There are three levels of debug.

  • :app - includes only application trace lines (files in the Rails.root directory);
  • :rails - includes all trace lines except the ones from the application (all files except those in Rails.root).
  • :full - full backtrace (includes all files), useful for debugging gems.
ActiveRecordQueryTrace.level = :app # default

If you need more control you can provide a custom bactrace cleaner using the :custom level. For example:

ActiveRecordQueryTrace.level = :custom
require "rails/backtrace_cleaner"
ActiveRecordQueryTrace.backtrace_cleaner = Rails::BacktraceCleaner.new.tap do |bc|
  bc.remove_filters!
  bc.remove_silencers!
  bc.add_silencer { |line| line =~ /\b(active_record_query_trace|active_support|active_record|another_gem)\b/ }
end

It's not necessary to create an instance of Rails::BacktraceCleaner, you can use any object responding to #clean or even a lambda/proc:

ActiveRecordQueryTrace.backtrace_cleaner = ->(trace) {
  trace.reject { |line| line =~ /\b(active_record_query_trace|active_support|active_record|another_gem)\b/ }
}

Display the trace only for read or write queries

You can choose to display the backtrace only for DB reads, writes or both.

  • :all - display backtrace for all queries;
  • :read - display backtrace only for DB read operations (SELECT);
  • :write - display the backtrace only for DB write operations (INSERT, UPDATE, DELETE).
ActiveRecordQueryTrace.query_type = :all # default

Suppress DB read queries

If set to true, this option will suppress all log lines generated by DB read (SELECT) operations, leaving only the lines generated by DB write queries (INSERT, UPDATE, DELETE). Beware, the entire log line is suppressed, not only the backtrace. Useful to reduce noise in the logs (e.g., N+1 queries) when you only care about queries that write to the DB.

ActiveRecordQueryTrace.suppress_logging_of_db_reads = false # default

Ignore cached queries

By default, a backtrace will be logged for every query, even cached queries that do not actually hit the database. You might find it useful not to print the backtrace for cached queries:

ActiveRecordQueryTrace.ignore_cached_queries = true # Default is false.

Limit the number of lines in the backtrace

If you are working with a large app, you may wish to limit the number of lines displayed for each query. If you set level to :full, you might want to set lines to 0 so you can see the entire trace.

ActiveRecordQueryTrace.lines = 10 # Default is 5. Setting to 0 includes entire trace.

Colorize the backtrace

To colorize the output:

ActiveRecordQueryTrace.colorize = false           # No colorization (default)
ActiveRecordQueryTrace.colorize = :light_purple   # Colorize in specific color

Valid colors are: :black, :red, :green, :brown, :blue, :purple, :cyan, :gray, :dark_gray, :light_red, :light_green, :yellow, :light_blue, :light_purple, :light_cyan, :white.

Authors

  • Cody Caughlan - Original author.
  • Bruno Facca - Current maintainer. LinkedIn

Contributing

Bug reports

Please use the issue tracker to report any bugs.

Test environment

This gem uses RSpec for testing. You can run the test suite by executing the rspec command. It has a decent test coverage and the test suite takes less than a second to run as it uses an in-memory SQLite DB.

Developing

  1. Create an issue and describe your idea
  2. Fork it
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Implement your changes;
  5. Run the test suite (rspec)
  6. Commit your changes (git commit -m 'Add some feature')
  7. Publish the branch (git push origin my-new-feature)
  8. Create a Pull Request

License

Released under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].