Class: Backup::Database::PostgreSQL

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

Constant Summary

Constants included from CLI::Helpers

CLI::Helpers::UTILITY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration::Helpers

included

Constructor Details

#initialize(model, &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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/backup/database/postgresql.rb', line 43

def initialize(model, &block)
  super(model)

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

  instance_eval(&block) if block_given?

  @pg_dump_utility ||= utility(:pg_dump)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Backup::Configuration::Helpers

Instance Attribute Details

#additional_options ⇒ Object

Additional "pg_dump" options



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

def additional_options
  @additional_options
end

#host ⇒ Object

Connectivity options



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

def host
  @host
end

#name ⇒ Object

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_tables ⇒ Object

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

#password ⇒ Object

Credentials for the specified database



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

def password
  @password
end

#pg_dump_utility ⇒ Object

Path to pg_dump utility (optional)



33
34
35
# File 'lib/backup/database/postgresql.rb', line 33

def pg_dump_utility
  @pg_dump_utility
end

#port ⇒ Object

Connectivity options



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

def port
  @port
end

#skip_tables ⇒ Object

Tables to skip while dumping the database



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

def skip_tables
  @skip_tables
end

#socket ⇒ Object

Connectivity options



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

def socket
  @socket
end

#username ⇒ Object

Credentials for the specified database



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

def username
  @username
end

Instance Method Details

#connectivity_options ⇒ Object

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



110
111
112
113
114
115
# File 'lib/backup/database/postgresql.rb', line 110

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

#password_options ⇒ Object

Builds the password syntax PostgreSQL uses to authenticate the user to perform database dumping



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

def password_options
  password.to_s.empty? ? '' : "PGPASSWORD='#{password}' "
end

#perform! ⇒ Object

Performs the pgdump command and outputs the data to the specified path based on the 'trigger'



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/backup/database/postgresql.rb', line 58

def perform!
  super

  pipeline = Pipeline.new
  dump_ext = 'sql'

  pipeline << pgdump
  if @model.compressor
    @model.compressor.compress_with do |command, ext|
      pipeline << command
      dump_ext << ext
    end
  end
  pipeline << "cat > '#{ File.join(@dump_path, name) }.#{ dump_ext }'"

  pipeline.run
  if pipeline.success?
    Logger.message "#{ database_name } Complete!"
  else
    raise Errors::Database::PipelineError,
        "#{ database_name } Dump Failed!\n" +
        pipeline.error_messages
  end
end

#pgdump ⇒ Object

Builds the full pgdump string based on all attributes



85
86
87
88
89
# File 'lib/backup/database/postgresql.rb', line 85

def pgdump
  "#{password_options}" +
  "#{ pg_dump_utility } #{ username_options } #{ connectivity_options } " +
  "#{ user_options } #{ tables_to_dump } #{ tables_to_skip } #{ name }"
end

#tables_to_dump ⇒ Object

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



127
128
129
130
131
# File 'lib/backup/database/postgresql.rb', line 127

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

#tables_to_skip ⇒ Object

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



136
137
138
139
140
# File 'lib/backup/database/postgresql.rb', line 136

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

#user_options ⇒ Object

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



120
121
122
# File 'lib/backup/database/postgresql.rb', line 120

def user_options
  additional_options.join(' ')
end

#username_options ⇒ Object

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



101
102
103
# File 'lib/backup/database/postgresql.rb', line 101

def username_options
  username.to_s.empty? ? '' : "--username='#{username}'"
end