mirror of
https://github.com/postgres/postgres.git
synced 2025-06-05 00:02:04 -04:00
Add the support for '-f' option in dropdb utility.
Specifying '-f' will add the 'force' option to the DROP DATABASE command sent to the server. This will try to terminate all existing connections to the target database before dropping it. Author: Pavel Stehule Reviewed-by: Vignesh C and Amit Kapila Discussion: https://postgr.es/m/CAP_rwwmLJJbn70vLOZFpxGw3XD7nLB_7+NKz46H5EOO2k5H7OQ@mail.gmail.com
This commit is contained in:
parent
eecf963269
commit
80e05a088e
@ -86,6 +86,18 @@ PostgreSQL documentation
|
|||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><option>-f</option></term>
|
||||||
|
<term><option>--force</option></term>
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
Attempt to terminate all existing connections to the target database
|
||||||
|
before dropping it. See <xref linkend="sql-dropdatabase"/> for more
|
||||||
|
information on this option.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-V</option></term>
|
<term><option>-V</option></term>
|
||||||
<term><option>--version</option></term>
|
<term><option>--version</option></term>
|
||||||
|
@ -34,6 +34,7 @@ main(int argc, char *argv[])
|
|||||||
{"interactive", no_argument, NULL, 'i'},
|
{"interactive", no_argument, NULL, 'i'},
|
||||||
{"if-exists", no_argument, &if_exists, 1},
|
{"if-exists", no_argument, &if_exists, 1},
|
||||||
{"maintenance-db", required_argument, NULL, 2},
|
{"maintenance-db", required_argument, NULL, 2},
|
||||||
|
{"force", no_argument, NULL, 'f'},
|
||||||
{NULL, 0, NULL, 0}
|
{NULL, 0, NULL, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -49,6 +50,7 @@ main(int argc, char *argv[])
|
|||||||
enum trivalue prompt_password = TRI_DEFAULT;
|
enum trivalue prompt_password = TRI_DEFAULT;
|
||||||
bool echo = false;
|
bool echo = false;
|
||||||
bool interactive = false;
|
bool interactive = false;
|
||||||
|
bool force = false;
|
||||||
|
|
||||||
PQExpBufferData sql;
|
PQExpBufferData sql;
|
||||||
|
|
||||||
@ -61,7 +63,7 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
handle_help_version_opts(argc, argv, "dropdb", help);
|
handle_help_version_opts(argc, argv, "dropdb", help);
|
||||||
|
|
||||||
while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
|
while ((c = getopt_long(argc, argv, "h:p:U:wWeif", long_options, &optindex)) != -1)
|
||||||
{
|
{
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
@ -86,6 +88,9 @@ main(int argc, char *argv[])
|
|||||||
case 'i':
|
case 'i':
|
||||||
interactive = true;
|
interactive = true;
|
||||||
break;
|
break;
|
||||||
|
case 'f':
|
||||||
|
force = true;
|
||||||
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
/* this covers the long options */
|
/* this covers the long options */
|
||||||
break;
|
break;
|
||||||
@ -123,8 +128,10 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
initPQExpBuffer(&sql);
|
initPQExpBuffer(&sql);
|
||||||
|
|
||||||
appendPQExpBuffer(&sql, "DROP DATABASE %s%s;",
|
appendPQExpBuffer(&sql, "DROP DATABASE %s%s%s;",
|
||||||
(if_exists ? "IF EXISTS " : ""), fmtId(dbname));
|
(if_exists ? "IF EXISTS " : ""),
|
||||||
|
fmtId(dbname),
|
||||||
|
force ? " WITH (FORCE)" : "");
|
||||||
|
|
||||||
/* Avoid trying to drop postgres db while we are connected to it. */
|
/* Avoid trying to drop postgres db while we are connected to it. */
|
||||||
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
|
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
|
||||||
@ -159,6 +166,7 @@ help(const char *progname)
|
|||||||
printf(_("\nOptions:\n"));
|
printf(_("\nOptions:\n"));
|
||||||
printf(_(" -e, --echo show the commands being sent to the server\n"));
|
printf(_(" -e, --echo show the commands being sent to the server\n"));
|
||||||
printf(_(" -i, --interactive prompt before deleting anything\n"));
|
printf(_(" -i, --interactive prompt before deleting anything\n"));
|
||||||
|
printf(_(" -f, --force try to terminate other connections before dropping\n"));
|
||||||
printf(_(" -V, --version output version information, then exit\n"));
|
printf(_(" -V, --version output version information, then exit\n"));
|
||||||
printf(_(" --if-exists don't report error if database doesn't exist\n"));
|
printf(_(" --if-exists don't report error if database doesn't exist\n"));
|
||||||
printf(_(" -?, --help show this help, then exit\n"));
|
printf(_(" -?, --help show this help, then exit\n"));
|
||||||
|
@ -3,7 +3,7 @@ use warnings;
|
|||||||
|
|
||||||
use PostgresNode;
|
use PostgresNode;
|
||||||
use TestLib;
|
use TestLib;
|
||||||
use Test::More tests => 11;
|
use Test::More tests => 13;
|
||||||
|
|
||||||
program_help_ok('dropdb');
|
program_help_ok('dropdb');
|
||||||
program_version_ok('dropdb');
|
program_version_ok('dropdb');
|
||||||
@ -19,5 +19,11 @@ $node->issues_sql_like(
|
|||||||
qr/statement: DROP DATABASE foobar1/,
|
qr/statement: DROP DATABASE foobar1/,
|
||||||
'SQL DROP DATABASE run');
|
'SQL DROP DATABASE run');
|
||||||
|
|
||||||
|
$node->safe_psql('postgres', 'CREATE DATABASE foobar2');
|
||||||
|
$node->issues_sql_like(
|
||||||
|
[ 'dropdb', '--force', 'foobar2' ],
|
||||||
|
qr/statement: DROP DATABASE foobar2 WITH \(FORCE\);/,
|
||||||
|
'SQL DROP DATABASE (FORCE) run');
|
||||||
|
|
||||||
$node->command_fails([ 'dropdb', 'nonexistent' ],
|
$node->command_fails([ 'dropdb', 'nonexistent' ],
|
||||||
'fails with nonexistent database');
|
'fails with nonexistent database');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user