Commit 7fe33a51 authored by Robert Haas's avatar Robert Haas

Add --if-exists option to dropdb and dropuser.

Josh Kupershmidt, with some further editing by me.
parent 94478aa8
...@@ -86,6 +86,16 @@ PostgreSQL documentation ...@@ -86,6 +86,16 @@ PostgreSQL documentation
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>--if-exists</></term>
<listitem>
<para>
Do not throw an error if the database does not exist. A notice is issued
in this case.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>-V</></term> <term><option>-V</></term>
<term><option>--version</></term> <term><option>--version</></term>
......
...@@ -88,6 +88,16 @@ PostgreSQL documentation ...@@ -88,6 +88,16 @@ PostgreSQL documentation
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>--if-exists</></term>
<listitem>
<para>
Do not throw an error if the database does not exist. A notice is
issued in this case.
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><option>-V</></term> <term><option>-V</></term>
<term><option>--version</></term> <term><option>--version</></term>
......
...@@ -21,6 +21,8 @@ static void help(const char *progname); ...@@ -21,6 +21,8 @@ static void help(const char *progname);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
static int if_exists = 0;
static struct option long_options[] = { static struct option long_options[] = {
{"host", required_argument, NULL, 'h'}, {"host", required_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'}, {"port", required_argument, NULL, 'p'},
...@@ -29,6 +31,7 @@ main(int argc, char *argv[]) ...@@ -29,6 +31,7 @@ main(int argc, char *argv[])
{"password", no_argument, NULL, 'W'}, {"password", no_argument, NULL, 'W'},
{"echo", no_argument, NULL, 'e'}, {"echo", no_argument, NULL, 'e'},
{"interactive", no_argument, NULL, 'i'}, {"interactive", no_argument, NULL, 'i'},
{"if-exists", no_argument, &if_exists, 1},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
...@@ -79,6 +82,9 @@ main(int argc, char *argv[]) ...@@ -79,6 +82,9 @@ main(int argc, char *argv[])
case 'i': case 'i':
interactive = true; interactive = true;
break; break;
case 0:
/* this covers the long options */
break;
default: default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1); exit(1);
...@@ -110,8 +116,8 @@ main(int argc, char *argv[]) ...@@ -110,8 +116,8 @@ main(int argc, char *argv[])
initPQExpBuffer(&sql); initPQExpBuffer(&sql);
appendPQExpBuffer(&sql, "DROP DATABASE %s;\n", appendPQExpBuffer(&sql, "DROP DATABASE %s%s;\n",
fmtId(dbname)); (if_exists ? "IF EXISTS " : ""), fmtId(dbname));
/* /*
* Connect to the 'postgres' database by default, except have the * Connect to the 'postgres' database by default, except have the
...@@ -146,6 +152,7 @@ help(const char *progname) ...@@ -146,6 +152,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(_(" --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"));
printf(_(" --version output version information, then exit\n")); printf(_(" --version output version information, then exit\n"));
printf(_("\nConnection options:\n")); printf(_("\nConnection options:\n"));
......
...@@ -21,6 +21,8 @@ static void help(const char *progname); ...@@ -21,6 +21,8 @@ static void help(const char *progname);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
static int if_exists = 0;
static struct option long_options[] = { static struct option long_options[] = {
{"host", required_argument, NULL, 'h'}, {"host", required_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'}, {"port", required_argument, NULL, 'p'},
...@@ -29,6 +31,7 @@ main(int argc, char *argv[]) ...@@ -29,6 +31,7 @@ main(int argc, char *argv[])
{"password", no_argument, NULL, 'W'}, {"password", no_argument, NULL, 'W'},
{"echo", no_argument, NULL, 'e'}, {"echo", no_argument, NULL, 'e'},
{"interactive", no_argument, NULL, 'i'}, {"interactive", no_argument, NULL, 'i'},
{"if-exists", no_argument, &if_exists, 1},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
...@@ -79,6 +82,9 @@ main(int argc, char *argv[]) ...@@ -79,6 +82,9 @@ main(int argc, char *argv[])
case 'i': case 'i':
interactive = true; interactive = true;
break; break;
case 0:
/* this covers the long options */
break;
default: default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1); exit(1);
...@@ -110,7 +116,8 @@ main(int argc, char *argv[]) ...@@ -110,7 +116,8 @@ main(int argc, char *argv[])
} }
initPQExpBuffer(&sql); initPQExpBuffer(&sql);
appendPQExpBuffer(&sql, "DROP ROLE %s;\n", fmtId(dropuser)); appendPQExpBuffer(&sql, "DROP ROLE %s%s;\n",
(if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
conn = connectDatabase("postgres", host, port, username, prompt_password, progname); conn = connectDatabase("postgres", host, port, username, prompt_password, progname);
...@@ -141,6 +148,7 @@ help(const char *progname) ...@@ -141,6 +148,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(_(" --if-exists don't report error if user doesn't exist\n"));
printf(_(" --help show this help, then exit\n")); printf(_(" --help show this help, then exit\n"));
printf(_(" --version output version information, then exit\n")); printf(_(" --version output version information, then exit\n"));
printf(_("\nConnection options:\n")); printf(_("\nConnection options:\n"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment