Sha256: 262b3a5748c956d8fd67d1efc9fd853c03d0828205f17e7e6dabe9f50f1950ff

Contents?: true

Size: 1.21 KB

Versions: 9

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true
require 'active_support'
require 'active_support/core_ext'

module FreshConnection
  class ConnectionSpecification
    def initialize(spec_name, modify_spec: nil)
      @spec_name = spec_name.to_s
      @modify_spec = modify_spec.with_indifferent_access if modify_spec
    end

    def spec
      resolver.spec(@spec_name.to_sym)
    end

    private

    def resolver
      ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(@spec_name => build_config)
    end

    def build_config
      config = base_config.with_indifferent_access

      s_config = replica_config(config)
      config = config.merge(s_config) if s_config

      config = config.merge(@modify_spec) if defined?(@modify_spec)
      config
    end

    def replica_config(config)
      if database_group_url
        config_from_url
      else
        config[@spec_name]
      end
    end

    def config_from_url
      ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(database_group_url).to_hash
    end

    def base_config
      ActiveRecord::Base.connection_pool.spec.config
    end

    def database_group_url
      ENV["DATABASE_#{@spec_name.upcase}_URL"]
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
fresh_connection-3.0.3 lib/fresh_connection/connection_specification.rb
fresh_connection-3.0.2 lib/fresh_connection/connection_specification.rb
fresh_connection-3.0.2.rc1 lib/fresh_connection/connection_specification.rb
fresh_connection-3.0.1 lib/fresh_connection/connection_specification.rb
fresh_connection-3.0.1.rc2 lib/fresh_connection/connection_specification.rb
fresh_connection-3.0.1.rc1 lib/fresh_connection/connection_specification.rb
fresh_connection-3.0.0 lib/fresh_connection/connection_specification.rb
fresh_connection-3.0.0.rc2 lib/fresh_connection/connection_specification.rb
fresh_connection-3.0.0.rc1 lib/fresh_connection/connection_specification.rb