Commit f80e7b91 authored by Thong Kuah's avatar Thong Kuah

Merge branch '346755-catch-parse-errors-cross-joins' into 'master'

Rescue PGQuery::ParseError in prevent_cross_joins

See merge request gitlab-org/gitlab!75593
parents 162a0f93 63f8e788
......@@ -31,9 +31,13 @@ module Database
# See https://gitlab.com/gitlab-org/gitlab/-/issues/339396
return if sql.include?("DISABLE TRIGGER") || sql.include?("ENABLE TRIGGER")
# PgQuery might fail in some cases due to limited nesting:
# https://github.com/pganalyze/pg_query/issues/209
tables = PgQuery.parse(sql).tables
tables = begin
PgQuery.parse(sql).tables
rescue PgQuery::ParseError
# PgQuery might fail in some cases due to limited nesting:
# https://github.com/pganalyze/pg_query/issues/209
return
end
schemas = ::Gitlab::Database::GitlabSchema.table_schemas(tables)
......
......@@ -39,6 +39,15 @@ RSpec.describe Database::PreventCrossJoins do
expect { main_and_ci_query_allowlist_nested }.not_to raise_error
end
end
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
end
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