diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-17 11:33:21 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-17 11:33:21 +0000 |
commit | 7021455bd1ed7b125c55eb1b33c5a01f2bc55ee0 (patch) | |
tree | 5bdc2229f5198d516781f8d24eace62fc7e589e9 /spec/lib/gitlab/database/query_analyzers/ci/partitioning_routing_analyzer_spec.rb | |
parent | 185b095e93520f96e9cfc31d9c3e69b498cdab7c (diff) | |
download | gitlab-ce-7021455bd1ed7b125c55eb1b33c5a01f2bc55ee0.tar.gz |
Add latest changes from gitlab-org/gitlab@15-6-stable-eev15.6.0-rc42
Diffstat (limited to 'spec/lib/gitlab/database/query_analyzers/ci/partitioning_routing_analyzer_spec.rb')
-rw-r--r-- | spec/lib/gitlab/database/query_analyzers/ci/partitioning_routing_analyzer_spec.rb | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/spec/lib/gitlab/database/query_analyzers/ci/partitioning_routing_analyzer_spec.rb b/spec/lib/gitlab/database/query_analyzers/ci/partitioning_routing_analyzer_spec.rb new file mode 100644 index 00000000000..1f86c2ccbb0 --- /dev/null +++ b/spec/lib/gitlab/database/query_analyzers/ci/partitioning_routing_analyzer_spec.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Database::QueryAnalyzers::Ci::PartitioningRoutingAnalyzer, query_analyzers: false do + let(:analyzer) { described_class } + + before do + allow(Gitlab::Database::QueryAnalyzer.instance).to receive(:all_analyzers).and_return([analyzer]) + end + + context 'when ci_partitioning_analyze_queries is disabled' do + before do + stub_feature_flags(ci_partitioning_analyze_queries: false) + end + + it 'does not analyze the query' do + expect(analyzer).not_to receive(:analyze) + + process_sql(Ci::BuildMetadata, "SELECT 1 FROM ci_builds_metadata") + end + end + + context 'when ci_partitioning_analyze_queries is enabled' do + context 'when analyzing targeted tables' do + described_class::ENABLED_TABLES.each do |enabled_table| + context 'when querying a non routing table' do + it 'tracks exception' do + expect(::Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception) + process_sql(Ci::ApplicationRecord, "SELECT 1 FROM #{enabled_table}") + end + + it 'raises RoutingTableNotUsedError' do + expect { process_sql(Ci::ApplicationRecord, "SELECT 1 FROM #{enabled_table}") } + .to raise_error(described_class::RoutingTableNotUsedError) + end + end + end + + context 'when updating a record' do + it 'raises RoutingTableNotUsedError' do + expect { process_sql(Ci::BuildMetadata, "UPDATE ci_builds_metadata SET id = 1") } + .to raise_error(described_class::RoutingTableNotUsedError) + end + end + + context 'when inserting a record' do + it 'raises RoutingTableNotUsedError' do + expect { process_sql(Ci::BuildMetadata, "INSERT INTO ci_builds_metadata (id) VALUES(1)") } + .to raise_error(described_class::RoutingTableNotUsedError) + end + end + end + + context 'when analyzing non targeted table' do + it 'does not raise error' do + expect { process_sql(Ci::BuildMetadata, "SELECT 1 FROM projects") }.not_to raise_error + end + end + end + + private + + def process_sql(model, sql) + Gitlab::Database::QueryAnalyzer.instance.within do + # Skip load balancer and retrieve connection assigned to model + Gitlab::Database::QueryAnalyzer.instance.send(:process_sql, sql, model.retrieve_connection) + end + end +end |