Skip to content
Browse files

support -c, like psql

  • Loading branch information...
1 parent f6c11e2 commit 5847c08320d94d1992a14c416f2390cacd3d97ba @freeformz freeformz committed Sep 12, 2013
Showing with 6 additions and 1 deletion.
  1. +6 −1 lib/heroku/command/pg.rb
View
7 lib/heroku/command/pg.rb
@@ -64,6 +64,8 @@ def promote
# pg:psql [DATABASE]
#
+ # -c, --command COMMAND # optional SQL command to run
+ #
# Open a psql shell to the database
#
# defaults to DATABASE_URL databases if no DATABASE is specified
@@ -76,7 +78,10 @@ def psql
begin
ENV["PGPASSWORD"] = uri.password
ENV["PGSSLMODE"] = 'require'
- exec "psql -U #{uri.user} -h #{uri.host} -p #{uri.port || 5432} #{uri.path[1..-1]}"
+ if command = options[:command]
+ command = "-c '#{command}'"
+ end
+ exec "psql -U #{uri.user} -h #{uri.host} -p #{uri.port || 5432} #{command} #{uri.path[1..-1]}"
rescue Errno::ENOENT
output_with_bang "The local psql command could not be located"
output_with_bang "For help installing psql, see http://devcenter.heroku.com/articles/local-postgresql"

5 comments on commit 5847c08

@will
will commented on 5847c08 Sep 27, 2013

why didn't you use the exec_sql method that's already there?

@will
will commented on 5847c08 Sep 27, 2013

nevermind

@will
will commented on 5847c08 Sep 27, 2013

well actually, what is wrong with just doing

~ ➤  h pg:psql -a will <<< "select now()"
Time: 192.751 ms
              now
-------------------------------
 2013-09-27 16:38:50.186136-07
(1 row)

Time: 104.515 ms
@freeformz
Heroku member

Nothing, except that as a psql user I may use -c ... and then try to use it with pg:psql and it will fail. Now it won't.

@BrentWheeldon

A reason for using -c ... is that it will exit with a non-zero exit code if the command files. The other way always exits with 0.

Please sign in to comment.
Something went wrong with that request. Please try again.