Sha256: bfe05e47279483f334468305c6c5f36f0aef338630c529f48e121ac12fe2624b

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module PsqlRunner
  extend self

  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

  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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dblink-0.4.3 lib/dblink/psql_runner.rb
dblink-0.4.2 lib/dblink/psql_runner.rb