Class: PgBundle::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/pgbundle/database.rb

Overview

The Database class defines on which database the extensions should be installed Note to install an extension the code must be compiled on the database server on a typical environment ssh access is needed if the database host differs from the Pgfile host

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Database

Returns a new instance of Database.



11
12
13
14
15
16
17
18
19
# File 'lib/pgbundle/database.rb', line 11

def initialize(name, opts = {})
  @name        = name
  @user        = opts[:user]        || 'postgres'
  @host        = opts[:host]        || 'localhost'
  @use_sudo    = opts[:use_sudo]    || false
  @system_user = opts[:system_user] || 'postgres'
  @port        = opts[:port]        || 5432
  @force_ssh   = opts[:force_ssh]   || false
end

Instance Attribute Details

#force_sshObject

Returns the value of attribute force_ssh.



10
11
12
# File 'lib/pgbundle/database.rb', line 10

def force_ssh
  @force_ssh
end

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/pgbundle/database.rb', line 10

def host
  @host
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/pgbundle/database.rb', line 10

def name
  @name
end

#portObject

Returns the value of attribute port.



10
11
12
# File 'lib/pgbundle/database.rb', line 10

def port
  @port
end

#system_userObject

Returns the value of attribute system_user.



10
11
12
# File 'lib/pgbundle/database.rb', line 10

def system_user
  @system_user
end

#use_sudoObject

Returns the value of attribute use_sudo.



10
11
12
# File 'lib/pgbundle/database.rb', line 10

def use_sudo
  @use_sudo
end

#userObject

Returns the value of attribute user.



10
11
12
# File 'lib/pgbundle/database.rb', line 10

def user
  @user
end

Instance Method Details

#connectionObject



21
22
23
24
25
# File 'lib/pgbundle/database.rb', line 21

def connection
  @connection ||= begin
    PG.connect(dbname: name, user: user, host: host, port: port)
  end
end

#current_definitionObject

returns currently installed extensions



80
81
82
# File 'lib/pgbundle/database.rb', line 80

def current_definition
  result = execute('SELECT name, version, requires FROM pg_available_extension_versions WHERE installed').to_a
end

#drop_extension(name) ⇒ Object



71
72
73
# File 'lib/pgbundle/database.rb', line 71

def drop_extension(name)
  execute "DROP EXTENSION IF EXISTS #{name}"
end

#execute(sql) ⇒ Object Also known as: exec

executes the given sql on the database connections redirects all noise to /dev/null



29
30
31
32
33
# File 'lib/pgbundle/database.rb', line 29

def execute(sql)
  silence do
    connection.exec sql
  end
end

#load_destination(ext_name) ⇒ Object



75
76
77
# File 'lib/pgbundle/database.rb', line 75

def load_destination(ext_name)
  "/tmp/pgbundle/#{ext_name}"
end

#make_install(source, ext_name) ⇒ Object

loads the source, runs make install and removes the source afterwards



55
56
57
58
59
60
61
# File 'lib/pgbundle/database.rb', line 55

def make_install(source, ext_name)
  run("mkdir -p -m 0777 /tmp/pgbundle/")
  remove_source(ext_name)
  source.load(host, system_user, load_destination(ext_name))
  run(make_install_cmd(ext_name))
  remove_source(ext_name)
end

#make_uninstall(source, ext_name) ⇒ Object

loads the source and runs make uninstall



64
65
66
67
68
69
# File 'lib/pgbundle/database.rb', line 64

def make_uninstall(source, ext_name)
  remove_source(ext_name)
  source.load(host, system_user, load_destination(ext_name))
  run(make_uninstall_cmd(ext_name))
  remove_source(ext_name)
end

#transaction(&block) ⇒ Object



37
38
39
40
41
# File 'lib/pgbundle/database.rb', line 37

def transaction(&block)
  silence do
    connection.transaction(&block)
  end
end

#transaction_rollback(&block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/pgbundle/database.rb', line 43

def transaction_rollback(&block)
  silence do
    connection.transaction do |con|
      yield con
      fail TransactionRollback
    end
  end

  rescue TransactionRollback
end