I noticed a few places where it would be handy if dropdb took a flag
like "--if-exists" which would basically just add in the 'IF EXISTS'
clause to the DROP DATABASE statement. For example, scripts like
find_static or mbregress.sh use dropdb && createdb, but they generate
noisy errors from dropdb when run for the first time since there's no
--if-exists flag. (They could just pipe 'DROP DATABASE IF EXISTS ...'
to psql, but what's the point of having dropdb if it's not used?)
Attached is a very quick patch implementing the "--if-exists" or "-X"
option for dropdb and dropuser. I didn't bother adding in a check to
make sure the server version was 8.2+ since we're not even supporting
8.1 nowadays, though that'd be easy enough to add in.
Josh
diff --git a/doc/src/sgml/ref/dropdb.sgml b/doc/src/sgml/ref/dropdb.sgml
index e20bcdb..2092bb6 100644
*** a/doc/src/sgml/ref/dropdb.sgml
--- b/doc/src/sgml/ref/dropdb.sgml
*************** PostgreSQL documentation
*** 87,92 ****
--- 87,102 ----
</varlistentry>
<varlistentry>
+ <term><option>-X</></term>
+ <term><option>--if-exists</></term>
+ <listitem>
+ <para>
+ Don't report an error if the specified database does not exist.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><option>-V</></term>
<term><option>--version</></term>
<listitem>
diff --git a/doc/src/sgml/ref/dropuser.sgml b/doc/src/sgml/ref/dropuser.sgml
index c158103..22580a4 100644
*** a/doc/src/sgml/ref/dropuser.sgml
--- b/doc/src/sgml/ref/dropuser.sgml
*************** PostgreSQL documentation
*** 89,94 ****
--- 89,104 ----
</varlistentry>
<varlistentry>
+ <term><option>-X</></term>
+ <term><option>--if-exists</></term>
+ <listitem>
+ <para>
+ Don't report an error if the specified user does not exist.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><option>-V</></term>
<term><option>--version</></term>
<listitem>
diff --git a/src/bin/scripts/dropdb.c b/src/bin/scripts/dropdb.c
index 4cec63e..187bf6c 100644
*** a/src/bin/scripts/dropdb.c
--- b/src/bin/scripts/dropdb.c
*************** main(int argc, char *argv[])
*** 29,34 ****
--- 29,35 ----
{"password", no_argument, NULL, 'W'},
{"echo", no_argument, NULL, 'e'},
{"interactive", no_argument, NULL, 'i'},
+ {"if-exists", no_argument, NULL, 'X'},
{NULL, 0, NULL, 0}
};
*************** main(int argc, char *argv[])
*** 43,48 ****
--- 44,50 ----
enum trivalue prompt_password = TRI_DEFAULT;
bool echo = false;
bool interactive = false;
+ bool if_exists = false;
PQExpBufferData sql;
*************** main(int argc, char *argv[])
*** 54,60 ****
handle_help_version_opts(argc, argv, "dropdb", help);
! while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
{
switch (c)
{
--- 56,62 ----
handle_help_version_opts(argc, argv, "dropdb", help);
! while ((c = getopt_long(argc, argv, "h:p:U:wWeiX", long_options, &optindex)) != -1)
{
switch (c)
{
*************** main(int argc, char *argv[])
*** 79,84 ****
--- 81,89 ----
case 'i':
interactive = true;
break;
+ case 'X':
+ if_exists = true;
+ break;
default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
*************** main(int argc, char *argv[])
*** 110,117 ****
initPQExpBuffer(&sql);
! appendPQExpBuffer(&sql, "DROP DATABASE %s;\n",
! fmtId(dbname));
/*
* Connect to the 'postgres' database by default, except have the
--- 115,122 ----
initPQExpBuffer(&sql);
! appendPQExpBuffer(&sql, "DROP DATABASE %s%s;\n",
! (if_exists ? "IF EXISTS " : ""), fmtId(dbname));
/*
* Connect to the 'postgres' database by default, except have the
*************** help(const char *progname)
*** 146,151 ****
--- 151,157 ----
printf(_("\nOptions:\n"));
printf(_(" -e, --echo show the commands being sent to the server\n"));
printf(_(" -i, --interactive prompt before deleting anything\n"));
+ printf(_(" -X, --if-exists don't report error if database doesn't exist\n"));
printf(_(" --help show this help, then exit\n"));
printf(_(" --version output version information, then exit\n"));
printf(_("\nConnection options:\n"));
diff --git a/src/bin/scripts/dropuser.c b/src/bin/scripts/dropuser.c
index 0949a5e..bf5196f 100644
*** a/src/bin/scripts/dropuser.c
--- b/src/bin/scripts/dropuser.c
*************** main(int argc, char *argv[])
*** 29,34 ****
--- 29,35 ----
{"password", no_argument, NULL, 'W'},
{"echo", no_argument, NULL, 'e'},
{"interactive", no_argument, NULL, 'i'},
+ {"if-exists", no_argument, NULL, 'X'},
{NULL, 0, NULL, 0}
};
*************** main(int argc, char *argv[])
*** 43,48 ****
--- 44,50 ----
enum trivalue prompt_password = TRI_DEFAULT;
bool echo = false;
bool interactive = false;
+ bool if_exists = false;
PQExpBufferData sql;
*************** main(int argc, char *argv[])
*** 54,60 ****
handle_help_version_opts(argc, argv, "dropuser", help);
! while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
{
switch (c)
{
--- 56,62 ----
handle_help_version_opts(argc, argv, "dropuser", help);
! while ((c = getopt_long(argc, argv, "h:p:U:wWeiX", long_options, &optindex)) != -1)
{
switch (c)
{
*************** main(int argc, char *argv[])
*** 79,84 ****
--- 81,89 ----
case 'i':
interactive = true;
break;
+ case 'X':
+ if_exists = true;
+ break;
default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
*************** main(int argc, char *argv[])
*** 110,116 ****
}
initPQExpBuffer(&sql);
! appendPQExpBuffer(&sql, "DROP ROLE %s;\n", fmtId(dropuser));
conn = connectDatabase("postgres", host, port, username, prompt_password, progname);
--- 115,122 ----
}
initPQExpBuffer(&sql);
! appendPQExpBuffer(&sql, "DROP ROLE %s%s;\n",
! (if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
conn = connectDatabase("postgres", host, port, username, prompt_password, progname);
*************** help(const char *progname)
*** 141,146 ****
--- 147,153 ----
printf(_("\nOptions:\n"));
printf(_(" -e, --echo show the commands being sent to the server\n"));
printf(_(" -i, --interactive prompt before deleting anything\n"));
+ printf(_(" -X, --if-exists don't report error if user doesn't exist\n"));
printf(_(" --help show this help, then exit\n"));
printf(_(" --version output version information, then exit\n"));
printf(_("\nConnection options:\n"));
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers