Class: Postgresql

Inherits:
Rehabilitate::Plugin show all
Defined in:
lib/rehabilitate/plugins/postgresql.rb

Instance Method Summary collapse

Methods inherited from Rehabilitate::Plugin

derivative_dirs, #log

Instance Method Details

#backup(options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rehabilitate/plugins/postgresql.rb', line 4

def backup(options)
  options._backup_files.collect! do |backup_file|
    new_backup_name = "#{backup_file}.sql"
    log "Backing up database #{options.database}"
    log %[pg_dump -h #{options.host} -U #{options.user} #{options.database} > #{new_backup_name}]
    log %x[pg_dump -h #{options.host} -U #{options.user} #{options.database} > #{new_backup_name}]
    options._failure = true if $? == 256
    options._tmp_files << new_backup_name
    new_backup_name
  end
end

#restore(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rehabilitate/plugins/postgresql.rb', line 16

def restore(options)
  options._backup_files.collect! do |backup_file|
    log "Restoring database #{options.database}"
    drop_table_sql =  File.join(options.tmp, 'droptables.sql')
    log %[psql -t -h #{options.host} -U #{options.user} -d #{options.database} -c "SELECT 'DROP TABLE ' || n.nspname || '.' || c.relname || ' CASCADE;' FROM pg_catalog.pg_class AS c LEFT JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace WHERE relkind = 'r' AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_catalog.pg_table_is_visible(c.oid)" > #{drop_table_sql} ]
    log %x[psql -t -h #{options.host} -U #{options.user} -d #{options.database} -c "SELECT 'DROP TABLE ' || n.nspname || '.' || c.relname || ' CASCADE;' FROM pg_catalog.pg_class AS c LEFT JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace WHERE relkind = 'r' AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_catalog.pg_table_is_visible(c.oid)" > #{drop_table_sql} ]
    log %[psql -h #{options.host} -U #{options.user} #{options.database} < #{drop_table_sql} #{"> /dev/null 2>&1" unless options.debug} ] unless $? == 256
    log %x[psql -h #{options.host} -U #{options.user} #{options.database} < #{drop_table_sql} #{"> /dev/null 2>&1" unless options.debug} ] unless $? == 256
    log %[psql -h #{options.host} -U #{options.user} #{options.database} < #{backup_file} #{"> /dev/null 2>&1" unless options.debug} ] unless $? == 256
    log %x[psql -h #{options.host} -U #{options.user} #{options.database} < #{backup_file} #{"> /dev/null 2>&1" unless options.debug} ] unless $? == 256
    options._tmp_files << drop_table_sql
    options._failure = true if $? == 256
  end
end