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
20
# 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
  @slave       = opts[:slave]       || 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



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

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

#current_definitionObject

returns currently installed extensions



90
91
92
# File 'lib/pgbundle/database.rb', line 90

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

#drop_extension(name) ⇒ Object



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

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



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

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

#load_destination(ext_name) ⇒ Object



85
86
87
# File 'lib/pgbundle/database.rb', line 85

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

#make_install(source, ext_name, flags) ⇒ Object

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



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

def make_install(source, ext_name, flags)
  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, flags))
  remove_source(ext_name)
  source.clean
end

#make_uninstall(source, ext_name, flags) ⇒ Object

loads the source and runs make uninstall



73
74
75
76
77
78
79
# File 'lib/pgbundle/database.rb', line 73

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

#slave?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/pgbundle/database.rb', line 32

def slave?
  @slave
end

#to_sObject



28
29
30
# File 'lib/pgbundle/database.rb', line 28

def to_s
  "host: #{@host}:#{port} db: #{@name}"
end

#transaction(&block) ⇒ Object



45
46
47
48
49
# File 'lib/pgbundle/database.rb', line 45

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

#transaction_rollback(&block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/pgbundle/database.rb', line 51

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

  rescue TransactionRollback
end