class DbAgent::DbHandler::PostgreSQL

Public Instance Methods

backup() click to toggle source
# File lib/db_agent/db_handler/postgresql.rb, line 15
def backup
  datetime = Time.now.strftime("%Y%m%dT%H%M%S")
  shell pg_dump("--clean", config[:database], "> #{backup_folder}/backup-#{datetime}.sql")
end
create() click to toggle source
# File lib/db_agent/db_handler/postgresql.rb, line 5
def create
  shell pg_cmd("createuser","--no-createdb","--no-createrole","--no-superuser","--no-password",config[:user]),
  pg_cmd("createdb","--owner=#{config[:user]}", config[:database])
end
drop() click to toggle source
# File lib/db_agent/db_handler/postgresql.rb, line 10
def drop
  shell pg_cmd("dropdb", config[:database]),
  pg_cmd("dropuser", config[:user])
end
repl() click to toggle source
# File lib/db_agent/db_handler/postgresql.rb, line 20
def repl
  shell pg_cmd('psql', config[:database])
end
restore(t, args) click to toggle source
# File lib/db_agent/db_handler/postgresql.rb, line 38
def restore(t, args)
  candidates = backup_folder.glob("*.sql").sort
  if args[:pattern] && rx = Regexp.new(args[:pattern])
    candidates = candidates.select{|f| f.basename.to_s =~ rx }
  end
  file = candidates.last
  shell pg_cmd('psql', config[:database], '<', file.to_s)
end
spy() click to toggle source
# File lib/db_agent/db_handler/postgresql.rb, line 24
def spy
  spy_jar = DbAgent._!('vendor').glob('schema*.jar').first
  jdbc_jar = DbAgent._!('vendor').glob('postgresql*.jar').first
  cmd = ""
  cmd << %Q{java -jar #{spy_jar} -dp #{jdbc_jar} -t pgsql}
  cmd << %Q{ -host #{config[:host]}}
  cmd << %Q{ -port #{config[:port]}} if config[:port]
  cmd << %Q{ -u #{config[:user]}}
  cmd << %Q{ -p #{config[:password]}} if config[:password]
  cmd << %Q{ -db #{config[:database]} -s public -o #{schema_folder}/spy}
  system(cmd)
  system %Q{open #{schema_folder}/spy/index.html}
end

Private Instance Methods

pg_cmd(cmd, *args) click to toggle source
# File lib/db_agent/db_handler/postgresql.rb, line 49
def pg_cmd(cmd, *args)
  %Q{#{cmd} -h #{config[:host]} -p #{config[:port]} -U #{config[:user]} #{args.join(' ')}}
end
pg_dump(*args) click to toggle source
# File lib/db_agent/db_handler/postgresql.rb, line 59
def pg_dump(*args)
  cmd = "pg_dump"
  cmd = "PGPASSWORD=#{config[:password]} #{cmd}" if config[:password]
  pg_cmd(cmd, *args)
end
psql(*args) click to toggle source
# File lib/db_agent/db_handler/postgresql.rb, line 53
def psql(*args)
  cmd = "psql"
  cmd = "PGPASSWORD=#{config[:password]} #{cmd}" if config[:password]
  pg_cmd(cmd, *args)
end