Hello

this is relative simple patch that add possibility to skip noexisting
tables. It is necessary for silent cleaning when dump is loaded.

Regards

Pavel Stehule
*** ./doc/src/sgml/ref/alter_table.sgml.orig	2011-12-01 22:47:20.000000000 +0100
--- ./doc/src/sgml/ref/alter_table.sgml	2012-01-02 13:59:16.390363069 +0100
***************
*** 21,33 ****
  
   <refsynopsisdiv>
  <synopsis>
! ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
      <replaceable class="PARAMETER">action</replaceable> [, ... ]
! ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
      RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable class="PARAMETER">new_column</replaceable>
! ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
      RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
! ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
      SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
  
  <phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
--- 21,33 ----
  
   <refsynopsisdiv>
  <synopsis>
! ALTER TABLE [ IF EXISTS ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
      <replaceable class="PARAMETER">action</replaceable> [, ... ]
! ALTER TABLE [ IF EXISTS ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
      RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable class="PARAMETER">new_column</replaceable>
! ALTER TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
      RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
! ALTER TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
      SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
  
  <phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
*** ./src/backend/nodes/copyfuncs.c.orig	2011-12-01 22:47:20.000000000 +0100
--- ./src/backend/nodes/copyfuncs.c	2012-01-02 11:16:05.521002912 +0100
***************
*** 2517,2522 ****
--- 2517,2523 ----
  	COPY_NODE_FIELD(relation);
  	COPY_NODE_FIELD(cmds);
  	COPY_SCALAR_FIELD(relkind);
+ 	COPY_SCALAR_FIELD(missing_ok);
  
  	return newnode;
  }
*** ./src/backend/nodes/equalfuncs.c.orig	2011-12-01 22:47:20.000000000 +0100
--- ./src/backend/nodes/equalfuncs.c	2012-01-02 11:16:33.871001059 +0100
***************
*** 1009,1014 ****
--- 1009,1015 ----
  	COMPARE_NODE_FIELD(relation);
  	COMPARE_NODE_FIELD(cmds);
  	COMPARE_SCALAR_FIELD(relkind);
+ 	COMPARE_SCALAR_FIELD(missing_ok);
  
  	return true;
  }
*** ./src/backend/parser/gram.y.orig	2011-12-01 22:47:20.000000000 +0100
--- ./src/backend/parser/gram.y	2012-01-02 13:53:13.001386815 +0100
***************
*** 1621,1626 ****
--- 1621,1636 ----
  					n->relation = $3;
  					n->cmds = $4;
  					n->relkind = OBJECT_TABLE;
+ 					n->missing_ok = false;
+ 					$$ = (Node *)n;
+ 				}
+ 		 |	ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
+ 				{
+ 					AlterTableStmt *n = makeNode(AlterTableStmt);
+ 					n->relation = $5;
+ 					n->cmds = $6;
+ 					n->relkind = OBJECT_TABLE;
+ 					n->missing_ok = true;
  					$$ = (Node *)n;
  				}
  		|	ALTER INDEX qualified_name alter_table_cmds
***************
*** 1629,1634 ****
--- 1639,1645 ----
  					n->relation = $3;
  					n->cmds = $4;
  					n->relkind = OBJECT_INDEX;
+ 					n->missing_ok = false;
  					$$ = (Node *)n;
  				}
  		|	ALTER SEQUENCE qualified_name alter_table_cmds
***************
*** 1637,1642 ****
--- 1648,1654 ----
  					n->relation = $3;
  					n->cmds = $4;
  					n->relkind = OBJECT_SEQUENCE;
+ 					n->missing_ok = false;
  					$$ = (Node *)n;
  				}
  		|	ALTER VIEW qualified_name alter_table_cmds
***************
*** 1645,1650 ****
--- 1657,1663 ----
  					n->relation = $3;
  					n->cmds = $4;
  					n->relkind = OBJECT_VIEW;
+ 					n->missing_ok = false;
  					$$ = (Node *)n;
  				}
  		;
*** ./src/backend/parser/parse_utilcmd.c.orig	2011-12-01 22:47:20.000000000 +0100
--- ./src/backend/parser/parse_utilcmd.c	2012-01-02 13:47:11.937410429 +0100
***************
*** 2256,2262 ****
  	 * new commands we add after this must not upgrade the lock level
  	 * requested here.
  	 */
! 	rel = relation_openrv(stmt->relation, lockmode);
  
  	/* Set up pstate and CreateStmtContext */
  	pstate = make_parsestate(NULL);
--- 2256,2278 ----
  	 * new commands we add after this must not upgrade the lock level
  	 * requested here.
  	 */
! 
! 	if (stmt->missing_ok)
! 	{
! 		rel = try_relation_openrv(stmt->relation, lockmode);
! 
! 		/* leave when relation doesn't exists */
! 		if (rel == NULL)
! 		{
! 			ereport(NOTICE,
! 					(errcode(ERRCODE_DUPLICATE_TABLE),
! 					 errmsg("table \"%s\" does not exists, skipping",
! 							stmt->relation->relname)));
! 			return NIL;
! 		}
! 	}
! 	else
! 		rel = relation_openrv(stmt->relation, lockmode);
  
  	/* Set up pstate and CreateStmtContext */
  	pstate = make_parsestate(NULL);
*** ./src/include/nodes/parsenodes.h.orig	2011-12-01 22:47:20.000000000 +0100
--- ./src/include/nodes/parsenodes.h	2012-01-02 11:15:04.643006891 +0100
***************
*** 1169,1174 ****
--- 1169,1175 ----
  	RangeVar   *relation;		/* table to work on */
  	List	   *cmds;			/* list of subcommands */
  	ObjectType	relkind;		/* type of object */
+ 	bool	   missing_ok;		/* skip error if table missing */
  } AlterTableStmt;
  
  typedef enum AlterTableType
*** ./src/test/regress/expected/alter_table.out.orig	2011-12-01 22:47:20.000000000 +0100
--- ./src/test/regress/expected/alter_table.out	2012-01-02 14:04:03.000000000 +0100
***************
*** 2002,2004 ****
--- 2002,2018 ----
   x      | integer      | 
   y      | numeric(8,2) | 
  
+ --
+ -- IF EXISTS test
+ --
+ ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
+ NOTICE:  table "tt8" does not exists, skipping
+ CREATE TABLE tt8(a int);
+ ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
+ \d tt8
+       Table "public.tt8"
+  Column |  Type   | Modifiers 
+ --------+---------+-----------
+  a      | integer | 
+  f      | integer | 
+ 
*** ./src/test/regress/sql/alter_table.sql.orig	2011-12-01 22:47:20.000000000 +0100
--- ./src/test/regress/sql/alter_table.sql	2012-01-02 14:03:34.319346213 +0100
***************
*** 1410,1412 ****
--- 1410,1421 ----
  ALTER TABLE tt7 OF tt_t1;			-- reassign an already-typed table
  ALTER TABLE tt7 NOT OF;
  \d tt7
+ 
+ --
+ -- IF EXISTS test
+ --
+ ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
+ 
+ CREATE TABLE tt8(a int);
+ ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
+ \d tt8
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to