summaryrefslogtreecommitdiff
path: root/lib/gitlab/database.rb
diff options
context:
space:
mode:
authorAndreas Brandl <abrandl@gitlab.com>2019-07-12 13:30:42 +0000
committerAndreas Brandl <abrandl@gitlab.com>2019-07-12 13:30:42 +0000
commite13f07f1954f9f4d1cffed0f4c6d63c8ee7aa2a6 (patch)
tree147f8c4911f5efa9a4157103aa394f0a4bca4e4d /lib/gitlab/database.rb
parent5ea899d34f6332733bb5aee225c5f3ced340cb24 (diff)
parentd4a919679a1eb5d3e2aaed4b920e6027d2482971 (diff)
downloadgitlab-ce-e13f07f1954f9f4d1cffed0f4c6d63c8ee7aa2a6.tar.gz
Merge branch 'js-specs-transactions' into 'master'
Use transactions in JS feature specs Closes #60207 See merge request gitlab-org/gitlab-ce!27496
Diffstat (limited to 'lib/gitlab/database.rb')
-rw-r--r--lib/gitlab/database.rb25
1 files changed, 18 insertions, 7 deletions
diff --git a/lib/gitlab/database.rb b/lib/gitlab/database.rb
index 64e7f371115..3e4c720b49a 100644
--- a/lib/gitlab/database.rb
+++ b/lib/gitlab/database.rb
@@ -284,17 +284,28 @@ module Gitlab
end
# inside_transaction? will return true if the caller is running within a transaction. Handles special cases
- # when running inside a test environment, in which the entire test is running with a DatabaseCleaner transaction
+ # when running inside a test environment, where tests may be wrapped in transactions
def self.inside_transaction?
- ActiveRecord::Base.connection.open_transactions > open_transactions_baseline
+ if Rails.env.test?
+ ActiveRecord::Base.connection.open_transactions > open_transactions_baseline
+ else
+ ActiveRecord::Base.connection.open_transactions > 0
+ end
end
- def self.open_transactions_baseline
- if ::Rails.env.test?
- return DatabaseCleaner.connections.count { |conn| conn.strategy.is_a?(DatabaseCleaner::ActiveRecord::Transaction) }
- 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 self.set_open_transactions_baseline
+ @open_transactions_baseline = ActiveRecord::Base.connection.open_transactions
+ end
+
+ def self.reset_open_transactions_baseline
+ @open_transactions_baseline = 0
+ end
- 0
+ def self.open_transactions_baseline
+ @open_transactions_baseline ||= 0
end
private_class_method :open_transactions_baseline