class SchemaEvolutionManager::ConnectionData

Constants

DEFAULT_PORT

Attributes

host[R]
name[R]
port[R]
user[R]

Public Class Methods

new(host, name, opts={}) click to toggle source
# File lib/schema-evolution-manager/connection_data.rb, line 9
def initialize(host, name, opts={})
  @host = host
  @name = name

  port = opts.delete(:port).to_s
  if port.to_s.empty?
    @port = DEFAULT_PORT
  else
    @port = port.to_i
  end
  Preconditions.check_argument(@port > 0, "Port must be > 0")

  @user = opts.delete(:user)
  Preconditions.assert_empty_opts(opts)
end
parse_url(url) click to toggle source

Parses a connection string into a ConnectionData instance. You will get an error if the URL could not be parsed.

@param url e.g. postgres://user1@db.com:5553/test_db

# File lib/schema-evolution-manager/connection_data.rb, line 36
def ConnectionData.parse_url(url)
  protocol, rest = url.split("//", 2)
  if rest.nil?
    raise "Invalid url[%s]. Expected to start with postgres://" % url
  end
  
  lead, name = rest.split("/", 2)
  if name.nil?
    raise "Invalid url[%s]. Missing database name" % url
  end

  parts = lead.split("@", 2)
  if parts.size == 2
    user = parts[0]
    db_host = parts[1]
  else
    user = nil
    db_host = lead
  end

  host, port = db_host.split(":", 2)
  if port
    if port.to_i.to_s != port
      raise "Invalid url[%s]. Expected database port[%s] to be an integer" % [url, port]
    end
  end

  ConnectionData.new(host, name, :user => user, :port => port)
end

Public Instance Methods

pgpass(password=nil) click to toggle source

Returns a valid pgpass line entry representing this connection.

@param password: Optional password to include in the connection string

# File lib/schema-evolution-manager/connection_data.rb, line 28
def pgpass(password=nil)
  [@host, @port, @name, @user, password.to_s].join(":")
end