Class: Backup::Database::PostgreSQL

Inherits:
Base
  • Object
show all
Defined in:
lib/backup/database/postgresql.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#dump_path, #utility_path

Instance Method Summary collapse

Methods inherited from Base

#log!, #prepare!

Methods included from Configuration::Helpers

#clear_defaults!, #getter_methods, #load_defaults!, #setter_methods

Methods included from CLI

#mkdir, #rm, #run, #utility

Constructor Details

#initialize(&block) ⇒ PostgreSQL

Creates a new instance of the PostgreSQL adapter object Sets the PGPASSWORD environment variable to the password so it doesn’t prompt and hang in the process



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/backup/database/postgresql.rb', line 35

def initialize(&block)
  load_defaults!

  @skip_tables        ||= Array.new
  @only_tables        ||= Array.new
  @additional_options ||= Array.new

  instance_eval(&block)
  prepare!
  ENV['PGPASSWORD'] = password
end

Instance Attribute Details

#additional_optionsObject

Additional “pg_dump” options



29
30
31
# File 'lib/backup/database/postgresql.rb', line 29

def additional_options
  @additional_options
end

#hostObject

Connectivity options



17
18
19
# File 'lib/backup/database/postgresql.rb', line 17

def host
  @host
end

#nameObject

Name of the database that needs to get dumped



9
10
11
# File 'lib/backup/database/postgresql.rb', line 9

def name
  @name
end

#only_tablesObject

Tables to dump, tables that aren’t specified won’t get dumped



25
26
27
# File 'lib/backup/database/postgresql.rb', line 25

def only_tables
  @only_tables
end

#passwordObject

Credentials for the specified database



13
14
15
# File 'lib/backup/database/postgresql.rb', line 13

def password
  @password
end

#portObject

Connectivity options



17
18
19
# File 'lib/backup/database/postgresql.rb', line 17

def port
  @port
end

#skip_tablesObject

Tables to skip while dumping the database



21
22
23
# File 'lib/backup/database/postgresql.rb', line 21

def skip_tables
  @skip_tables
end

#socketObject

Connectivity options



17
18
19
# File 'lib/backup/database/postgresql.rb', line 17

def socket
  @socket
end

#usernameObject

Credentials for the specified database



13
14
15
# File 'lib/backup/database/postgresql.rb', line 13

def username
  @username
end

Instance Method Details

#connectivity_optionsObject

Builds the PostgreSQL connectivity options syntax to connect the user to perform the database dumping process, socket gets gsub’d to host since that’s the option PostgreSQL takes for socket connections as well. In case both the host and the socket are specified, the socket will take priority over the host



78
79
80
81
82
83
# File 'lib/backup/database/postgresql.rb', line 78

def connectivity_options
  %w[host port socket].map do |option|
    next if send(option).nil? or (send(option).respond_to?(:empty?) and send(option).empty?)
    "--#{option}='#{send(option)}'".gsub('--socket=', '--host=')
  end.compact.join("\s")
end

#credential_optionsObject

Builds the credentials PostgreSQL syntax to authenticate the user to perform the database dumping process



68
69
70
71
# File 'lib/backup/database/postgresql.rb', line 68

def credential_options
  return '' unless username.is_a?(String) and not username.empty?
  "--username='#{username}'"
end

#optionsObject

Builds a PostgreSQL compatible string for the additional options specified by the user



88
89
90
# File 'lib/backup/database/postgresql.rb', line 88

def options
  additional_options.join("\s")
end

#perform!Object

Performs the pgdump command and outputs the data to the specified path based on the ‘trigger’ and resets the ‘PGPASSWORD’ environment variable to nil



103
104
105
106
107
# File 'lib/backup/database/postgresql.rb', line 103

def perform!
  log!
  run("#{pgdump} > '#{File.join(dump_path, name)}.sql'")
  ENV['PGPASSWORD'] = nil
end

#pgdumpObject

Builds the full pgdump string based on all attributes



94
95
96
97
# File 'lib/backup/database/postgresql.rb', line 94

def pgdump
  "#{ utility(:pg_dump) } #{ credential_options } #{ connectivity_options } " +
  "#{ options } #{ tables_to_dump } #{ tables_to_skip } #{ name }"
end

#tables_to_dumpObject

Builds the PostgreSQL syntax for specifying which tables to dump during the dumping of the database



59
60
61
62
63
# File 'lib/backup/database/postgresql.rb', line 59

def tables_to_dump
  only_tables.map do |table|
    "--table='#{table}'"
  end.join("\s")
end

#tables_to_skipObject

Builds the PostgreSQL syntax for specifying which tables to skip during the dumping of the database



50
51
52
53
54
# File 'lib/backup/database/postgresql.rb', line 50

def tables_to_skip
  skip_tables.map do |table|
    "--exclude-table='#{table}'"
  end.join("\s")
end