Class: Gitlab::Database::Transaction::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/transaction/settings.rb

Constant Summary collapse

ALLOWED_CONFIGS =
%w[
  LOCK_TIMEOUT
].freeze
InvalidConfigError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.check_allowed!(config_name) ⇒ Object

Raises:



39
40
41
42
43
# File 'lib/gitlab/database/transaction/settings.rb', line 39

def check_allowed!(config_name)
  return if ALLOWED_CONFIGS.include?(config_name)

  raise InvalidConfigError, "Config #{config_name} is not allowed"
end

.get(config_name, connection = ApplicationRecord.connection) ⇒ Object



33
34
35
36
37
# File 'lib/gitlab/database/transaction/settings.rb', line 33

def get(config_name, connection = ApplicationRecord.connection)
  check_allowed!(config_name)

  connection.select_all("SHOW #{config_name}").rows[0][0]
end

.set(config_name, value, connection = ApplicationRecord.connection) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/gitlab/database/transaction/settings.rb', line 24

def set(config_name, value, connection = ApplicationRecord.connection)
  check_allowed!(config_name)

  quoted_value = connection.quote(value)
  query = "SET LOCAL #{config_name} = #{quoted_value}"

  connection.exec_query(query)
end

.with(config_name, value, connection = ApplicationRecord.connection) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/gitlab/database/transaction/settings.rb', line 14

def with(config_name, value, connection = ApplicationRecord.connection)
  old_value = get(config_name, connection)

  set(config_name, value, connection)

  yield

  set(config_name, old_value, connection)
end