Sha256: 895caa8cf97d96ab2e2e222403e6787425f2130ae9dab1da01d581c87c59789f

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

Stored size: 1.17 KB

Contents

module Backhoe
  class Database
    def initialize config=load_config
      @config = config
    end

    attr_reader :config 

    def current_environment_name
      [
        defined?(Rails) && Rails.env,
        ENV["RAILS_ENV"],
        "development",
      ].find(&:itself)
    end

    def to_mysql_options
      options =  " -u #{config["username"]}"
      options += " -p'#{config["password"]}'" if config["password"]
      options += " -h #{config["host"]}"      if config["host"]
      options += " -S #{config["socket"]}"    if config["socket"]
      options
    end

    def name
      config["database"]
    end

    def adapter
      config["adapter"]
    end

    def postgresql?
      config["adapter"] == "postgresql"
    end

    def mysql?
      %w[mysql2 trilogy].include?(config["adapter"])
    end

    private

    def load_config
      configs = ActiveRecord::Base.configurations
      config = configs.configs_for(env_name: current_environment_name).first
      hash = if config.respond_to?(:configuration_hash)
        config.configuration_hash # rails 7
      else
        config.config # rails 6
      end
      HashWithIndifferentAccess.new(hash)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
backhoe-0.10.1 lib/backhoe/database.rb
backhoe-0.10.0 lib/backhoe/database.rb
backhoe-0.9.0 lib/backhoe/database.rb
backhoe-0.8.3 lib/backhoe/database.rb
backhoe-0.8.2 lib/backhoe/database.rb
backhoe-0.8.0 lib/backhoe/database.rb