| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
|
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
my $tempdir = PostgreSQL::Test::Utils::tempdir; |
| 12 |
|
| 13 |
######################################### |
| 14 |
# Basic checks |
| 15 |
|
| 16 |
program_help_ok('pg_dump'); |
| 17 |
program_version_ok('pg_dump'); |
| 18 |
program_options_handling_ok('pg_dump'); |
| 19 |
|
| 20 |
program_help_ok('pg_restore'); |
| 21 |
program_version_ok('pg_restore'); |
| 22 |
program_options_handling_ok('pg_restore'); |
| 23 |
|
| 24 |
program_help_ok('pg_dumpall'); |
| 25 |
program_version_ok('pg_dumpall'); |
| 26 |
program_options_handling_ok('pg_dumpall'); |
| 27 |
|
| 28 |
######################################### |
| 29 |
# Test various invalid options and disallowed combinations |
| 30 |
# Doesn't require a PG instance to be set up, so do this first. |
| 31 |
|
| 32 |
command_fails_like( |
| 33 |
[ 'pg_dump', 'qqq', 'abc' ], |
| 34 |
qr/\Qpg_dump: error: too many command-line arguments (first is "abc")\E/, |
| 35 |
'pg_dump: too many command-line arguments'); |
| 36 |
|
| 37 |
command_fails_like( |
| 38 |
[ 'pg_restore', 'qqq', 'abc' ], |
| 39 |
qr/\Qpg_restore: error: too many command-line arguments (first is "abc")\E/, |
| 40 |
'pg_restore: too many command-line arguments'); |
| 41 |
|
| 42 |
command_fails_like( |
| 43 |
[ 'pg_dumpall', 'qqq', 'abc' ], |
| 44 |
qr/\Qpg_dumpall: error: too many command-line arguments (first is "qqq")\E/, |
| 45 |
'pg_dumpall: too many command-line arguments'); |
| 46 |
|
| 47 |
command_fails_like( |
| 48 |
[ 'pg_dump', '-s', '-a' ], |
| 49 |
qr/\Qpg_dump: error: options -s\/--schema-only and -a\/--data-only cannot be used together\E/, |
| 50 |
'pg_dump: options -s/--schema-only and -a/--data-only cannot be used together' |
| 51 |
); |
| 52 |
|
| 53 |
command_fails_like( |
| 54 |
[ 'pg_dump', '-s', '--statistics-only' ], |
| 55 |
qr/\Qpg_dump: error: options -s\/--schema-only and --statistics-only cannot be used together\E/, |
| 56 |
'pg_dump: error: options -s/--schema-only and --statistics-only cannot be used together' |
| 57 |
); |
| 58 |
|
| 59 |
command_fails_like( |
| 60 |
[ 'pg_dump', '-a', '--statistics-only' ], |
| 61 |
qr/\Qpg_dump: error: options -a\/--data-only and --statistics-only cannot be used together\E/, |
| 62 |
'pg_dump: error: options -a/--data-only and --statistics-only cannot be used together' |
| 63 |
); |
| 64 |
|
| 65 |
command_fails_like( |
| 66 |
[ 'pg_dump', '-s', '--include-foreign-data=xxx' ], |
| 67 |
qr/\Qpg_dump: error: options -s\/--schema-only and --include-foreign-data cannot be used together\E/, |
| 68 |
'pg_dump: options -s/--schema-only and --include-foreign-data cannot be used together' |
| 69 |
); |
| 70 |
|
| 71 |
command_fails_like( |
| 72 |
[ 'pg_dump', '--statistics-only', '--no-statistics' ], |
| 73 |
qr/\Qpg_dump: error: options --statistics-only and --no-statistics cannot be used together\E/, |
| 74 |
'pg_dump: options --statistics-only and --no-statistics cannot be used together' |
| 75 |
); |
| 76 |
|
| 77 |
command_fails_like( |
| 78 |
[ 'pg_dump', '-j2', '--include-foreign-data=xxx' ], |
| 79 |
qr/\Qpg_dump: error: option --include-foreign-data is not supported with parallel backup\E/, |
| 80 |
'pg_dump: option --include-foreign-data is not supported with parallel backup' |
| 81 |
); |
| 82 |
|
| 83 |
command_fails_like( |
| 84 |
['pg_restore'], |
| 85 |
qr{\Qpg_restore: error: one of -d/--dbname and -f/--file must be specified\E}, |
| 86 |
'pg_restore: error: one of -d/--dbname and -f/--file must be specified'); |
| 87 |
|
| 88 |
command_fails_like( |
| 89 |
[ 'pg_restore', '-s', '-a', '-f -' ], |
| 90 |
qr/\Qpg_restore: error: options -s\/--schema-only and -a\/--data-only cannot be used together\E/, |
| 91 |
'pg_restore: options -s/--schema-only and -a/--data-only cannot be used together' |
| 92 |
); |
| 93 |
|
| 94 |
command_fails_like( |
| 95 |
[ 'pg_restore', '-d', 'xxx', '-f', 'xxx' ], |
| 96 |
qr/\Qpg_restore: error: options -d\/--dbname and -f\/--file cannot be used together\E/, |
| 97 |
'pg_restore: options -d/--dbname and -f/--file cannot be used together'); |
| 98 |
|
| 99 |
command_fails_like( |
| 100 |
[ 'pg_dump', '-c', '-a' ], |
| 101 |
qr/\Qpg_dump: error: options -c\/--clean and -a\/--data-only cannot be used together\E/, |
| 102 |
'pg_dump: options -c/--clean and -a/--data-only cannot be used together'); |
| 103 |
|
| 104 |
command_fails_like( |
| 105 |
[ 'pg_restore', '-c', '-a', '-f -' ], |
| 106 |
qr/\Qpg_restore: error: options -c\/--clean and -a\/--data-only cannot be used together\E/, |
| 107 |
'pg_restore: options -c/--clean and -a/--data-only cannot be used together' |
| 108 |
); |
| 109 |
|
| 110 |
command_fails_like( |
| 111 |
[ 'pg_dump', '--if-exists' ], |
| 112 |
qr/\Qpg_dump: error: option --if-exists requires option -c\/--clean\E/, |
| 113 |
'pg_dump: option --if-exists requires option -c/--clean'); |
| 114 |
|
| 115 |
command_fails_like( |
| 116 |
[ 'pg_dump', '-j3' ], |
| 117 |
qr/\Qpg_dump: error: parallel backup only supported by the directory format\E/, |
| 118 |
'pg_dump: parallel backup only supported by the directory format'); |
| 119 |
|
| 120 |
# Note the trailing whitespace for the value of --jobs, that is valid. |
| 121 |
command_fails_like( |
| 122 |
[ 'pg_dump', '-j', '-1 ' ], |
| 123 |
qr/\Qpg_dump: error: -j\/--jobs must be in range\E/, |
| 124 |
'pg_dump: -j/--jobs must be in range'); |
| 125 |
|
| 126 |
command_fails_like( |
| 127 |
[ 'pg_dump', '-F', 'garbage' ], |
| 128 |
qr/\Qpg_dump: error: invalid output format\E/, |
| 129 |
'pg_dump: invalid output format'); |
| 130 |
|
| 131 |
command_fails_like( |
| 132 |
[ 'pg_restore', '-j', '-1', '-f -' ], |
| 133 |
qr/\Qpg_restore: error: -j\/--jobs must be in range\E/, |
| 134 |
'pg_restore: -j/--jobs must be in range'); |
| 135 |
|
| 136 |
command_fails_like( |
| 137 |
[ 'pg_restore', '--single-transaction', '-j3', '-f -' ], |
| 138 |
qr/\Qpg_restore: error: cannot specify both --single-transaction and multiple jobs\E/, |
| 139 |
'pg_restore: cannot specify both --single-transaction and multiple jobs'); |
| 140 |
|
| 141 |
command_fails_like( |
| 142 |
[ 'pg_dump', '--compress', 'garbage' ], |
| 143 |
qr/\Qpg_dump: error: unrecognized compression algorithm/, |
| 144 |
'pg_dump: invalid --compress'); |
| 145 |
|
| 146 |
command_fails_like( |
| 147 |
[ 'pg_dump', '--compress', 'none:1' ], |
| 148 |
qr/\Qpg_dump: error: invalid compression specification: compression algorithm "none" does not accept a compression level\E/, |
| 149 |
'pg_dump: invalid compression specification: compression algorithm "none" does not accept a compression level' |
| 150 |
); |
| 151 |
|
| 152 |
|
| 153 |
if (check_pg_config("#define HAVE_LIBZ 1")) |
| 154 |
{ |
| 155 |
command_fails_like( |
| 156 |
[ 'pg_dump', '-Z', '15' ], |
| 157 |
qr/\Qpg_dump: error: invalid compression specification: compression algorithm "gzip" expects a compression level between 1 and 9 (default at -1)\E/, |
| 158 |
'pg_dump: invalid compression specification: must be in range'); |
| 159 |
|
| 160 |
command_fails_like( |
| 161 |
[ 'pg_dump', '--compress', '1', '--format', 'tar' ], |
| 162 |
qr/\Qpg_dump: error: compression is not supported by tar archive format\E/, |
| 163 |
'pg_dump: compression is not supported by tar archive format'); |
| 164 |
|
| 165 |
command_fails_like( |
| 166 |
[ 'pg_dump', '-Z', 'gzip:nonInt' ], |
| 167 |
qr/\Qpg_dump: error: invalid compression specification: unrecognized compression option: "nonInt"\E/, |
| 168 |
'pg_dump: invalid compression specification: must be an integer'); |
| 169 |
} |
| 170 |
else |
| 171 |
{ |
| 172 |
# --jobs > 1 forces an error with tar format. |
| 173 |
command_fails_like( |
| 174 |
[ 'pg_dump', '--format', 'tar', '-j3' ], |
| 175 |
qr/\Qpg_dump: error: parallel backup only supported by the directory format\E/, |
| 176 |
'pg_dump: warning: parallel backup not supported by tar format'); |
| 177 |
|
| 178 |
command_fails_like( |
| 179 |
[ 'pg_dump', '-Z', 'gzip:nonInt', '--format', 'tar', '-j2' ], |
| 180 |
qr/\Qpg_dump: error: invalid compression specification: unrecognized compression option\E/, |
| 181 |
'pg_dump: invalid compression specification: must be an integer'); |
| 182 |
} |
| 183 |
|
| 184 |
command_fails_like( |
| 185 |
[ 'pg_dump', '--extra-float-digits', '-16' ], |
| 186 |
qr/\Qpg_dump: error: --extra-float-digits must be in range\E/, |
| 187 |
'pg_dump: --extra-float-digits must be in range'); |
| 188 |
|
| 189 |
command_fails_like( |
| 190 |
[ 'pg_dump', '--rows-per-insert', '0' ], |
| 191 |
qr/\Qpg_dump: error: --rows-per-insert must be in range\E/, |
| 192 |
'pg_dump: --rows-per-insert must be in range'); |
| 193 |
|
| 194 |
command_fails_like( |
| 195 |
[ 'pg_restore', '--if-exists', '-f -' ], |
| 196 |
qr/\Qpg_restore: error: option --if-exists requires option -c\/--clean\E/, |
| 197 |
'pg_restore: option --if-exists requires option -c/--clean'); |
| 198 |
|
| 199 |
command_fails_like( |
| 200 |
[ 'pg_restore', '-f -', '-F', 'garbage' ], |
| 201 |
qr/\Qpg_restore: error: unrecognized archive format "garbage";\E/, |
| 202 |
'pg_dump: unrecognized archive format'); |
| 203 |
|
| 204 |
command_fails_like( |
| 205 |
[ 'pg_dump', '--on-conflict-do-nothing' ], |
| 206 |
qr/pg_dump: error: option --on-conflict-do-nothing requires option --inserts, --rows-per-insert, or --column-inserts/, |
| 207 |
'pg_dump: --on-conflict-do-nothing requires --inserts, --rows-per-insert, --column-inserts' |
| 208 |
); |
| 209 |
|
| 210 |
# pg_dumpall command-line argument checks |
| 211 |
command_fails_like( |
| 212 |
[ 'pg_dumpall', '-g', '-r' ], |
| 213 |
qr/\Qpg_dumpall: error: options -g\/--globals-only and -r\/--roles-only cannot be used together\E/, |
| 214 |
'pg_dumpall: options -g/--globals-only and -r/--roles-only cannot be used together' |
| 215 |
); |
| 216 |
|
| 217 |
command_fails_like( |
| 218 |
[ 'pg_dumpall', '-g', '-t' ], |
| 219 |
qr/\Qpg_dumpall: error: options -g\/--globals-only and -t\/--tablespaces-only cannot be used together\E/, |
| 220 |
'pg_dumpall: options -g/--globals-only and -t/--tablespaces-only cannot be used together' |
| 221 |
); |
| 222 |
|
| 223 |
command_fails_like( |
| 224 |
[ 'pg_dumpall', '-r', '-t' ], |
| 225 |
qr/\Qpg_dumpall: error: options -r\/--roles-only and -t\/--tablespaces-only cannot be used together\E/, |
| 226 |
'pg_dumpall: options -r/--roles-only and -t/--tablespaces-only cannot be used together' |
| 227 |
); |
| 228 |
|
| 229 |
command_fails_like( |
| 230 |
[ 'pg_dumpall', '--if-exists' ], |
| 231 |
qr/\Qpg_dumpall: error: option --if-exists requires option -c\/--clean\E/, |
| 232 |
'pg_dumpall: option --if-exists requires option -c/--clean'); |
| 233 |
|
| 234 |
command_fails_like( |
| 235 |
[ 'pg_restore', '-C', '-1', '-f -' ], |
| 236 |
qr/\Qpg_restore: error: options -C\/--create and -1\/--single-transaction cannot be used together\E/, |
| 237 |
'pg_restore: options -C\/--create and -1\/--single-transaction cannot be used together' |
| 238 |
); |
| 239 |
|
| 240 |
# also fails for -r and -t, but it seems pointless to add more tests for those. |
| 241 |
command_fails_like( |
| 242 |
[ 'pg_dumpall', '--exclude-database=foo', '--globals-only' ], |
| 243 |
qr/\Qpg_dumpall: error: option --exclude-database cannot be used together with -g\/--globals-only\E/, |
| 244 |
'pg_dumpall: option --exclude-database cannot be used together with -g/--globals-only' |
| 245 |
); |
| 246 |
|
| 247 |
done_testing(); |