Module: RunPSQL

Defined in:
lib/run_psql.rb

Instance Method Summary collapse

Instance Method Details

#db_file_path(filename) ⇒ Object



2
3
4
# File 'lib/run_psql.rb', line 2

def db_file_path(filename)
  "#{Rails.root}/db/#{filename}"
end

#run_pg_command(config, command, output_file = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/run_psql.rb', line 10

def run_pg_command(config, command, output_file=nil)
  database = config["database"]
  username = config["username"]
  host = config["host"]
  port = config["port"]
  password = config["password"]
  pipe_to_file = output_file ? "> #{output_file}" : ""
  variables = "-v ON_ERROR_STOP=1"
  puts "Executing: #{command} -U #{username} #{host ? '-h '+host : ''} #{port ? '-p '+port.to_s : ''} #{database} #{pipe_to_file}"
  `export PGPASSWORD="#{password}"`
  `#{command} #{variables} -U #{username} #{host ? '-h '+host : ''} #{port ? '-p '+port.to_s : ''} #{database} #{pipe_to_file}`
  exit_status = $?.exitstatus
  `export PGPASSWORD=""`
  puts "exit status: #{exit_status}"
  if exit_status != 0
    raise "pg command failed"
  end
end

#run_psql_file(config, filename) ⇒ Object



5
6
7
8
# File 'lib/run_psql.rb', line 5

def run_psql_file(config, filename)
  file_with_path = db_file_path(filename)
  run_pg_command(config, "psql -f #{file_with_path}")
end