From deccc8ab63ca6fae1e3ed97f5280e6b1ac9df376 Mon Sep 17 00:00:00 2001 From: Alex Ives Date: Tue, 22 Jun 2021 13:27:34 -0500 Subject: [PATCH] Fixes an error where pipe in query changes table Relates to https://gitlab.com/gitlab-org/database-team/gitlab-com-database-testing/issues/15 --- notifier/query.rb | 1 + notifier/spec/query_spec.rb | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/notifier/query.rb b/notifier/query.rb index c9126a52..c9f1bd98 100644 --- a/notifier/query.rb +++ b/notifier/query.rb @@ -41,6 +41,7 @@ class Query .gsub('/*', '/*') .gsub('*/', '*/') .gsub("\n", '
') + .gsub('|', '|') rescue StandardError => e warn "Query formatting error:\n#{e}\nFor query: #{query}" query diff --git a/notifier/spec/query_spec.rb b/notifier/spec/query_spec.rb index abf38c48..cc583db5 100644 --- a/notifier/spec/query_spec.rb +++ b/notifier/spec/query_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' RSpec.describe Query do - let(:pg_pass) do + let(:pgss) do { "query" => "select pg_database_size(current_database()) /*application:test*/", "calls" => 1, @@ -13,7 +13,7 @@ RSpec.describe Query do } end - subject(:query) { described_class.new(pg_pass) } + subject(:query) { described_class.new(pgss) } it 'inits from a pgpass row' do expect { subject }.not_to raise_error @@ -59,7 +59,7 @@ RSpec.describe Query do end it 'returns false if concurrent operations are below 5 minutes' do - pg_pass['query'] = 'CREATE INDEX CONCURRENTLY index_ci_runners_on_token_lower '\ + pgss['query'] = 'CREATE INDEX CONCURRENTLY index_ci_runners_on_token_lower '\ 'ON ci_runners (LOWER(token)) /*application:test*/' expect(subject.exceeds_time_guidance?).to be false @@ -71,4 +71,24 @@ RSpec.describe Query do expect(subject.exceeds_time_guidance?).to be false end end + + context 'when query has special characters' do + let(:pgss) do + { + "query" => "CREATE INDEX CONCURRENTLY \"tmp_index_merge_requests_draft_and_status\""\ + " ON \"merge_requests\" (\"id\") WHERE draft = false AND state_id = 1 AND "\ + "((title)::text ~* '^\\[draft\\]|\\(draft\\)|draft:|draft|\\[WIP\\]|WIP:|WIP'::text) "\ + "/*application:test*/", + "calls" => 1, + "total_time" => 1824825.496259, + "max_time" => 1824825.496259, + "mean_time" => 1824825.496259, + "rows" => 0 + } + end + + it 'replaces the pipe character with |' do + expect(subject.formatted_query).not_to include('|') + end + end end -- GitLab