Class: DbAgent::DbHandler::PostgreSQL

Inherits:
DbAgent::DbHandler show all
Defined in:
lib/db_agent/db_handler/postgresql.rb

Instance Attribute Summary

Attributes inherited from DbAgent::DbHandler

#backup_folder, #config, #data_folder, #migrations_folder, #schema_folder, #superconfig, #viewpoints_folder

Instance Method Summary collapse

Methods inherited from DbAgent::DbHandler

factor, #initialize, #migrate, #ping, #require_viewpoints!, #sequel_db, #sequel_superdb, #system, #wait, #wait_server

Constructor Details

This class inherits a constructor from DbAgent::DbHandler

Instance Method Details

#backupObject



15
16
17
18
# 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

#createObject



5
6
7
8
# 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

#dropObject



10
11
12
13
# File 'lib/db_agent/db_handler/postgresql.rb', line 10

def drop
  shell pg_cmd("dropdb", config[:database]),
  pg_cmd("dropuser", config[:user])
end

#replObject



20
21
22
# File 'lib/db_agent/db_handler/postgresql.rb', line 20

def repl
  shell pg_cmd('psql', config[:database])
end

#restore(t, args) ⇒ Object



38
39
40
41
42
43
44
45
# 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

#spyObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# 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