Class: PostgresSession
- Inherits:
-
Object
- Object
- PostgresSession
- Defined in:
- lib/resources/postgres_session.rb
Instance Method Summary collapse
-
#initialize(user, pass) ⇒ PostgresSession
constructor
A new instance of PostgresSession.
- #query(query, db = [], &block) ⇒ Object
Constructor Details
#initialize(user, pass) ⇒ PostgresSession
Returns a new instance of PostgresSession.
37 38 39 40 |
# File 'lib/resources/postgres_session.rb', line 37 def initialize(user, pass) @user = user || 'postgres' @pass = pass end |
Instance Method Details
#query(query, db = [], &block) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/resources/postgres_session.rb', line 42 def query(query, db = [], &block) dbs = db.map { |x| "-d #{x}" }.join(' ') # TODO: simple escape, must be handled by a library # that does this securely escaped_query = query.gsub(/\\/, '\\\\').gsub(/"/, '\\"').gsub(/\$/, '\\$') # run the query cmd = inspec.command("PGPASSWORD='#{@pass}' psql -U #{@user} #{dbs} -c \"#{escaped_query}\"") out = cmd.stdout + "\n" + cmd.stderr if out =~ /could not connect to .*/ or out.downcase =~ /^error/ # skip this test if the server can't run the query RSpec.describe(cmd) do it 'is skipped', skip: out do end end else # remove the whole header (i.e. up to the first ^-----+------+------$) # remove the tail lines = cmd.stdout .sub(/(.*\n)+([-]+[+])*[-]+\n/, '') .sub(/\n[^\n]*\n\n$/, '') l = Lines.new(lines.strip, "PostgreSQL query: #{query}") RSpec.__send__('describe', l, &block) end end |