2021-10-27 15:23:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Database::PreventCrossJoins do
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'when running in a default scope' do
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'when only non-CI tables are used' do
|
|
|
|
it 'does not raise exception' do
|
|
|
|
expect { main_only_query }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when only CI tables are used' do
|
|
|
|
it 'does not raise exception' do
|
|
|
|
expect { ci_only_query }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when CI and non-CI tables are used' do
|
|
|
|
it 'raises exception' do
|
|
|
|
expect { main_and_ci_query }.to raise_error(
|
|
|
|
described_class::CrossJoinAcrossUnsupportedTablesError)
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'when annotation is used' do
|
|
|
|
it 'does not raise exception' do
|
|
|
|
expect { main_and_ci_allowed_via_annotate }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'when allow_cross_joins_across_databases is used' do
|
|
|
|
it 'does not raise exception' do
|
2021-11-11 11:23:49 +05:30
|
|
|
expect { main_and_ci_query_allowlisted }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
context 'when allow_cross_joins_across_databases is used' do
|
|
|
|
it 'does not raise exception' do
|
|
|
|
expect { main_and_ci_query_allowlist_nested }.not_to raise_error
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
context 'when there is a parser error' do
|
|
|
|
it 'does not raise parse PGQuery::ParseError' do
|
|
|
|
# Since this is in an invalid query it still raises from ActiveRecord
|
|
|
|
# but this tests that we rescue the PGQuery::ParseError which would
|
|
|
|
# have otherwise raised first
|
|
|
|
expect { ApplicationRecord.connection.execute('SELECT SELECT FROM SELECT') }.to raise_error(ActiveRecord::StatementInvalid)
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def main_and_ci_query_allowlisted
|
|
|
|
Gitlab::Database.allow_cross_joins_across_databases(url: 'http://issue-url') do
|
|
|
|
main_and_ci_query
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def main_and_ci_query_allowlist_nested
|
|
|
|
Gitlab::Database.allow_cross_joins_across_databases(url: 'http://issue-url') do
|
|
|
|
main_and_ci_query_allowlisted
|
|
|
|
|
|
|
|
main_and_ci_query
|
|
|
|
end
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
def main_and_ci_allowed_via_annotate
|
|
|
|
main_and_ci_query do |relation|
|
|
|
|
relation.allow_cross_joins_across_databases(url: 'http://issue-url')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def main_only_query
|
|
|
|
Issue.joins(:project).last
|
|
|
|
end
|
|
|
|
|
|
|
|
def ci_only_query
|
|
|
|
Ci::Build.joins(:pipeline).last
|
|
|
|
end
|
|
|
|
|
|
|
|
def main_and_ci_query
|
2021-11-18 22:05:49 +05:30
|
|
|
relation = Ci::Build.joins(:project)
|
|
|
|
relation = yield(relation) if block_given?
|
|
|
|
relation.last
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
end
|