diff --git a/config/initializers/0_marginalia.rb b/config/initializers/0_marginalia.rb index 344b24252e5af803cacda29051756898dd32e09e..e88599fd93cb0ca9d74363c5565fc218628b5167 100644 --- a/config/initializers/0_marginalia.rb +++ b/config/initializers/0_marginalia.rb @@ -17,9 +17,9 @@ # As mentioned in https://github.com/basecamp/marginalia/pull/93/files, # adding :line has some overhead because a regexp on the backtrace has -# to be run on every SQL query. Only enable this in development because +# to be run on every SQL query. Only enable this in development and test because # we've seen it slow things down. -if Rails.env.development? +if Gitlab.dev_or_test_env? Marginalia::Comment.components << :line Marginalia::Comment.lines_to_ignore = Regexp.union( Gitlab::BacktraceCleaner::IGNORE_BACKTRACES + %w[ diff --git a/spec/requests/api/graphql/project/merge_requests_spec.rb b/spec/requests/api/graphql/project/merge_requests_spec.rb index c7f121c48ab8b8537c15bac6cfc5fe8d3444f07c..d69f542cd185331181f1d3a98db580083010424e 100644 --- a/spec/requests/api/graphql/project/merge_requests_spec.rb +++ b/spec/requests/api/graphql/project/merge_requests_spec.rb @@ -506,10 +506,12 @@ def pagination_query(params) end context 'when only the count is requested' do + let_it_be(:merged_at) { Time.new(2020, 1, 3) } + context 'when merged at filter is present' do let_it_be(:merge_request) do create(:merge_request, :unique_branches, source_project: project).tap do |mr| - mr.metrics.update!(merged_at: Time.new(2020, 1, 3)) + mr.metrics.update!(merged_at: merged_at, created_at: merged_at - 2.days) end end @@ -526,12 +528,18 @@ def pagination_query(params) it 'does not query the merge requests table for the count' do query_recorder = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: current_user) } - queries = query_recorder.data.each_value.first[:occurrences] + queries = query_recorder.log expect(queries).not_to include(match(/SELECT COUNT\(\*\) FROM "merge_requests"/)) expect(queries).to include(match(/SELECT COUNT\(\*\) FROM "merge_request_metrics"/)) end context 'when total_time_to_merge and count is queried' do + let_it_be(:merge_request_2) do + create(:merge_request, :unique_branches, source_project: project).tap do |mr| + mr.metrics.update!(merged_at: merged_at, created_at: merged_at - 1.day) + end + end + let(:query) do graphql_query_for(:project, { full_path: project.full_path }, <<~QUERY) mergeRequests(mergedAfter: "2020-01-01", mergedBefore: "2020-01-05", first: 0) { @@ -541,11 +549,18 @@ def pagination_query(params) QUERY end - it 'does not query the merge requests table for the total_time_to_merge' do + it 'uses the merge_request_metrics table for total_time_to_merge' do query_recorder = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: current_user) } - queries = query_recorder.data.each_value.first[:occurrences] - expect(queries).to include(match(/SELECT.+SUM.+FROM "merge_request_metrics" WHERE/)) + expect(query_recorder.log).to include(match(/SELECT.+SUM.+FROM "merge_request_metrics" WHERE/)) + end + + it 'returns the correct total time to merge' do + post_graphql(query, current_user: current_user) + + sum = graphql_data_at(:project, :merge_requests, :total_time_to_merge) + + expect(sum).to eq(3.days.to_f) end end diff --git a/spec/support_specs/helpers/active_record/query_recorder_spec.rb b/spec/support_specs/helpers/active_record/query_recorder_spec.rb index f1af9ceffb9aa3fb2cbb29b1adcdb6a652c0a50a..d6c52b22449aed0f580ba613113dd53698470c0e 100644 --- a/spec/support_specs/helpers/active_record/query_recorder_spec.rb +++ b/spec/support_specs/helpers/active_record/query_recorder_spec.rb @@ -78,12 +78,14 @@ def expect_section(query, lines) end describe 'detecting the right number of calls and their origin' do - it 'detects two separate queries' do - control = ActiveRecord::QueryRecorder.new query_recorder_debug: true do + let(:control) do + ActiveRecord::QueryRecorder.new query_recorder_debug: true do 2.times { TestQueries.count } TestQueries.first end + end + it 'detects two separate queries' do # Check #find_query expect(control.find_query(/.*/, 0).size) .to eq(control.data.keys.size) @@ -98,8 +100,8 @@ def expect_section(query, lines) expect(control.log.size).to eq(3) # Ensure memoization value match the raw value above expect(control.count).to eq(control.log.size) - # Ensure we have only two sources of queries - expect(control.data.keys.size).to eq(1) + # Ensure we have two sources of queries + expect(control.data.keys.size).to eq(2) end end end