From 8d9a8d8db5194b6c94b908a89f619bdd1faa98b6 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 28 Apr 2023 14:35:16 +0100 Subject: [PATCH 1/2] Enable verbose query logs in development This is a new feature in Rails 7. Whenever a query is triggered it not only show you the query (as before), but also tells you (directly in the logs) exactly which line of code was responsible for triggering the query. It's unbelievably useful for debugging and performance work. --- .env.development | 2 ++ config/application.rb | 3 +++ 2 files changed, 5 insertions(+) diff --git a/.env.development b/.env.development index 15e09487f2..1605f8ffba 100644 --- a/.env.development +++ b/.env.development @@ -5,6 +5,8 @@ # # cp .env.development .env.local +VERBOSE_QUERY_LOGS=true + SECRET_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" OFN_REDIS_URL="redis://localhost:6379/1" diff --git a/config/application.rb b/config/application.rb index 9672715eb4..d042539da4 100644 --- a/config/application.rb +++ b/config/application.rb @@ -221,6 +221,9 @@ module Openfoodnetwork config.assets.precompile += ['shared/*'] config.assets.precompile += ['*.jpg', '*.jpeg', '*.png', '*.gif' '*.svg'] + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = ENV.fetch("VERBOSE_QUERY_LOGS", false) + # Apply framework defaults. New recommended defaults are successively added with each Rails version and # include the defaults from previous versions. For more info see: # https://guides.rubyonrails.org/configuring.html#results-of-config-load-defaults From f42a9e3a66d8c2522d0010c3d9352279a80acdb9 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 28 Apr 2023 14:38:05 +0100 Subject: [PATCH 2/2] Add query_count gem in development A very simple gem that counts the total number of queries triggered by each request and displays a little number in the log. Nice. --- Gemfile | 1 + Gemfile.lock | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/Gemfile b/Gemfile index 4391b6d87d..2a6a3b8448 100644 --- a/Gemfile +++ b/Gemfile @@ -178,6 +178,7 @@ group :development do gem 'foreman' gem 'listen' gem 'pry', '~> 0.13.0' + gem 'query_count' gem 'rails-erd' gem 'rubocop' gem 'rubocop-rails' diff --git a/Gemfile.lock b/Gemfile.lock index b75cc34f48..cf7b703f0a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -487,6 +487,9 @@ GEM public_suffix (5.0.1) puma (6.2.2) nio4r (~> 2.0) + query_count (1.1.1) + activerecord (>= 4.2) + railties (>= 4.2) raabro (1.4.0) racc (1.6.2) rack (2.2.7) @@ -857,6 +860,7 @@ DEPENDENCIES private_address_check pry (~> 0.13.0) puma + query_count rack-mini-profiler (< 3.0.0) rack-rewrite rack-timeout