Skip to content

Commit 0ed92cf

Browse files
committed
pg_dump: reject combination of "only" and "with"
Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de> Discussion: https://postgr.es/m/8ce896d1a05040905cc1a3afbc04e94d8e95669a.camel@j-davis.com Backpatch-through: 18
1 parent a4801eb commit 0ed92cf

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,17 @@ main(int argc, char **argv)
860860
if (with_statistics && no_statistics)
861861
pg_fatal("options --with-statistics and --no-statistics cannot be used together");
862862

863+
/* reject conflicting "-only" and "with-" options */
864+
if (data_only && (with_schema || with_statistics))
865+
pg_fatal("options %s and %s cannot be used together",
866+
"-a/--data-only", with_schema ? "--with-schema" : "--with-statistics");
867+
if (schema_only && (with_data || with_statistics))
868+
pg_fatal("options %s and %s cannot be used together",
869+
"-s/--schema-only", with_data ? "--with-data" : "--with-statistics");
870+
if (statistics_only && (with_data || with_schema))
871+
pg_fatal("options %s and %s cannot be used together",
872+
"--statistics-only", with_data ? "--with-data" : "--with-schema");
873+
863874
if (schema_only && foreign_servers_include_patterns.head != NULL)
864875
pg_fatal("options -s/--schema-only and --include-foreign-data cannot be used together");
865876

@@ -873,11 +884,9 @@ main(int argc, char **argv)
873884
pg_fatal("option --if-exists requires option -c/--clean");
874885

875886
/*
876-
* Set derivative flags. An "-only" option may be overridden by an
877-
* explicit "with-" option; e.g. "--schema-only --with-statistics" will
878-
* include schema and statistics. Other ambiguous or nonsensical
879-
* combinations, e.g. "--schema-only --no-schema", will have already
880-
* caused an error in one of the checks above.
887+
* Set derivative flags. Ambiguous or nonsensical combinations, e.g.
888+
* "--schema-only --no-schema", will have already caused an error in one
889+
* of the checks above.
881890
*/
882891
dopt.dumpData = ((dopt.dumpData && !schema_only && !statistics_only) ||
883892
(data_only || with_data)) && !no_data;

src/bin/pg_dump/pg_restore.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,17 @@ main(int argc, char **argv)
381381
if (with_statistics && no_statistics)
382382
pg_fatal("options --with-statistics and --no-statistics cannot be used together");
383383

384+
/* reject conflicting "only-" and "with-" options */
385+
if (data_only && (with_schema || with_statistics))
386+
pg_fatal("options %s and %s cannot be used together",
387+
"-a/--data-only", with_schema ? "--with-schema" : "--with-statistics");
388+
if (schema_only && (with_data || with_statistics))
389+
pg_fatal("options %s and %s cannot be used together",
390+
"-s/--schema-only", with_data ? "--with-data" : "--with-statistics");
391+
if (statistics_only && (with_data || with_schema))
392+
pg_fatal("options %s and %s cannot be used together",
393+
"--statistics-only", with_data ? "--with-data" : "--with-schema");
394+
384395
if (data_only && opts->dropSchema)
385396
pg_fatal("options -c/--clean and -a/--data-only cannot be used together");
386397

@@ -399,11 +410,9 @@ main(int argc, char **argv)
399410
pg_fatal("cannot specify both --single-transaction and multiple jobs");
400411

401412
/*
402-
* Set derivative flags. An "-only" option may be overridden by an
403-
* explicit "with-" option; e.g. "--schema-only --with-statistics" will
404-
* include schema and statistics. Other ambiguous or nonsensical
405-
* combinations, e.g. "--schema-only --no-schema", will have already
406-
* caused an error in one of the checks above.
413+
* Set derivative flags. Ambiguous or nonsensical combinations, e.g.
414+
* "--schema-only --no-schema", will have already caused an error in one
415+
* of the checks above.
407416
*/
408417
opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) ||
409418
(data_only || with_data)) && !no_data;

src/bin/pg_dump/t/002_pg_dump.pl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -799,13 +799,6 @@
799799
'postgres',
800800
],
801801
},
802-
schema_only_with_statistics => {
803-
dump_cmd => [
804-
'pg_dump', '--no-sync',
805-
"--file=$tempdir/schema_only_with_statistics.sql",
806-
'--schema-only', '--with-statistics', 'postgres',
807-
],
808-
},
809802
no_schema => {
810803
dump_cmd => [
811804
'pg_dump', '--no-sync',
@@ -5207,6 +5200,17 @@
52075200
qr/\Qpg_dump: error: no matching schemas were found for pattern\E/,
52085201
'no matching schemas');
52095202
5203+
command_fails_like(
5204+
[
5205+
'pg_dump',
5206+
'--port' => $port,
5207+
'--strict-names',
5208+
'--schema-only',
5209+
'--with-statistics',
5210+
],
5211+
qr/\Qpg_dump: error: options -s\/--schema-only and --with-statistics cannot be used together\E/,
5212+
'cannot use --schema-only and --with-statistics together');
5213+
52105214
command_fails_like(
52115215
[
52125216
'pg_dump',

0 commit comments

Comments
 (0)