1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_analyzers: false do
let(:analyzer) { described_class }
before do
allow(Gitlab::Database::QueryAnalyzer.instance).to receive(:all_analyzers).and_return([analyzer])
end
it 'does not increment metrics if feature flag is disabled' do
stub_feature_flags(query_analyzer_gitlab_schema_metrics: false)
expect(analyzer).not_to receive(:analyze)
process_sql(ActiveRecord::Base, "SELECT 1 FROM projects")
end
context 'properly observes all queries', :mocked_ci_connection do
using RSpec::Parameterized::TableSyntax
where do
{
"for simple query observes schema correctly" => {
model: ApplicationRecord,
sql: "SELECT 1 FROM projects",
expectations: {
gitlab_schemas: "gitlab_main",
db_config_name: "main"
}
},
"for query accessing gitlab_ci and gitlab_main" => {
model: ApplicationRecord,
sql: "SELECT 1 FROM projects LEFT JOIN ci_builds ON ci_builds.project_id=projects.id",
expectations: {
gitlab_schemas: "gitlab_ci,gitlab_main",
db_config_name: "main"
}
},
"for query accessing gitlab_ci and gitlab_main the gitlab_schemas is always ordered" => {
model: ApplicationRecord,
sql: "SELECT 1 FROM ci_builds LEFT JOIN projects ON ci_builds.project_id=projects.id",
expectations: {
gitlab_schemas: "gitlab_ci,gitlab_main",
db_config_name: "main"
}
},
"for query accessing CI database" => {
model: Ci::ApplicationRecord,
sql: "SELECT 1 FROM ci_builds",
expectations: {
gitlab_schemas: "gitlab_ci",
db_config_name: "ci"
}
}
}
end
with_them do
around do |example|
Gitlab::Database::QueryAnalyzer.instance.within { example.run }
end
it do
expect(described_class.schemas_metrics).to receive(:increment)
.with(expectations).and_call_original
process_sql(model, sql)
end
end
end
def process_sql(model, sql)
Gitlab::Database::QueryAnalyzer.instance.within do
# Skip load balancer and retrieve connection assigned to model
Gitlab::Database::QueryAnalyzer.instance.process_sql(sql, model.retrieve_connection)
end
end
end
|