drop database if exists - Mailing list pgsql-patches
From | Andrew Dunstan |
---|---|
Subject | drop database if exists |
Date | |
Msg-id | 4381D7B4.60908@dunslane.net Whole thread Raw |
Responses |
Re: drop database if exists
|
List | pgsql-patches |
here's a patch for "drop database if exists". Barring objections I will apply it in a day or two. cheers andrew Index: doc/src/sgml/ref/drop_database.sgml =================================================================== RCS file: /cvsroot/pgsql/doc/src/sgml/ref/drop_database.sgml,v retrieving revision 1.20 diff -c -r1.20 drop_database.sgml *** doc/src/sgml/ref/drop_database.sgml 21 Jun 2005 04:02:31 -0000 1.20 --- doc/src/sgml/ref/drop_database.sgml 21 Nov 2005 13:39:55 -0000 *************** *** 20,26 **** <refsynopsisdiv> <synopsis> ! DROP DATABASE <replaceable class="PARAMETER">name</replaceable> </synopsis> </refsynopsisdiv> --- 20,26 ---- <refsynopsisdiv> <synopsis> ! DROP DATABASE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable> </synopsis> </refsynopsisdiv> *************** *** 46,51 **** --- 46,61 ---- <variablelist> <varlistentry> + <term><literal>IF EXISTS</literal></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> <term><replaceable class="PARAMETER">name</replaceable></term> <listitem> <para> Index: src/backend/commands/dbcommands.c =================================================================== RCS file: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v retrieving revision 1.173 diff -c -r1.173 dbcommands.c *** src/backend/commands/dbcommands.c 15 Oct 2005 02:49:15 -0000 1.173 --- src/backend/commands/dbcommands.c 21 Nov 2005 13:39:55 -0000 *************** *** 551,557 **** * DROP DATABASE */ void ! dropdb(const char *dbname) { Oid db_id; bool db_istemplate; --- 551,557 ---- * DROP DATABASE */ void ! dropdb(const char *dbname, bool missing_ok) { Oid db_id; bool db_istemplate; *************** *** 585,593 **** if (!get_db_info(dbname, &db_id, NULL, NULL, &db_istemplate, NULL, NULL, NULL, NULL, NULL)) ! ereport(ERROR, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database \"%s\" does not exist", dbname))); if (!pg_database_ownercheck(db_id, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE, --- 585,609 ---- if (!get_db_info(dbname, &db_id, NULL, NULL, &db_istemplate, NULL, NULL, NULL, NULL, NULL)) ! { ! if (! missing_ok) ! { ! ereport(ERROR, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database \"%s\" does not exist", dbname))); + } + else + { + + /* Close pg_database, release the lock, since we changed nothing */ + heap_close(pgdbrel, ExclusiveLock); + ereport(NOTICE, + (errmsg("database \"%s\" does not exist, skipping", + dbname))); + + return; + } + } if (!pg_database_ownercheck(db_id, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE, Index: src/backend/parser/gram.y =================================================================== RCS file: /cvsroot/pgsql/src/backend/parser/gram.y,v retrieving revision 2.514 diff -c -r2.514 gram.y *** src/backend/parser/gram.y 21 Nov 2005 12:49:31 -0000 2.514 --- src/backend/parser/gram.y 21 Nov 2005 13:40:01 -0000 *************** *** 4698,4704 **** /***************************************************************************** * ! * DROP DATABASE * * This is implicitly CASCADE, no need for drop behavior *****************************************************************************/ --- 4698,4704 ---- /***************************************************************************** * ! * DROP DATABASE [ IF EXISTS ] * * This is implicitly CASCADE, no need for drop behavior *****************************************************************************/ *************** *** 4707,4712 **** --- 4707,4720 ---- { DropdbStmt *n = makeNode(DropdbStmt); n->dbname = $3; + n->missing_ok = FALSE; + $$ = (Node *)n; + } + | DROP DATABASE IF_P EXISTS database_name + { + DropdbStmt *n = makeNode(DropdbStmt); + n->dbname = $5; + n->missing_ok = TRUE; $$ = (Node *)n; } ; Index: src/backend/tcop/utility.c =================================================================== RCS file: /cvsroot/pgsql/src/backend/tcop/utility.c,v retrieving revision 1.247 diff -c -r1.247 utility.c *** src/backend/tcop/utility.c 21 Nov 2005 12:49:32 -0000 1.247 --- src/backend/tcop/utility.c 21 Nov 2005 13:40:02 -0000 *************** *** 840,846 **** { DropdbStmt *stmt = (DropdbStmt *) parsetree; ! dropdb(stmt->dbname); } break; --- 840,846 ---- { DropdbStmt *stmt = (DropdbStmt *) parsetree; ! dropdb(stmt->dbname, stmt->missing_ok); } break; Index: src/include/commands/dbcommands.h =================================================================== RCS file: /cvsroot/pgsql/src/include/commands/dbcommands.h,v retrieving revision 1.42 diff -c -r1.42 dbcommands.h *** src/include/commands/dbcommands.h 15 Oct 2005 02:49:44 -0000 1.42 --- src/include/commands/dbcommands.h 21 Nov 2005 13:40:03 -0000 *************** *** 53,59 **** } xl_dbase_drop_rec; extern void createdb(const CreatedbStmt *stmt); ! extern void dropdb(const char *dbname); extern void RenameDatabase(const char *oldname, const char *newname); extern void AlterDatabase(AlterDatabaseStmt *stmt); extern void AlterDatabaseSet(AlterDatabaseSetStmt *stmt); --- 53,59 ---- } xl_dbase_drop_rec; extern void createdb(const CreatedbStmt *stmt); ! extern void dropdb(const char *dbname, bool missing_ok); extern void RenameDatabase(const char *oldname, const char *newname); extern void AlterDatabase(AlterDatabaseStmt *stmt); extern void AlterDatabaseSet(AlterDatabaseSetStmt *stmt); Index: src/include/nodes/parsenodes.h =================================================================== RCS file: /cvsroot/pgsql/src/include/nodes/parsenodes.h,v retrieving revision 1.294 diff -c -r1.294 parsenodes.h *** src/include/nodes/parsenodes.h 21 Nov 2005 12:49:32 -0000 1.294 --- src/include/nodes/parsenodes.h 21 Nov 2005 13:40:04 -0000 *************** *** 1672,1677 **** --- 1672,1678 ---- { NodeTag type; char *dbname; /* database to drop */ + bool missing_ok; /* skip error if db is missing? */ } DropdbStmt; /* ----------------------
pgsql-patches by date: