Module: PsqlRunner

Extended by:
PsqlRunner
Included in:
PsqlRunner
Defined in:
lib/dblink/psql_runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_psqlObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dblink/psql_runner.rb', line 36

def self.find_psql
  path = `which psql`

  if path != ''
    path = 'psql'
  elsif RUBY_PLATFORM =~ /darwin/
    paths = Dir.glob('/Applications/Postgres.app/Contents/Versions/**/psql')
    path = paths.last.strip if paths.size > 0
  end

  path == '' ? nil : path
end

Instance Method Details

#check_connection(opt = {}) ⇒ 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
# File 'lib/dblink/psql_runner.rb', line 4

def check_connection(opt = {})
  psql_path = find_psql

  unless psql_path
    result = "Can not find #{'psql'.bold} in your system\n"
    if RUBY_PLATFORM =~ /darwin/
      result += "Please run 'brew install postgres' or download it from http://postgresapp.com"
    else
      result += "Please install postgresql package in your system"
    end
    result += "\nOr disable connection checking (--no-check-local and --no-check-remote)"

    return result
  end

  passwd = opt[:password] && opt[:password] == '' ? '' : "PGPASSWORD=#{opt[:password]}"

  psql_command = "#{passwd} #{psql_path} -h #{opt[:host]} -p #{opt[:port]} -U #{opt[:user]} #{opt[:database]} -c 'select now()'"

  if opt[:verbose]
    puts "EXEC #{psql_command}"
  end

  result = %x(#{psql_command} 2>&1)

  #unless result.include?('(1 row)')
  #  puts result
  #end

  result.include?('(1 row)') ? true : result
end