diff options
author | Stan Hu <stanhu@gmail.com> | 2018-04-27 09:42:25 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-04-27 10:45:51 -0700 |
commit | 8c8b228acc93116188382fa97b32de5b5cd39f1b (patch) | |
tree | f9567ed84c602015b2521e77fbf1848efb06f07a | |
parent | 4c91ea317b103e314ed0b113651114f79f500a39 (diff) | |
download | gitlab-ce-sh-query-limiter-ignore-schema-loads.tar.gz |
Ignore Rails schema loads in QueryLimitersh-query-limiter-ignore-schema-loads
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
-rw-r--r-- | lib/gitlab/query_limiting/active_support_subscriber.rb | 11 | ||||
-rw-r--r-- | spec/lib/gitlab/query_limiting/active_support_subscriber_spec.rb | 7 |
2 files changed, 17 insertions, 1 deletions
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 |