Class: Tetrahedron::Databases::Postgres

Inherits:
Tetrahedron::Database::Provider show all
Defined in:
lib/tetrahedron/databases/postgres.rb

Defined Under Namespace

Classes: Configuration

Instance Method Summary collapse

Constructor Details

#initialize(&configurator) ⇒ Postgres

Returns a new instance of Postgres.



13
14
15
16
# File 'lib/tetrahedron/databases/postgres.rb', line 13

def initialize(&configurator)
  @configuration = Configuration.new
  @configuration.instance_eval(&configurator) if block_given?
end

Instance Method Details

#connectObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tetrahedron/databases/postgres.rb', line 22

def connect
  # Wait some amount of time before assuming the database is down.
  Service.wait_until_reachable!(:protocol => :tcp,
                                :host => @configuration.host,
                                :port => @configuration.port,
                                :timeout => 15)

  @connection = Sequel.postgres(:host => @configuration.host,
                                :port => @configuration.port,
                                :user => @configuration.user,
                                :password => @configuration.password,
                                :database => @configuration.database,
                                :test => true,
                                :sslmode => :prefer,
                                :max_connections => @configuration.pool)

  true
end

#connectionObject



18
19
20
# File 'lib/tetrahedron/databases/postgres.rb', line 18

def connection
  @connection
end

#disconnectObject



41
42
43
44
45
# File 'lib/tetrahedron/databases/postgres.rb', line 41

def disconnect
  @connection.disconnect if @connection
  @connection = nil
  true
end