Commit 975fff2c authored by Thong Kuah's avatar Thong Kuah

Fix nested calls of allowlist method

parent 1a614fb8
......@@ -21,8 +21,10 @@ module Database
module PreventCrossJoins
CrossJoinAcrossUnsupportedTablesError = Class.new(StandardError)
ALLOW_THREAD_KEY = :allow_cross_joins_across_databases
def self.validate_cross_joins!(sql)
return if Thread.current[:allow_cross_joins_across_databases]
return if Thread.current[ALLOW_THREAD_KEY]
# Allow spec/support/database_cleaner.rb queries to disable/enable triggers for many tables
# See https://gitlab.com/gitlab-org/gitlab/-/issues/339396
......@@ -55,7 +57,7 @@ module Database
::Database::PreventCrossJoins.validate_cross_joins!(event.payload[:sql])
end
Thread.current[:allow_cross_joins_across_databases] = false
Thread.current[ALLOW_THREAD_KEY] = false
yield
ensure
......@@ -65,11 +67,12 @@ module Database
module GitlabDatabaseMixin
def allow_cross_joins_across_databases(url:)
Thread.current[:allow_cross_joins_across_databases] = true
old_value = Thread.current[ALLOW_THREAD_KEY]
Thread.current[ALLOW_THREAD_KEY] = true
yield
ensure
Thread.current[:allow_cross_joins_across_databases] = false
Thread.current[ALLOW_THREAD_KEY] = old_value
end
end
end
......
......@@ -27,6 +27,12 @@ RSpec.describe Database::PreventCrossJoins do
expect { main_and_ci_query_allowlisted }.not_to raise_error
end
end
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
......@@ -38,6 +44,14 @@ RSpec.describe Database::PreventCrossJoins do
end
end
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
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment