Module: Webhookdb::Dbutil

Includes:
Appydays::Configurable
Included in:
ConnectionCache, Webhookdb::DBAdapter::SequelConnection, Organization::DatabaseMigration, Organization::DbBuilder, SyncTarget
Defined in:
lib/webhookdb/dbutil.rb

Overview

Mixin that provides helpers when dealing with databases and connections.

Use borrow_conn to create a connection that is disconnected after the block runs. A block must be given. By default, this connection uses the Webhookdb logger, and uses test: false and keep_reference: false Sequel.connect options, since this is a quick-lived and self-managed connection.

Use take_conn where you will take care of disconnecting the connection. Note you MUST take care to call ‘disconnect` at some point or connections will leak.

Constant Summary collapse

MOCK_CONN =

Needed when we need to work with a source.

Sequel.connect("mock://")

Class Method Summary collapse

Class Method Details

.borrow_conn(url, **opts, &block) ⇒ Object

Raises:

  • (LocalJumpError)


46
47
48
49
50
# File 'lib/webhookdb/dbutil.rb', line 46

module_function def borrow_conn(url, **opts, &block)
  raise LocalJumpError, "borrow_conn requires a block" if block.nil?
  opts = conn_opts(opts)
  Sequel.connect(url, **opts, &block)
end

.configured_connection_optionsObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/webhookdb/dbutil.rb', line 67

def self.configured_connection_options
  res = {}
  res[:sql_log_level] ||= :debug
  res[:log_warn_duration] ||= Webhookdb::Dbutil.slow_query_seconds
  res[:max_connections] ||= Webhookdb::Dbutil.max_connections
  res[:pool_timeout] ||= Webhookdb::Dbutil.pool_timeout
  res[:driver_options] = {}
  (res[:driver_options][:gssencmode] = Webhookdb::Dbutil.gssencmode) if Webhookdb::Dbutil.gssencmode.present?
  return res
end

.conn_opts(opts) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/webhookdb/dbutil.rb', line 58

module_function def conn_opts(opts)
  res = Webhookdb::Dbutil.configured_connection_options
  res.merge!(opts)
  res[:test] = false unless res.key?(:test)
  res[:loggers] = [Webhookdb.logger] unless res.key?(:logger) || res.key?(:loggers)
  res[:keep_reference] = false unless res.key?(:keep_reference)
  return res
end

.displaysafe_url(url) ⇒ Object



78
79
80
81
82
83
# File 'lib/webhookdb/dbutil.rb', line 78

module_function def displaysafe_url(url)
  u = URI(url)
  u.user = "***"
  u.password = "***"
  return u.to_s
end

.reduce_expr(dataset, op_symbol, operands, method: :where) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/webhookdb/dbutil.rb', line 85

module_function def reduce_expr(dataset, op_symbol, operands, method: :where)
  return dataset if operands.blank?
  present_ops = operands.select(&:present?)
  return dataset if present_ops.empty?
  full_op = present_ops.reduce(&op_symbol)
  return dataset.send(method, full_op)
end

.take_conn(url, **opts, &block) ⇒ Object

Raises:

  • (LocalJumpError)


52
53
54
55
56
# File 'lib/webhookdb/dbutil.rb', line 52

module_function def take_conn(url, **opts, &block)
  raise LocalJumpError, "take_conn cannot use a block" unless block.nil?
  opts = conn_opts(opts)
  return Sequel.connect(url, **opts, &block)
end