st 13. 11. 2019 v 7:13 odesÃlatel Pavel Stehule <pavel.steh...@gmail.com> napsal:
> > > st 13. 11. 2019 v 7:12 odesÃlatel Amit Kapila <amit.kapil...@gmail.com> > napsal: > >> On Tue, Nov 12, 2019 at 11:17 AM Amit Kapila <amit.kapil...@gmail.com> >> wrote: >> > >> > I am planning to commit this patch tomorrow unless I see more comments >> > or interest from someone else to review this. >> > >> >> Pushed. Pavel, feel free to submit dropdb utility-related patch if you >> want. >> > > I hope I send this patch today. It's simply job. > here it is. It's based on Filip Rembialkowski's patch if I remember correctly Pavel > Pavel > > >> -- >> With Regards, >> Amit Kapila. >> EnterpriseDB: http://www.enterprisedb.com >> >
diff --git a/doc/src/sgml/ref/dropdb.sgml b/doc/src/sgml/ref/dropdb.sgml index 3fbdb33716..5d10ad1c92 100644 --- a/doc/src/sgml/ref/dropdb.sgml +++ b/doc/src/sgml/ref/dropdb.sgml @@ -86,6 +86,20 @@ PostgreSQL documentation </listitem> </varlistentry> + <varlistentry> + <term><option>-f</option></term> + <term><option>--force</option></term> + <listitem> + <para> + Force termination of connected backends before removing the database. + </para> + <para> + This will add the <literal>FORCE</literal> option to the <literal>DROP + DATABASE</literal> command sent to the server. + </para> + </listitem> + </varlistentry> + <varlistentry> <term><option>-V</option></term> <term><option>--version</option></term> diff --git a/src/bin/scripts/dropdb.c b/src/bin/scripts/dropdb.c index dacd8e5f1d..3a6aac8ac3 100644 --- a/src/bin/scripts/dropdb.c +++ b/src/bin/scripts/dropdb.c @@ -34,6 +34,7 @@ main(int argc, char *argv[]) {"interactive", no_argument, NULL, 'i'}, {"if-exists", no_argument, &if_exists, 1}, {"maintenance-db", required_argument, NULL, 2}, + {"force", no_argument, NULL, 'f'}, {NULL, 0, NULL, 0} }; @@ -49,6 +50,7 @@ main(int argc, char *argv[]) enum trivalue prompt_password = TRI_DEFAULT; bool echo = false; bool interactive = false; + bool force = false; PQExpBufferData sql; @@ -61,7 +63,7 @@ main(int argc, char *argv[]) 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) { @@ -86,6 +88,9 @@ main(int argc, char *argv[]) case 'i': interactive = true; break; + case 'f': + force = true; + break; case 0: /* this covers the long options */ break; @@ -123,9 +128,6 @@ main(int argc, char *argv[]) initPQExpBuffer(&sql); - appendPQExpBuffer(&sql, "DROP DATABASE %s%s;", - (if_exists ? "IF EXISTS " : ""), fmtId(dbname)); - /* Avoid trying to drop postgres db while we are connected to it. */ if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0) maintenance_db = "template1"; @@ -134,6 +136,12 @@ main(int argc, char *argv[]) host, port, username, prompt_password, progname, echo); + /* now, only FORCE option can be used, so usage is very simple */ + appendPQExpBuffer(&sql, "DROP DATABASE %s%s%s;", + (if_exists ? "IF EXISTS " : ""), + fmtId(dbname), + force ? " WITH (FORCE)" : ""); + if (echo) printf("%s\n", sql.data); result = PQexec(conn, sql.data); @@ -159,6 +167,7 @@ help(const char *progname) printf(_("\nOptions:\n")); printf(_(" -e, --echo show the commands being sent to the server\n")); printf(_(" -i, --interactive prompt before deleting anything\n")); + printf(_(" -f, --force force termination of connected backends\n")); printf(_(" -V, --version output version information, then exit\n")); printf(_(" --if-exists don't report error if database doesn't exist\n")); printf(_(" -?, --help show this help, then exit\n")); diff --git a/src/bin/scripts/t/050_dropdb.pl b/src/bin/scripts/t/050_dropdb.pl index 25aa54a4ae..c51babe093 100644 --- a/src/bin/scripts/t/050_dropdb.pl +++ b/src/bin/scripts/t/050_dropdb.pl @@ -3,7 +3,7 @@ use warnings; use PostgresNode; use TestLib; -use Test::More tests => 11; +use Test::More tests => 13; program_help_ok('dropdb'); program_version_ok('dropdb'); @@ -19,5 +19,11 @@ $node->issues_sql_like( qr/statement: DROP DATABASE foobar1/, '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' ], 'fails with nonexistent database');