Module: Legion::Data::Connection

Defined in:
lib/legion/data/connection.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.sequelObject

Returns the value of attribute sequel.



7
8
9
# File 'lib/legion/data/connection.rb', line 7

def sequel
  @sequel
end

Class Method Details

.adapterObject



9
10
11
# File 'lib/legion/data/connection.rb', line 9

def adapter
  @adapter ||= RUBY_ENGINE == 'jruby' ? :jdbc : :mysql2
end

.creds_builder(final_creds = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/data/connection.rb', line 32

def creds_builder(final_creds = {})
  final_creds.merge! Legion::Data::Settings.creds
  final_creds.merge! Legion::Settings[:data][:creds] if Legion::Settings[:data][:creds].is_a? Hash

  if Legion::Settings[:data][:connection][:max_connections].is_a? Integer
    final_creds[:max_connections] = Legion::Settings[:data][:connection][:max_connections]
  end

  final_creds[:preconnect] = :concurrently if Legion::Settings[:data][:connection][:preconnect]

  return final_creds if Legion::Settings[:vault].nil?

  if Legion::Settings[:vault][:connected] && ::Vault.sys.mounts.key?(:database)
    temp_vault_creds = Legion::Crypt.read('database/creds/legion')
    final_creds[:user] = temp_vault_creds[:username]
    final_creds[:password] = temp_vault_creds[:password]
  end

  final_creds
end

.default_credsObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/legion/data/connection.rb', line 53

def default_creds
  {
    host:            '127.0.0.1',
    port:            3306,
    username:        'legion',
    password:        'legion',
    database:        'legion',
    max_connections: 4
  }
end

.setupObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legion/data/connection.rb', line 13

def setup
  @sequel = if adapter == :mysql2
              ::Sequel.connect(adapter: adapter, **creds_builder)
            else
              ::Sequel.connect("jdbc:mysql://#{creds_builder[:host]}:#{creds_builder[:port]}/#{creds_builder[:database]}?user=#{creds_builder[:username]}&password=#{creds_builder[:password]}&serverTimezone=UTC") # rubocop:disable Layout/LineLength
            end
  Legion::Settings[:data][:connected] = true
  return if Legion::Settings[:data][:connection].nil? || Legion::Settings[:data][:connection][:log].nil?

  @sequel.logger = Legion::Logging
  @sequel.sql_log_level = Legion::Settings[:data][:connection][:sql_log_level]
  @sequel.log_warn_duration = Legion::Settings[:data][:connection][:log_warn_duration]
end

.shutdownObject



27
28
29
30
# File 'lib/legion/data/connection.rb', line 27

def shutdown
  @sequel&.disconnect
  Legion::Settings[:data][:connected] = false
end