class PgBundle::Database

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

Attributes

force_ssh[RW]
host[RW]
name[RW]
password[RW]
port[RW]
system_user[RW]
use_sudo[RW]
user[RW]

Public Class Methods

new(name, opts = {}) click to toggle source
# File lib/pgbundle/database.rb, line 12
def initialize(name, opts = {})
  @name        = name
  @user        = opts[:user]        || 'postgres'
  @password    = opts[:password]
  @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

Public Instance Methods

connection() click to toggle source
# File lib/pgbundle/database.rb, line 24
def connection
  @connection ||= begin
    PG.connect(connection_opts)
  end
end
connection_opts() click to toggle source
# File lib/pgbundle/database.rb, line 30
def connection_opts
  {
    dbname: name,
    user: user,
    password: password,
    host: host,
    port: port
  }.compact
end
current_definition() click to toggle source

returns currently installed extensions

# File lib/pgbundle/database.rb, line 102
def current_definition
  result = execute('SELECT name, version, requires FROM pg_available_extension_versions WHERE installed').to_a
end
drop_extension(name) click to toggle source
# File lib/pgbundle/database.rb, line 93
def drop_extension(name)
  execute "DROP EXTENSION IF EXISTS #{name}"
end
exec(sql)
Alias for: execute
execute(sql) click to toggle source

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

# File lib/pgbundle/database.rb, line 49
def execute(sql)
  silence do
    connection.exec sql
  end
end
Also aliased as: exec
load_destination(ext_name) click to toggle source
# File lib/pgbundle/database.rb, line 97
def load_destination(ext_name)
  "/tmp/pgbundle/#{ext_name}"
end
make_install(source, ext_name, flags) click to toggle source

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

# File lib/pgbundle/database.rb, line 75
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) click to toggle source

loads the source and runs make uninstall

# File lib/pgbundle/database.rb, line 85
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?() click to toggle source
# File lib/pgbundle/database.rb, line 44
def slave?
  @slave
end
to_s() click to toggle source
# File lib/pgbundle/database.rb, line 40
def to_s
  "host: #{@host}:#{port} db: #{@name}"
end
transaction(&block) click to toggle source
# File lib/pgbundle/database.rb, line 57
def transaction(&block)
  silence do
    connection.transaction(&block)
  end
end
transaction_rollback() { |con| ... } click to toggle source
# File lib/pgbundle/database.rb, line 63
def transaction_rollback(&block)
  silence do
    connection.transaction do |con|
      yield con
      fail TransactionRollback
    end
  end

  rescue TransactionRollback
end

Private Instance Methods

local(cmd) click to toggle source
# File lib/pgbundle/database.rb, line 137
def local(cmd)
  res = %x((#{cmd}) 2>&1 )
  raise res unless $?.success?
end
make_install_cmd(name, flags) click to toggle source
# File lib/pgbundle/database.rb, line 116
    def make_install_cmd(name, flags)
      <<-CMD.gsub(/\s+/, ' ').strip
        cd #{load_destination(name)} &&
        #{sudo} make #{flags} clean &&
        #{sudo} make #{flags} &&
        #{sudo} make #{flags} install
      CMD
    end
make_uninstall_cmd(name, flags) click to toggle source
# File lib/pgbundle/database.rb, line 125
def make_uninstall_cmd(name, flags)
  "cd #{load_destination(name)} && #{sudo} make #{flags} uninstall"
end
remote(cmd) click to toggle source
# File lib/pgbundle/database.rb, line 142
def remote(cmd)
  Net::SSH.start(host, system_user) do |ssh|
    ssh.exec cmd
  end
end
remove_source(name) click to toggle source
# File lib/pgbundle/database.rb, line 112
def remove_source(name)
  run("rm -rf #{load_destination(name)}")
end
run(cmd) click to toggle source
# File lib/pgbundle/database.rb, line 129
def run(cmd)
  if host == 'localhost' && !force_ssh
    local cmd
  else
    remote cmd
  end
end
silence() { || ... } click to toggle source
# File lib/pgbundle/database.rb, line 149
def silence
  begin
    orig_stderr = $stderr.clone
    orig_stdout = $stdout.clone
    $stderr.reopen File.new('/dev/null', 'w')
    $stdout.reopen File.new('/dev/null', 'w')
    retval = yield
  rescue Exception => e
    $stdout.reopen orig_stdout
    $stderr.reopen orig_stderr
    raise e
  ensure
    $stdout.reopen orig_stdout
    $stderr.reopen orig_stderr
  end
  retval
end
sudo() click to toggle source
# File lib/pgbundle/database.rb, line 108
def sudo
  use_sudo ? 'sudo' : ''
end