From: Andrew Dunstan Date: Sat, 4 Feb 2006 19:06:47 +0000 (+0000) Subject: DROP IF EXISTS for ROLE/USER/GROUP X-Git-Tag: REL9_0_0~8473 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=f8b54fe6ed7a2eae575d365091d136b4a4068316;p=pg-rex%2Fsyncrep.git DROP IF EXISTS for ROLE/USER/GROUP --- diff --git a/doc/src/sgml/ref/drop_group.sgml b/doc/src/sgml/ref/drop_group.sgml index 1decf950d6..e19c0a1e47 100644 --- a/doc/src/sgml/ref/drop_group.sgml +++ b/doc/src/sgml/ref/drop_group.sgml @@ -1,5 +1,5 @@ @@ -20,7 +20,7 @@ PostgreSQL documentation -DROP GROUP name [, ...] +DROP GROUP [ IF EXISTS ] name [, ...] diff --git a/doc/src/sgml/ref/drop_role.sgml b/doc/src/sgml/ref/drop_role.sgml index dc8a0896de..d2364dabf4 100644 --- a/doc/src/sgml/ref/drop_role.sgml +++ b/doc/src/sgml/ref/drop_role.sgml @@ -1,5 +1,5 @@ @@ -20,7 +20,7 @@ PostgreSQL documentation -DROP ROLE name [, ...] +DROP ROLE [ IF EXISTS ] name [, ...] @@ -54,6 +54,17 @@ DROP ROLE name [, ...] + IF EXISTS + + + Do not throw an error if the role does not exist. A notice is issued + in this case. + + + + + + name diff --git a/doc/src/sgml/ref/drop_user.sgml b/doc/src/sgml/ref/drop_user.sgml index 419a7eda7c..7ed3573455 100644 --- a/doc/src/sgml/ref/drop_user.sgml +++ b/doc/src/sgml/ref/drop_user.sgml @@ -1,5 +1,5 @@ @@ -20,7 +20,7 @@ PostgreSQL documentation -DROP USER name [, ...] +DROP USER [ IF EXISTS ] name [, ...] diff --git a/src/backend/commands/user.c b/src/backend/commands/user.c index 0ffcc21501..b3aa2ed829 100644 --- a/src/backend/commands/user.c +++ b/src/backend/commands/user.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.167 2005/12/23 16:46:39 petere Exp $ + * $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.168 2006/02/04 19:06:46 adunstan Exp $ * *------------------------------------------------------------------------- */ @@ -840,9 +840,22 @@ DropRole(DropRoleStmt *stmt) PointerGetDatum(role), 0, 0, 0); if (!HeapTupleIsValid(tuple)) - ereport(ERROR, - (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("role \"%s\" does not exist", role))); + { + if (!stmt->missing_ok) + { + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("role \"%s\" does not exist", role))); + } + else + { + ereport(NOTICE, + (errmsg("role \"%s\" does not exist, skipping", + role))); + } + + continue; + } roleid = HeapTupleGetOid(tuple); diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index 19b987908b..6578bf37af 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -15,7 +15,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.325 2006/01/31 21:39:23 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.326 2006/02/04 19:06:46 adunstan Exp $ * *------------------------------------------------------------------------- */ @@ -2518,6 +2518,7 @@ _copyDropRoleStmt(DropRoleStmt *from) DropRoleStmt *newnode = makeNode(DropRoleStmt); COPY_NODE_FIELD(roles); + COPY_SCALAR_FIELD(missing_ok); return newnode; } diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c index e7a9ced0ed..a9fdc95f6b 100644 --- a/src/backend/nodes/equalfuncs.c +++ b/src/backend/nodes/equalfuncs.c @@ -18,7 +18,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.261 2006/01/31 21:39:23 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.262 2006/02/04 19:06:46 adunstan Exp $ * *------------------------------------------------------------------------- */ @@ -1398,6 +1398,7 @@ static bool _equalDropRoleStmt(DropRoleStmt *a, DropRoleStmt *b) { COMPARE_NODE_FIELD(roles); + COMPARE_SCALAR_FIELD(missing_ok); return true; } diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index bda5c93254..8c21c42158 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -11,7 +11,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.525 2006/01/31 22:40:03 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.526 2006/02/04 19:06:46 adunstan Exp $ * * HISTORY * AUTHOR DATE MAJOR EVENT @@ -824,9 +824,17 @@ DropRoleStmt: DROP ROLE name_list { DropRoleStmt *n = makeNode(DropRoleStmt); + n->missing_ok = FALSE; n->roles = $3; $$ = (Node *)n; } + | DROP ROLE IF_P EXISTS name_list + { + DropRoleStmt *n = makeNode(DropRoleStmt); + n->missing_ok = TRUE; + n->roles = $5; + $$ = (Node *)n; + } ; /***************************************************************************** @@ -842,9 +850,17 @@ DropUserStmt: DROP USER name_list { DropRoleStmt *n = makeNode(DropRoleStmt); + n->missing_ok = FALSE; n->roles = $3; $$ = (Node *)n; } + | DROP USER IF_P EXISTS name_list + { + DropRoleStmt *n = makeNode(DropRoleStmt); + n->roles = $5; + n->missing_ok = TRUE; + $$ = (Node *)n; + } ; @@ -900,9 +916,17 @@ DropGroupStmt: DROP GROUP_P name_list { DropRoleStmt *n = makeNode(DropRoleStmt); + n->missing_ok = FALSE; n->roles = $3; $$ = (Node *)n; } + | DROP GROUP_P IF_P EXISTS name_list + { + DropRoleStmt *n = makeNode(DropRoleStmt); + n->missing_ok = TRUE; + n->roles = $5; + $$ = (Node *)n; + } ; diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index e738ec2b03..0efe47fc6e 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.299 2006/01/21 02:16:20 momjian Exp $ + * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.300 2006/02/04 19:06:46 adunstan Exp $ * *------------------------------------------------------------------------- */ @@ -1193,6 +1193,7 @@ typedef struct DropRoleStmt { NodeTag type; List *roles; /* List of roles to remove */ + bool missing_ok; /* skip error if a role is missing? */ } DropRoleStmt; /* ---------------------- diff --git a/src/test/regress/expected/drop_if_exists.out b/src/test/regress/expected/drop_if_exists.out index 10feb3f986..092c90403a 100644 --- a/src/test/regress/expected/drop_if_exists.out +++ b/src/test/regress/expected/drop_if_exists.out @@ -65,3 +65,27 @@ ERROR: type "test_domain_exists" does not exist DROP TABLE IF EXISTS test_exists; DROP TABLE test_exists; ERROR: table "test_exists" does not exist +--- +--- role/user/group +--- +CREATE USER tu1; +CREATE ROLE tr1; +CREATE GROUP tg1; +DROP USER tu2; +ERROR: role "tu2" does not exist +DROP USER IF EXISTS tu1, tu2; +NOTICE: role "tu2" does not exist, skipping +DROP USER tu1; +ERROR: role "tu1" does not exist +DROP ROLE tr2; +ERROR: role "tr2" does not exist +DROP ROLE IF EXISTS tr1, tr2; +NOTICE: role "tr2" does not exist, skipping +DROP ROLE tr1; +ERROR: role "tr1" does not exist +DROP GROUP tg2; +ERROR: role "tg2" does not exist +DROP GROUP IF EXISTS tg1, tg2; +NOTICE: role "tg2" does not exist, skipping +DROP GROUP tg1; +ERROR: role "tg1" does not exist diff --git a/src/test/regress/sql/drop_if_exists.sql b/src/test/regress/sql/drop_if_exists.sql index 7addedaa73..ae7543fc43 100644 --- a/src/test/regress/sql/drop_if_exists.sql +++ b/src/test/regress/sql/drop_if_exists.sql @@ -89,3 +89,30 @@ DROP TABLE IF EXISTS test_exists; DROP TABLE test_exists; + +--- +--- role/user/group +--- + +CREATE USER tu1; +CREATE ROLE tr1; +CREATE GROUP tg1; + +DROP USER tu2; + +DROP USER IF EXISTS tu1, tu2; + +DROP USER tu1; + +DROP ROLE tr2; + +DROP ROLE IF EXISTS tr1, tr2; + +DROP ROLE tr1; + +DROP GROUP tg2; + +DROP GROUP IF EXISTS tg1, tg2; + +DROP GROUP tg1; +