Em qua, 27 de fev de 2019 às 23:48, Imai, Yoshikazu
<imai.yoshik...@jp.fujitsu.com> escreveu:
>
> Is there no need to rewrite the Description in the Doc to state we should 
> specify either -d or -f option?
> (and also it might be better to write if -l option is given, neither -d nor 
> -f option isn't necessarily needed.)
>
I don't think so. The description is already there (see "pg_restore
can operate in two modes..."). I left -l as is which means that
'pg_restore -l foo.dump' dumps to standard output and 'pg_restore -f -
-l foo.dump' has the same behavior).

> I think the former one looks like pretty, but which one is preffered?
>
I don't have a style preference but decided to change to your
suggestion. New version attached.


-- 
   Euler Taveira                                   Timbira -
http://www.timbira.com.br/
   PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento
From 31bee4d3ba26b7867120d7fce60399fbfecb792b Mon Sep 17 00:00:00 2001
From: Euler Taveira <eu...@timbira.com.br>
Date: Sun, 17 Feb 2019 14:16:27 +0000
Subject: [PATCH] pg_restore supports stdout in --file

pg_restore defaults to standard output if neither -f nor -d is
specified. This behavior confuses users that expect an error if the
restore target (database or file) isn't specified. Other clients already
support '-f -' but pg_restore does not.

This change breaks backward compatibility because it errors out if
neither -f nor -d is specified and '-f -' doesn't create a file called
'-' instead it outputs to standard output.

Discussion: https://www.postgresql.org/message-id/87sgwrmhdv....@news-spur.riddles.org.uk
---
 doc/src/sgml/ref/pg_restore.sgml     |  4 ++--
 src/bin/pg_dump/pg_backup_archiver.c |  7 ++++++-
 src/bin/pg_dump/pg_restore.c         |  7 +++++++
 src/bin/pg_dump/t/001_basic.pl       | 14 +++++++-------
 4 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/doc/src/sgml/ref/pg_restore.sgml b/doc/src/sgml/ref/pg_restore.sgml
index 725acb1..c8c4c20 100644
--- a/doc/src/sgml/ref/pg_restore.sgml
+++ b/doc/src/sgml/ref/pg_restore.sgml
@@ -176,8 +176,8 @@
       <listitem>
        <para>
         Specify output file for generated script, or for the listing
-        when used with <option>-l</option>. Default is the standard
-        output.
+        when used with <option>-l</option>. Use <option>-f</option>
+        <literal>-</literal> for <systemitem>stdout</systemitem>.
        </para>
       </listitem>
      </varlistentry>
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 62bf149..9308b3b 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -1520,7 +1520,12 @@ SetOutput(ArchiveHandle *AH, const char *filename, int compression)
 	int			fn;
 
 	if (filename)
-		fn = -1;
+	{
+		if (strcmp(filename, "-") == 0)
+			fn = fileno(stdout);
+		else
+			fn = -1;
+	}
 	else if (AH->FH)
 		fn = fileno(AH->FH);
 	else if (AH->fSpec)
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 428e040..21cd6da 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -303,6 +303,13 @@ main(int argc, char **argv)
 		exit_nicely(1);
 	}
 
+	/* Complain if neither -f nor -d was specified (this restriction is not valid for TOC) */
+	if (!opts->dbname && !opts->filename && !opts->tocSummary)
+	{
+		fprintf(stderr, _("%s: option -d/--dbname or -f/--file should be specified\n"), progname);
+		exit_nicely(1);
+	}
+
 	/* Should get at most one of -d and -f, else user is confused */
 	if (opts->dbname)
 	{
diff --git a/src/bin/pg_dump/t/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl
index 3a58f9b..b4cce65 100644
--- a/src/bin/pg_dump/t/001_basic.pl
+++ b/src/bin/pg_dump/t/001_basic.pl
@@ -50,7 +50,7 @@ command_fails_like(
 );
 
 command_fails_like(
-	[ 'pg_restore', '-s', '-a' ],
+	[ 'pg_restore', '-s', '-a', '-f -' ],
 	qr/\Qpg_restore: options -s\/--schema-only and -a\/--data-only cannot be used together\E/,
 	'pg_restore: options -s/--schema-only and -a/--data-only cannot be used together'
 );
@@ -66,7 +66,7 @@ command_fails_like(
 	'pg_dump: options -c/--clean and -a/--data-only cannot be used together');
 
 command_fails_like(
-	[ 'pg_restore', '-c', '-a' ],
+	[ 'pg_restore', '-c', '-a', '-f -' ],
 	qr/\Qpg_restore: options -c\/--clean and -a\/--data-only cannot be used together\E/,
 	'pg_restore: options -c/--clean and -a/--data-only cannot be used together'
 );
@@ -92,12 +92,12 @@ command_fails_like(
 	'pg_dump: invalid output format');
 
 command_fails_like(
-	[ 'pg_restore', '-j', '-1' ],
+	[ 'pg_restore', '-j', '-1', '-f -' ],
 	qr/\Qpg_restore: invalid number of parallel jobs\E/,
 	'pg_restore: invalid number of parallel jobs');
 
 command_fails_like(
-	[ 'pg_restore', '--single-transaction', '-j3' ],
+	[ 'pg_restore', '--single-transaction', '-j3', '-f -' ],
 	qr/\Qpg_restore: cannot specify both --single-transaction and multiple jobs\E/,
 	'pg_restore: cannot specify both --single-transaction and multiple jobs');
 
@@ -107,12 +107,12 @@ command_fails_like(
 	'pg_dump: compression level must be in range 0..9');
 
 command_fails_like(
-	[ 'pg_restore', '--if-exists' ],
+	[ 'pg_restore', '--if-exists', '-f -' ],
 	qr/\Qpg_restore: option --if-exists requires option -c\/--clean\E/,
 	'pg_restore: option --if-exists requires option -c/--clean');
 
 command_fails_like(
-	[ 'pg_restore', '-F', 'garbage' ],
+	[ 'pg_restore', '-f -', '-F', 'garbage' ],
 	qr/\Qpg_restore: unrecognized archive format "garbage";\E/,
 	'pg_dump: unrecognized archive format');
 
@@ -146,7 +146,7 @@ command_fails_like(
 	'pg_dumpall: option --if-exists requires option -c/--clean');
 
 command_fails_like(
-	[ 'pg_restore', '-C', '-1' ],
+	[ 'pg_restore', '-C', '-1', '-f -' ],
 	qr/\Qpg_restore: options -C\/--create and -1\/--single-transaction cannot be used together\E/,
 	'pg_restore: options -C\/--create and -1\/--single-transaction cannot be used together'
 );
-- 
2.7.4

Reply via email to