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 ...@@ -51,14 +51,17 @@ module Gitlab
return return
elsif self.transaction_end?(parsed) elsif self.transaction_end?(parsed)
context[:transaction_depth_by_db][database] -= 1 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 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 end
return return
end 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: # 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
...@@ -141,6 +144,10 @@ module Gitlab ...@@ -141,6 +144,10 @@ module Gitlab
Rails.env.test? Rails.env.test?
end end
def self.in_transaction?
context[:transaction_depth_by_db].values.any?(&:positive?)
end
# We ignore execution in the #create method from FactoryBot # We ignore execution in the #create method from FactoryBot
# because it is not representative of real code we run in # because it is not representative of real code we run in
# production. There are far too many false positives caused # 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