diff options
Diffstat (limited to 'spec/lib/gitlab/database/query_analyzers')
4 files changed, 57 insertions, 12 deletions
diff --git a/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_metrics_spec.rb b/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_metrics_spec.rb index 62c5ead855a..3a92f35d585 100644 --- a/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_metrics_spec.rb +++ b/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_metrics_spec.rb @@ -53,6 +53,14 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasMetrics, query_ana gitlab_schemas: "gitlab_ci", db_config_name: "ci" } + }, + "for query accessing gitlab_main and unknown schema" => { + model: ApplicationRecord, + sql: "SELECT 1 FROM projects LEFT JOIN not_in_schema ON not_in_schema.project_id=projects.id", + expectations: { + gitlab_schemas: "gitlab_main,undefined_not_in_schema", + db_config_name: "main" + } } } end diff --git a/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_validate_connection_spec.rb b/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_validate_connection_spec.rb index ddf5793049d..47038bbd138 100644 --- a/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_validate_connection_spec.rb +++ b/spec/lib/gitlab/database/query_analyzers/gitlab_schemas_validate_connection_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection, query_analyzers: false do +RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection, query_analyzers: false, + feature_category: :pods do let(:analyzer) { described_class } # We keep only the GitlabSchemasValidateConnection analyzer running @@ -51,6 +52,12 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::GitlabSchemasValidateConnection sql: "SELECT 1 FROM ci_builds", expect_error: /The query tried to access \["ci_builds"\]/, setup: -> (_) { skip_if_multiple_databases_not_setup } + }, + "for query accessing unknown gitlab_schema" => { + model: ::ApplicationRecord, + sql: "SELECT 1 FROM new_table", + expect_error: /The query tried to access \["new_table"\] \(of undefined_new_table\)/, + setup: -> (_) { skip_if_multiple_databases_not_setup } } } end diff --git a/spec/lib/gitlab/database/query_analyzers/prevent_cross_database_modification_spec.rb b/spec/lib/gitlab/database/query_analyzers/prevent_cross_database_modification_spec.rb index 22a70dc7df0..a4322689bf9 100644 --- a/spec/lib/gitlab/database/query_analyzers/prevent_cross_database_modification_spec.rb +++ b/spec/lib/gitlab/database/query_analyzers/prevent_cross_database_modification_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' -RSpec.describe Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification, query_analyzers: false do +RSpec.describe Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification, query_analyzers: false, + feature_category: :pods do let_it_be(:pipeline, refind: true) { create(:ci_pipeline) } let_it_be(:project, refind: true) { create(:project) } diff --git a/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb b/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb index bcc39c0c3db..22ff66ff55e 100644 --- a/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb +++ b/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Gitlab::Database::QueryAnalyzers::QueryRecorder, query_analyzers: false do +RSpec.describe Gitlab::Database::QueryAnalyzers::QueryRecorder, feature_category: :database, query_analyzers: false do # We keep only the QueryRecorder analyzer running around do |example| described_class.with_suppressed(false) do @@ -11,7 +11,6 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::QueryRecorder, query_analyzers: end context 'with query analyzer' do - let(:query) { 'SELECT 1 FROM projects' } let(:log_path) { Rails.root.join(described_class::LOG_PATH) } let(:log_file) { described_class.log_file } @@ -20,14 +19,44 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::QueryRecorder, query_analyzers: end shared_examples_for 'an enabled query recorder' do - it 'logs queries to a file' do - allow(FileUtils).to receive(:mkdir_p) - .with(log_path) - expect(File).to receive(:write) - .with(log_file, /^{"sql":"#{query}/, mode: 'a') - expect(described_class).to receive(:analyze).with(/^#{query}/).and_call_original - - expect { ApplicationRecord.connection.execute(query) }.not_to raise_error + using RSpec::Parameterized::TableSyntax + + normalized_query = <<~SQL.strip.tr("\n", ' ') + SELECT \\\\"projects\\\\".\\\\"id\\\\" + FROM \\\\"projects\\\\" + WHERE \\\\"projects\\\\".\\\\"namespace_id\\\\" = \\? + AND \\\\"projects\\\\".\\\\"id\\\\" IN \\(\\?,\\?,\\?\\); + SQL + + where(:list_parameter, :bind_parameters) do + '$2, $3' | [1, 2, 3] + '$2, $3, $4' | [1, 2, 3, 4] + '$2 ,$3 ,$4 ,$5' | [1, 2, 3, 4, 5] + '$2 , $3 , $4 , $5, $6' | [1, 2, 3, 4, 5, 6] + '$2, $3 ,$4 , $5,$6,$7' | [1, 2, 3, 4, 5, 6, 7] + '$2,$3,$4,$5,$6,$7,$8' | [1, 2, 3, 4, 5, 6, 7, 8] + end + + with_them do + before do + allow(described_class).to receive(:analyze).and_call_original + allow(FileUtils).to receive(:mkdir_p) + .with(log_path) + end + + it 'logs normalized queries to a file' do + expect(File).to receive(:write) + .with(log_file, /^{"normalized":"#{normalized_query}/, mode: 'a') + + expect do + ApplicationRecord.connection.exec_query(<<~SQL.strip.tr("\n", ' '), 'SQL', bind_parameters) + SELECT "projects"."id" + FROM "projects" + WHERE "projects"."namespace_id" = $1 + AND "projects"."id" IN (#{list_parameter}); + SQL + end.not_to raise_error + end end end |