diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
commit | 6ed4ec3e0b1340f96b7c043ef51d1b33bbe85fde (patch) | |
tree | dc4d20fe6064752c0bd323187252c77e0a89144b /spec/lib/gitlab/database/query_analyzers/ci/partitioning_analyzer_spec.rb | |
parent | 9868dae7fc0655bd7ce4a6887d4e6d487690eeed (diff) |
Add latest changes from gitlab-org/gitlab@15-4-stable-eev15.4.0-rc42
Diffstat (limited to 'spec/lib/gitlab/database/query_analyzers/ci/partitioning_analyzer_spec.rb')
-rw-r--r-- | spec/lib/gitlab/database/query_analyzers/ci/partitioning_analyzer_spec.rb | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/spec/lib/gitlab/database/query_analyzers/ci/partitioning_analyzer_spec.rb b/spec/lib/gitlab/database/query_analyzers/ci/partitioning_analyzer_spec.rb new file mode 100644 index 00000000000..ef7c7965c09 --- /dev/null +++ b/spec/lib/gitlab/database/query_analyzers/ci/partitioning_analyzer_spec.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Database::QueryAnalyzers::Ci::PartitioningAnalyzer, 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 + + context 'when querying a routing table' do + it 'does not raise error' do + expect { process_sql(Ci::BuildMetadata, "SELECT 1 FROM p_ci_builds_metadata") } + .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 |