Commit dfa10174 authored by Thong Kuah's avatar Thong Kuah

Merge branch 'reintroduce_cross_db_check' into 'master'

Reintroduce cross-database query check

See merge request gitlab-org/gitlab!69261
parents 68ee227a 6a6dac47
...@@ -30,7 +30,15 @@ module Database ...@@ -30,7 +30,15 @@ module Database
# PgQuery might fail in some cases due to limited nesting: # PgQuery might fail in some cases due to limited nesting:
# https://github.com/pganalyze/pg_query/issues/209 # https://github.com/pganalyze/pg_query/issues/209
#
# Also, we disable GC while parsing because of https://github.com/pganalyze/pg_query/issues/226
begin
GC.disable
tables = PgQuery.parse(sql).tables tables = PgQuery.parse(sql).tables
ensure
GC.enable
end
schemas = Database::GitlabSchema.table_schemas(tables) schemas = Database::GitlabSchema.table_schemas(tables)
if schemas.include?(:gitlab_ci) && schemas.include?(:gitlab_main) if schemas.include?(:gitlab_ci) && schemas.include?(:gitlab_main)
...@@ -72,10 +80,13 @@ ALLOW_LIST = Set.new(YAML.load_file(Rails.root.join('.cross-join-allowlist.yml') ...@@ -72,10 +80,13 @@ ALLOW_LIST = Set.new(YAML.load_file(Rails.root.join('.cross-join-allowlist.yml')
RSpec.configure do |config| RSpec.configure do |config|
config.include(::Database::PreventCrossJoins::SpecHelpers) config.include(::Database::PreventCrossJoins::SpecHelpers)
# TODO: remove `:prevent_cross_joins` to enable the check by default config.around do |example|
config.around(:each, :prevent_cross_joins) do |example|
Thread.current[:has_cross_join_exception] = false Thread.current[:has_cross_join_exception] = false
if ALLOW_LIST.include?(example.file_path)
example.run
else
with_cross_joins_prevented { example.run } with_cross_joins_prevented { example.run }
end end
end
end end
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Database::PreventCrossJoins do 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 context 'when only non-CI tables are used' do
it 'does not raise exception' do it 'does not raise exception' do
expect { main_only_query }.not_to raise_error expect { main_only_query }.not_to raise_error
...@@ -32,14 +32,6 @@ RSpec.describe Database::PreventCrossJoins do ...@@ -32,14 +32,6 @@ RSpec.describe Database::PreventCrossJoins do
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
end
end
private private
def main_only_query def main_only_query
......
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