Sha256: b20305162fa0b453e0edb7d0f0694b390dac433b28780c2e72e0657fae6137c6

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require 'sequel'

module Legion
  module Data
    module Connection
      class << self
        attr_reader :sequel
        if RUBY_ENGINE == 'jruby'
          require_relative 'connections/jdbc'
          include Legion::Data::Connections::JDBC
        else
          require_relative 'connections/mysql2'
          include Legion::Data::Connections::MySQL2
        end

        def setup
          @sequel = ::Sequel.connect(adapter: adapter, **creds_builder)
          Legion::Settings[:data][:connected] = true
          return unless Legion::Settings[:data][:connection][:log]

          @sequel.logger = Legion::Logging::Logger.new(level: 'debug')
          @sequel.sql_log_level = Legion::Settings[:data][:connection][:sql_log_level]
          @sequel.log_warn_duration = Legion::Settings[:data][:connection][:log_warn_duration]
        end

        def shutdown
          @sequel.disconnect
        end

        def creds_builder # rubocop:disable Metrics/AbcSize
          final_creds = {}
          final_creds.merge! default_creds
          final_creds.merge! Legion::Settings[:data][:creds]
          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

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

        def adapter
          'mysql2'
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
legion-data-0.2.0 lib/legion/data/connection.rb