Commit 185222aa authored by Kamil Trzciński's avatar Kamil Trzciński

Raise exception on misaligned transaction

In some cases transaction tracking might have invalid
boundaries. Detect such cases and log them as an
exception.
parent c7a6ab39
......@@ -51,14 +51,17 @@ module Gitlab
return
elsif self.transaction_end?(parsed)
context[:transaction_depth_by_db][database] -= 1
if context[:transaction_depth_by_db][database] <= 0
if context[:transaction_depth_by_db][database] == 0
context[:modified_tables_by_db][database].clear
elsif context[:transaction_depth_by_db][database] < 0
context[:transaction_depth_by_db][database] = 0
raise CrossDatabaseModificationAcrossUnsupportedTablesError, "Something bad happened as we have misaligned transactions."
end
return
end
return if context[:transaction_depth_by_db].values.all?(&:zero?)
return unless self.in_transaction?
# PgQuery might fail in some cases due to limited nesting:
# https://github.com/pganalyze/pg_query/issues/209
......@@ -141,6 +144,10 @@ module Gitlab
Rails.env.test?
end
def self.in_transaction?
context[:transaction_depth_by_db].values.any?(&:positive?)
end
# We ignore execution in the #create method from FactoryBot
# because it is not representative of real code we run in
# production. There are far too many false positives caused
......
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