diff options
Diffstat (limited to 'spec/support_specs/database')
-rw-r--r-- | spec/support_specs/database/prevent_cross_database_modification_spec.rb | 27 | ||||
-rw-r--r-- | spec/support_specs/database/prevent_cross_joins_spec.rb | 28 |
2 files changed, 42 insertions, 13 deletions
diff --git a/spec/support_specs/database/prevent_cross_database_modification_spec.rb b/spec/support_specs/database/prevent_cross_database_modification_spec.rb index 4fd55d59db0..e86559bb14a 100644 --- a/spec/support_specs/database/prevent_cross_database_modification_spec.rb +++ b/spec/support_specs/database/prevent_cross_database_modification_spec.rb @@ -66,7 +66,7 @@ RSpec.describe 'Database::PreventCrossDatabaseModification' do pipeline.touch end end - end.to raise_error /Cross-database data modification queries/ + end.to raise_error /Cross-database data modification/ end end @@ -84,7 +84,7 @@ RSpec.describe 'Database::PreventCrossDatabaseModification' do context 'when data modification happens in a transaction' do it 'raises error' do Project.transaction do - expect { run_queries }.to raise_error /Cross-database data modification queries/ + expect { run_queries }.to raise_error /Cross-database data modification/ end end @@ -93,12 +93,31 @@ RSpec.describe 'Database::PreventCrossDatabaseModification' do Project.transaction(requires_new: true) do project.touch Project.transaction(requires_new: true) do - expect { pipeline.touch }.to raise_error /Cross-database data modification queries/ + expect { pipeline.touch }.to raise_error /Cross-database data modification/ end end end end end + + context 'when executing a SELECT FOR UPDATE query' do + def run_queries + project.touch + pipeline.lock! + end + + context 'outside transaction' do + it { expect { run_queries }.not_to raise_error } + end + + context 'when data modification happens in a transaction' do + it 'raises error' do + Project.transaction do + expect { run_queries }.to raise_error /Cross-database data modification/ + end + end + end + end end context 'when CI association is modified through project' do @@ -127,7 +146,7 @@ RSpec.describe 'Database::PreventCrossDatabaseModification' do ApplicationRecord.transaction do create(:ci_pipeline) end - end.to raise_error /Cross-database data modification queries/ + end.to raise_error /Cross-database data modification/ end it 'skips raising error on factory creation' do diff --git a/spec/support_specs/database/prevent_cross_joins_spec.rb b/spec/support_specs/database/prevent_cross_joins_spec.rb index dd4ed9c40b8..e9a95fe77a5 100644 --- a/spec/support_specs/database/prevent_cross_joins_spec.rb +++ b/spec/support_specs/database/prevent_cross_joins_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' RSpec.describe Database::PreventCrossJoins do - context 'when running in :prevent_cross_joins scope', :prevent_cross_joins do + context 'when running in a default scope' do context 'when only non-CI tables are used' do it 'does not raise exception' do expect { main_only_query }.not_to raise_error @@ -24,23 +24,33 @@ RSpec.describe Database::PreventCrossJoins do context 'when allow_cross_joins_across_databases is used' do it 'does not raise exception' do - Gitlab::Database.allow_cross_joins_across_databases(url: 'http://issue-url') + expect { main_and_ci_query_allowlisted }.not_to raise_error + end + end - expect { main_and_ci_query }.not_to raise_error + 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 end end end end - context 'when running in a default scope' do - context 'when CI and non-CI tables are used' do - it 'does not raise exception' do - expect { main_and_ci_query }.not_to raise_error - end + private + + def main_and_ci_query_allowlisted + Gitlab::Database.allow_cross_joins_across_databases(url: 'http://issue-url') do + main_and_ci_query end end - private + 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 def main_only_query Issue.joins(:project).last |