Sha256: 4869ef3bf2892faed4fad7cfc529b0d51724664a36a6adf0ab32afe507a2de1e

Contents?: true

Size: 2 KB

Versions: 2

Compression:

Stored size: 2 KB

Contents

module Departure
  # Holds the parameters of the DB connection and formats them to string
  class ConnectionDetails

    DEFAULT_PORT = 3306
    # Constructor
    #
    # @param [Hash] connection parametes as used in #establish_conneciton
    def initialize(connection_data)
      @connection_data = connection_data
    end

    # Returns the details formatted as an string to be used with
    # pt-online-schema-change. It follows the mysql client's format.
    #
    # @return [String]
    def to_s
      @to_s ||= "-h #{host} -P #{port} -u #{user} #{password_argument}"
    end

    # TODO: Doesn't the abstract adapter already handle this somehow?
    # Returns the database name. If PERCONA_DB_NAME is passed its value will be
    # used instead
    #
    # Returns the database name
    #
    # @return [String]
    def database
      ENV.fetch('PERCONA_DB_NAME', connection_data[:database])
    end

    # Returns the password fragment of the details string if a password is passed
    #
    # @return [String]
    def password_argument
      if password.present?
        %Q[--password "#{password}" ]
      else
        ''
      end
    end

    private

    attr_reader :connection_data

    # Returns the database host name, defaulting to localhost. If PERCONA_DB_HOST
    # is passed its value will be used instead
    #
    # @return [String]
    def host
      ENV.fetch('PERCONA_DB_HOST', connection_data[:host]) || 'localhost'
    end

    # Returns the database user. If PERCONA_DB_USER is passed its value will be
    # used instead
    #
    # @return [String]
    def user
      ENV.fetch('PERCONA_DB_USER', connection_data[:username])
    end

    # Returns the database user's password. If PERCONA_DB_PASSWORD is passed its
    # value will be used instead
    #
    # @return [String]
    def password
      ENV.fetch('PERCONA_DB_PASSWORD', connection_data[:password])
    end

    # Returns the database's port.
    #
    # @return [String]
    def port
      connection_data.fetch(:port, DEFAULT_PORT)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
departure-2.0.1 lib/departure/connection_details.rb
departure-3.0.1 lib/departure/connection_details.rb