Em qui, 14 de fev de 2019 às 22:41, Andreas Karlsson
<andr...@proxel.se> escreveu:
> Agreed, "-f -" would be acceptable. I use pg_restore to stdout a lot,
> but almost always manually and would have to have to remember and type
> "--convert-to-text".
>
Since no one has stepped up, I took a stab at it. It will prohibit
standard output unless '-f -' be specified. -l option also has the
same restriction.

It breaks backward compatibility and as Tom suggested a variant of
this patch (without docs) should be applied to back branches.


-- 
   Euler Taveira                                   Timbira -
http://www.timbira.com.br/
   PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento
From 4c8cba412975995dceeccae40663753b9dbe1383 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 (i) it errors out if
neither -f nor -d is specified, (ii) '-f -' doesn't create a file called
'-' instead it outputs to standard output and (iii) it errors out if -l
option doesn't specify -f as well.

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

diff --git a/doc/src/sgml/ref/pg_restore.sgml b/doc/src/sgml/ref/pg_restore.sgml
index 725acb1..b197a2b 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>
@@ -287,7 +287,9 @@
         List the table of contents of the archive. The output of this operation
         can be used as input to the <option>-L</option> option.  Note that
         if filtering switches such as <option>-n</option> or <option>-t</option> are
-        used with <option>-l</option>, they will restrict the items listed.
+        used with <option>-l</option>, they will restrict the items listed. Use
+        <option>-f</option> <literal>-</literal> for
+        <systemitem>stdout</systemitem>.
        </para>
       </listitem>
      </varlistentry>
@@ -952,7 +954,7 @@ CREATE DATABASE foo WITH TEMPLATE template0;
    To reorder database items, it is first necessary to dump the table of
    contents of the archive:
 <screen>
-<prompt>$</prompt> <userinput>pg_restore -l db.dump &gt; db.list</userinput>
+<prompt>$</prompt> <userinput>pg_restore -l -f db.list db.dump</userinput>
 </screen>
    The listing file consists of a header and one line for each item, e.g.:
 <programlisting>
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 8b55f59..e6b8b6a 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -1517,7 +1517,9 @@ SetOutput(ArchiveHandle *AH, const char *filename, int compression)
 {
 	int			fn;
 
-	if (filename)
+	if (filename && strcmp(filename, "-") == 0)
+		fn = fileno(stdout);
+	else if (filename)
 		fn = -1;
 	else if (AH->FH)
 		fn = fileno(AH->FH);
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 428e040..b5c6aae 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -303,6 +303,16 @@ main(int argc, char **argv)
 		exit_nicely(1);
 	}
 
+	/* Complain if neither -f nor -d was specified */
+	if (!opts->dbname && !opts->filename)
+	{
+		if (opts->tocSummary)
+			fprintf(stderr, _("%s: option -f/--file should be specified\n"), progname);
+		else
+			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 a875d54..0cfda48 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