From 8c8b228acc93116188382fa97b32de5b5cd39f1b Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Fri, 27 Apr 2018 09:42:25 -0700 Subject: Ignore Rails schema loads in QueryLimiter Rails will attempt to load the table schema the first time it accesses it. This can push specs over the query limit, and the application can't do anything about it (aside from avoiding column_exists? or table_exists? calls). Closes #45814 --- lib/gitlab/query_limiting/active_support_subscriber.rb | 11 ++++++++++- .../gitlab/query_limiting/active_support_subscriber_spec.rb | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/gitlab/query_limiting/active_support_subscriber.rb b/lib/gitlab/query_limiting/active_support_subscriber.rb index 4c83581c4b1..995a7f91151 100644 --- a/lib/gitlab/query_limiting/active_support_subscriber.rb +++ b/lib/gitlab/query_limiting/active_support_subscriber.rb @@ -4,10 +4,19 @@ module Gitlab attach_to :active_record def sql(event) - unless event.payload[:name] == 'CACHE' + unless event.payload[:name] == 'CACHE' || rails_schema_load?(event.payload[:sql]) Transaction.current&.increment end end + + # Rails will attempt to load the table schema the first time it accesses + # it. This can push specs over the query limit, and the application + # can't do anything about it (aside from avoiding column_exists? or + # table_exists? calls). + def rails_schema_load?(sql) + # Can't use Gitlab::Database.postgresql? since this may break test expectations + sql.match(/SELECT.*FROM pg_attribute /m) + end end end end diff --git a/spec/lib/gitlab/query_limiting/active_support_subscriber_spec.rb b/spec/lib/gitlab/query_limiting/active_support_subscriber_spec.rb index f8faeffb935..69b552640e8 100644 --- a/spec/lib/gitlab/query_limiting/active_support_subscriber_spec.rb +++ b/spec/lib/gitlab/query_limiting/active_support_subscriber_spec.rb @@ -18,6 +18,13 @@ describe Gitlab::QueryLimiting::ActiveSupportSubscriber do .once end + it 'ignores Rails schema loads' do + ActiveRecord::Base.connection.column_exists?(:users, :id) + + expect(transaction) + .not_to have_received(:increment) + end + context 'when the query is actually a rails cache hit' do it 'does not increment the number of executed SQL queries' do ActiveRecord::Base.connection.cache do -- cgit v1.2.1