diff options
-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 |