diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-15 18:10:57 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-15 18:10:57 +0300 |
commit | e5c7d631a84940c66e46f1824ba7ce0c7f1d0ea4 (patch) | |
tree | ac1cce129d8c8cdcee6a668e27d8034d2da172e4 /app/models/concerns/transactions.rb | |
parent | 0687020509cafe1e24eb0bfa0e0f5c9f6c4799d2 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/concerns/transactions.rb')
-rw-r--r-- | app/models/concerns/transactions.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/app/models/concerns/transactions.rb b/app/models/concerns/transactions.rb new file mode 100644 index 00000000000..a186ebc8475 --- /dev/null +++ b/app/models/concerns/transactions.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module Transactions + extend ActiveSupport::Concern + + class_methods do + # inside_transaction? will return true if the caller is running within a + # transaction. Handles special cases when running inside a test environment, + # where tests may be wrapped in transactions + def inside_transaction? + base = Rails.env.test? ? @open_transactions_baseline.to_i : 0 + + connection.open_transactions > base + end + + # These methods that access @open_transactions_baseline are not thread-safe. + # These are fine though because we only call these in RSpec's main thread. + # If we decide to run specs multi-threaded, we would need to use something + # like ThreadGroup to keep track of this value + def set_open_transactions_baseline + @open_transactions_baseline = connection.open_transactions + end + + def reset_open_transactions_baseline + @open_transactions_baseline = 0 + end + end +end |