Class: Storey::GenDumpCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/storey/gen_dump_command.rb

Class Method Summary collapse

Class Method Details

.call(database_url: Storey.configuration.database_url, database: nil, host: nil, username: nil, structure_only: false, file: nil, schemas: nil, password: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/storey/gen_dump_command.rb', line 4

def self.call(
  database_url: Storey.configuration.database_url,
  database: nil,
  host: nil,
  username: nil,
  structure_only: false,
  file: nil,
  schemas: nil,
  password: nil
)
  switches = {}

  if database_url.nil?
    if database.blank?
      raise ArgumentError, 'database must be supplied'
    end

    switches['host'] = host if host.present?
    switches['username'] = username if username.present?
  end

  switches['schema-only'] = nil if structure_only
  switches['no-privileges'] = nil
  switches['no-owner'] = nil
  switches[:file] = Shellwords.escape(file)

  if schemas
    schemas = schemas.split(',')
    schemas_switches = schemas.map do |part|
      Utils.command_line_switches_from({schema: Shellwords.escape(part) })
    end
  end

  command_parts = []
  if password.present?
    command_parts << "PGPASSWORD=#{password}"
  end
  command_parts << "pg_dump"
  command_parts << database_url if database_url.present?
  command_parts += [
    Utils.command_line_switches_from(switches),
    schemas_switches,
  ]
  command_parts << database if database_url.blank?
  command_parts.compact.join(' ')
end