Class: SchemaEvolutionManager::ConnectionData

Inherits:
Object
  • Object
show all
Defined in:
lib/schema-evolution-manager/connection_data.rb

Constant Summary collapse

DEFAULT_PORT =
5432

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, name, opts = {}) ⇒ ConnectionData

Returns a new instance of ConnectionData.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/schema-evolution-manager/connection_data.rb', line 7

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/schema-evolution-manager/connection_data.rb', line 7

def name
  @name
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/schema-evolution-manager/connection_data.rb', line 7

def port
  @port
end

#userObject (readonly)

Returns the value of attribute user.



7
8
9
# File 'lib/schema-evolution-manager/connection_data.rb', line 7

def user
  @user
end

Class Method Details

.parse_url(url) ⇒ Object

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

Parameters:

  • url

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

Instance Method Details

#pgpass(password = nil) ⇒ Object

Returns a valid pgpass line entry representing this connection.

Parameters:

  • password:

    Optional password to include in the connection string



28
29
30
# File 'lib/schema-evolution-manager/connection_data.rb', line 28

def pgpass(password=nil)
  [@host, @port, @name, @user, password.to_s].join(":")
end