Class: PgHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_tools/pg_helper.rb

Class Method Summary collapse

Class Method Details

.run_pg_command(config, cmd, connect_database: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sequel_tools/pg_helper.rb', line 4

def self.run_pg_command(config, cmd, connect_database: nil)
  require 'open3'
  require 'tempfile'
  Tempfile.open 'pgpass' do |file|
    c = config
    file.chmod 0600
    file.write "#{c[:dbhost]}:#{c[:dbport]}:#{c[:dbname]}:#{c[:username]}:#{c[:password]}"
    file.close
    env = {
      'PGDATABASE' => connect_database || c[:dbname],
      'PGHOST' => c[:dbhost],
      'PGPORT' => c[:dbport].to_s,
      'PGUSER' => c[:username],
      'PGPASSFILE' => file.path
    }
    stdout, stderr, status = Open3.capture3 env, cmd
    puts "#{cmd} failed: #{[stderr, stdout].join "\n\n"}" if status != 0
    [ stdout, stderr, status == 0 ]
  end
end