lib/legion/data/connection.rb in legion-data-0.2.0 vs lib/legion/data/connection.rb in legion-data-1.1.2

- old
+ new

@@ -2,37 +2,39 @@ 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 + attr_accessor :sequel + + def adapter + @adapter ||= RUBY_ENGINE == 'jruby' ? :jdbc : :mysql2 end def setup - @sequel = ::Sequel.connect(adapter: adapter, **creds_builder) + @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 unless Legion::Settings[:data][:connection][:log] + return if Legion::Settings[:data][:connection].nil? || Legion::Settings[:data][:connection][:log].nil? - @sequel.logger = Legion::Logging::Logger.new(level: 'debug') + @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 def shutdown - @sequel.disconnect + @sequel&.disconnect + Legion::Settings[:data][:connected] = false end - def creds_builder # rubocop:disable Metrics/AbcSize + def creds_builder final_creds = {} final_creds.merge! default_creds - final_creds.merge! Legion::Settings[:data][:creds] + final_creds.merge! Legion::Settings[:data][:creds] if Legion::Settings[:data][:creds].is_a? Hash 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] @@ -50,13 +52,9 @@ password: 'legion', database: 'legion', max_connections: 32, preconnect: 'concurrently' } - end - - def adapter - 'mysql2' end end end end end