#include "common/string.h"
#include "connectdb.h"
#include "dumputils.h"
+#include "fe_utils/option_utils.h"
#include "fe_utils/string_utils.h"
#include "filter.h"
#include "getopt_long.h"
bool data_only = false;
bool globals_only = false;
bool roles_only = false;
+ bool schema_only = false;
bool tablespaces_only = false;
PGconn *conn;
int encoding;
break;
case 's':
+ schema_only = true;
appendPQExpBufferStr(pgdumpopts, " -s");
break;
exit_nicely(1);
}
- if (database_exclude_patterns.head != NULL &&
- (globals_only || roles_only || tablespaces_only))
- {
- pg_log_error("option %s cannot be used together with %s, %s, or %s",
- "--exclude-database",
- "-g/--globals-only", "-r/--roles-only", "-t/--tablespaces-only");
- pg_log_error_hint("Try \"%s --help\" for more information.", progname);
- exit_nicely(1);
- }
-
- /* Make sure the user hasn't specified a mix of globals-only options */
- if (globals_only && roles_only)
- {
- pg_log_error("options %s and %s cannot be used together",
- "-g/--globals-only", "-r/--roles-only");
- pg_log_error_hint("Try \"%s --help\" for more information.", progname);
- exit_nicely(1);
- }
-
- if (globals_only && tablespaces_only)
- {
- pg_log_error("options %s and %s cannot be used together",
- "-g/--globals-only", "-t/--tablespaces-only");
- pg_log_error_hint("Try \"%s --help\" for more information.", progname);
- exit_nicely(1);
- }
+ /* --exclude_database is incompatible with global *-only options */
+ check_mut_excl_opts(database_exclude_patterns.head, "--exclude-database",
+ globals_only, "-g/--globals-only",
+ roles_only, "-r/--roles-only",
+ tablespaces_only, "-t/--tablespaces-only");
+
+ /* *-only options are incompatible with each other */
+ check_mut_excl_opts(data_only, "-a/--data-only",
+ globals_only, "-g/--globals-only",
+ roles_only, "-r/--roles-only",
+ schema_only, "-s/--schema-only",
+ statistics_only, "--statistics-only",
+ tablespaces_only, "-t/--tablespaces-only");
+
+ /* --no-* and *-only for same thing are incompatible */
+ check_mut_excl_opts(data_only, "-a/--data-only",
+ no_data, "--no-data");
+ check_mut_excl_opts(schema_only, "-s/--schema-only",
+ no_schema, "--no-schema");
+ check_mut_excl_opts(statistics_only, "--statistics-only",
+ no_statistics, "--no-statistics");
+
+ /* --statistics and --no-statistics are incompatible */
+ check_mut_excl_opts(with_statistics, "--statistics",
+ no_statistics, "--no-statistics");
+
+ /* --statistics is incompatible with *-only (except --statistics-only) */
+ check_mut_excl_opts(with_statistics, "--statistics",
+ data_only, "-a/--data-only",
+ globals_only, "-g/--globals-only",
+ roles_only, "-r/--roles-only",
+ schema_only, "-s/--schema-only",
+ tablespaces_only, "-t/--tablespaces-only");
if (if_exists && !output_clean)
pg_fatal("option %s requires option %s",
"--if-exists", "-c/--clean");
- if (roles_only && tablespaces_only)
- {
- pg_log_error("options %s and %s cannot be used together",
- "-r/--roles-only", "-t/--tablespaces-only");
- pg_log_error_hint("Try \"%s --help\" for more information.", progname);
- exit_nicely(1);
- }
-
/* Get format for dump. */
archDumpFormat = parseDumpFormat(format_name);
# also fails for -r and -t, but it seems pointless to add more tests for those.
command_fails_like(
[ 'pg_dumpall', '--exclude-database=foo', '--globals-only' ],
- qr/\Qpg_dumpall: error: option --exclude-database cannot be used together with -g\/--globals-only\E/,
- 'pg_dumpall: option --exclude-database cannot be used together with -g/--globals-only'
+ qr/\Qpg_dumpall: error: options --exclude-database and -g\/--globals-only cannot be used together\E/,
+ 'pg_dumpall: options --exclude-database and -g/--globals-only cannot be used together'
+);
+
+command_fails_like(
+ [ 'pg_dumpall', '-a', '--no-data' ],
+ qr/\Qpg_dumpall: error: options -a\/--data-only and --no-data cannot be used together\E/,
+ 'pg_dumpall: options -a\/--data-only and --no-data cannot be used together'
+);
+
+command_fails_like(
+ [ 'pg_dumpall', '-s', '--no-schema' ],
+ qr/\Qpg_dumpall: error: options -s\/--schema-only and --no-schema cannot be used together\E/,
+ 'pg_dumpall: options -s\/--schema-only and --no-schema cannot be used together'
+);
+
+command_fails_like(
+ [ 'pg_dumpall', '--statistics-only', '--no-statistics' ],
+ qr/\Qpg_dumpall: error: options --statistics-only and --no-statistics cannot be used together\E/,
+ 'pg_dumpall: options --statistics-only and --no-statistics cannot be used together'
+);
+
+command_fails_like(
+ [ 'pg_dumpall', '--statistics', '--no-statistics' ],
+ qr/\Qpg_dumpall: error: options --statistics and --no-statistics cannot be used together\E/,
+ 'pg_dumpall: options --statistics-only and --no-statistics cannot be used together'
+);
+
+command_fails_like(
+ [ 'pg_dumpall', '--statistics', '--tablespaces-only' ],
+ qr/\Qpg_dumpall: error: options --statistics and -t\/--tablespaces-only cannot be used together\E/,
+ 'pg_dumpall: options --statistics and -t\/--tablespaces-only cannot be used together'
);
command_fails_like(
return true;
}
+
+/*
+ * Fail with appropriate error if 2 or more of the specified options are set.
+ * The first parameter is the number of arguments. Following that is an
+ * arbitrary number of bool/string pairs. The bool indicates whether the
+ * option is set, and the string is used for the error message. Note that only
+ * the first pair of enabled options we find are reported.
+ *
+ * Don't call this directly. Use the check_mut_excl_opts() macro in
+ * option_utils.h, which is the exact same except it doesn't take the first
+ * parameter (it discovers the number of arguments automagically).
+ */
+void
+check_mut_excl_opts_internal(int n,...)
+{
+ char *first = NULL;
+ va_list args;
+
+ Assert(n % 2 == 0);
+
+ va_start(args, n);
+ for (int i = 0; i < n; i += 2)
+ {
+ bool set = va_arg(args, int);
+ char *opt = va_arg(args, char *);
+
+ if (set && first)
+ pg_fatal("options %s and %s cannot be used together",
+ first, opt);
+
+ if (set)
+ first = opt;
+ }
+ va_end(args);
+}