From 289a826dab09844cb7df41828d60ac7aef35125c Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Thu, 21 Jan 1999 22:53:37 +0000
Subject: [PATCH] Here is a new version of my patch for allowing pg_dump to
 DROP schema elements prior to CREATEing new ones.  It is under control of the
 -c command line option (with the default being status quo).

The DROP TRIGGER portion still needs implementation.  Anyone able to
help clarify what exactly the CREATE TRIGGER portion does so I can fix
this?

Again, I have tried this with tables/indexes/sequences, but do not
have other schema elements in my database.  As a result, I am not 100%
convinced that I got the syntax correct in all cases (but think I did,
nonetheless).  If anyone can check the other cases, I'd appreciate it.

Cheers,
Brook

[I added manual page and sgml additions for the new -c option.]
---
 doc/src/sgml/ref/pg_dump.sgml | 13 +++++-
 src/bin/pg_dump/pg_dump.c     | 83 ++++++++++++++++++++++++++++++++++-
 src/man/pg_dump.1             |  8 +++-
 3 files changed, 100 insertions(+), 4 deletions(-)

diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml
index 1f55f58754e..8fe0bf47897 100644
--- a/doc/src/sgml/ref/pg_dump.sgml
+++ b/doc/src/sgml/ref/pg_dump.sgml
@@ -22,7 +22,7 @@ pg_dump [ <replaceable class="parameter">dbname</replaceable> ]
 pg_dump [ -h <replaceable class="parameter">host</replaceable> ] [ -p <replaceable class="parameter">port</replaceable> ]
     [ -t <replaceable class="parameter">table</replaceable> ]
     [ -f <replaceable class="parameter">outputfile</replaceable> ]
-    [ -a ] [ -d ] [ -D ] [ -n ] [ -N ] [ -o ] [ -s ] [ -u ] [ -v ] [ -z ]
+    [ -a ] [ -c ] [ -d ] [ -D ] [ -n ] [ -N ] [ -o ] [ -s ] [ -u ] [ -v ] [ -z ]
     [ <replaceable class="parameter">dbname</replaceable> ]
 </SYNOPSIS>
 
@@ -63,6 +63,17 @@ pg_dump [ -h <replaceable class="parameter">host</replaceable> ] [ -p <replaceab
 	    </listitem>
 	  </varlistentry>
 	  
+	  <varlistentry>
+	    <term>
+	      -c
+	    </term>
+	    <listitem>
+	      <para>
+		Clean(drop) schema prior to create.
+	      </para>
+	    </listitem>
+	  </varlistentry>
+	  
 	  <varlistentry>
 	    <term>
 	      -d
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index dc8f30a1144..6b9095bcc40 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -21,7 +21,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.99 1999/01/18 06:32:26 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.100 1999/01/21 22:53:36 momjian Exp $
  *
  * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
  *
@@ -117,6 +117,7 @@ int		attrNames;			/* put attr names into insert strings */
 int		schemaOnly;
 int		dataOnly;
 int		aclsOption;
+bool		drop_schema;
 
 char		g_opaque_type[10];		/* name for the opaque type */
 
@@ -132,6 +133,8 @@ usage(const char *progname)
 			"usage:  %s [options] dbname\n", progname);
 	fprintf(stderr,
 			"\t -a          \t\t dump out only the data, no schema\n");
+	fprintf(stderr,
+			"\t -c          \t\t clean(drop) schema prior to create\n");
 	fprintf(stderr,
 			"\t -d          \t\t dump data as proper insert strings\n");
 	fprintf(stderr,
@@ -556,6 +559,7 @@ main(int argc, char **argv)
 
 	g_verbose = false;
 	force_quotes = true;
+	drop_schema = false;
 
 	strcpy(g_comment_start, "-- ");
 	g_comment_end[0] = '\0';
@@ -565,13 +569,16 @@ main(int argc, char **argv)
 
 	progname = *argv;
 
-	while ((c = getopt(argc, argv, "adDf:h:nNop:st:vzu")) != EOF)
+	while ((c = getopt(argc, argv, "acdDf:h:nNop:st:vzu")) != EOF)
 	{
 		switch (c)
 		{
 			case 'a':			/* Dump data only */
 				dataOnly = 1;
 				break;
+			case 'c':			/* clean (i.e., drop) schema prior to create */
+				drop_schema = true;
+				break;
 			case 'd':			/* dump data as proper insert strings */
 				dumpData = 1;
 				break;
@@ -1638,6 +1645,18 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
 					exit_nicely(g_conn);
 				}
 				tgfunc = finfo[findx].proname;
+
+#if 0				
+				/* XXX - how to emit this DROP TRIGGER? */
+				if (drop_schema)
+				  {
+				    sprintf(query, "DROP TRIGGER %s ON %s;\n",
+					    fmtId(PQgetvalue(res2, i2, i_tgname), force_quotes),
+					    fmtId(tblinfo[i].relname, force_quotes));
+				    fputs(query, fout);
+				  }
+#endif
+
 				sprintf(query, "CREATE TRIGGER %s ", fmtId(PQgetvalue(res2, i2, i_tgname), force_quotes));
 				/* Trigger type */
 				findx = 0;
@@ -2034,6 +2053,12 @@ dumpTypes(FILE *fout, FuncInfo *finfo, int numFuncs,
 
 		becomeUser(fout, tinfo[i].usename);
 
+		if (drop_schema)
+		  {
+		    sprintf(q, "DROP TYPE %s;\n", fmtId(tinfo[i].typname, force_quotes));
+		    fputs(q, fout);
+		  }
+
 		sprintf(q,
 				"CREATE TYPE %s "
 				"( internallength = %s, externallength = %s, input = %s, "
@@ -2130,6 +2155,9 @@ dumpProcLangs(FILE *fout, FuncInfo *finfo, int numFuncs,
 		lanname = checkForQuote(PQgetvalue(res, i, i_lanname));
 		lancompiler = checkForQuote(PQgetvalue(res, i, i_lancompiler));
 
+		if (drop_schema)
+		  fprintf(fout, "DROP PROCEDURAL LANGUAGE '%s';\n", lanname);
+
 		fprintf(fout, "CREATE %sPROCEDURAL LANGUAGE '%s' "
 			"HANDLER %s LANCOMPILER '%s';\n",
 			(PQgetvalue(res, i, i_lanpltrusted)[0] == 't') ? "TRUSTED " : "",
@@ -2245,6 +2273,23 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
 		PQclear(res);
 	}
 
+	if (drop_schema)
+	  {
+	    sprintf(q, "DROP FUNCTION %s (", fmtId(finfo[i].proname, force_quotes));
+	    for (j = 0; j < finfo[i].nargs; j++)
+	      {
+		char	   *typname;
+		
+		typname = findTypeByOid(tinfo, numTypes, finfo[i].argtypes[j]);
+		sprintf(q, "%s%s%s",
+			q,
+			(j > 0) ? "," : "",
+			fmtId(typname, false));
+	      }
+	    sprintf (q, "%s);\n", q);
+	    fputs(q, fout);
+	  }
+
 	sprintf(q, "CREATE FUNCTION %s (", fmtId(finfo[i].proname, force_quotes));
 	for (j = 0; j < finfo[i].nargs; j++)
 	{
@@ -2355,6 +2400,14 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
 
 		becomeUser(fout, oprinfo[i].usename);
 
+		if (drop_schema)
+		  {
+		    sprintf(q, "DROP OPERATOR %s (%s, %s);\n", oprinfo[i].oprname, 
+			    fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprleft), false),
+			    fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprright), false));
+		    fputs(q, fout);
+		  }
+
 		sprintf(q,
 				"CREATE OPERATOR %s "
 				"(PROCEDURE = %s %s %s %s %s %s %s %s %s);\n ",
@@ -2450,6 +2503,13 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
 
 		becomeUser(fout, agginfo[i].usename);
 
+		if (drop_schema)
+		  {
+		    sprintf(q, "DROP AGGREGATE %s %s;\n", agginfo[i].aggname,
+			    fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggbasetype), false));
+		    fputs(q, fout);
+		  }
+
 		sprintf(q, "CREATE AGGREGATE %s ( %s %s%s %s%s %s );\n",
 				agginfo[i].aggname,
 				basetype,
@@ -2649,6 +2709,12 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
 
 			becomeUser(fout, tblinfo[i].usename);
 
+			if (drop_schema)
+			  {
+			    sprintf(q, "DROP TABLE %s;\n", fmtId(tblinfo[i].relname, force_quotes));
+			    fputs(q, fout);
+			  }
+
 			sprintf(q, "CREATE TABLE %s (\n\t", fmtId(tblinfo[i].relname, force_quotes));
 			actual_atts = 0;
 			for (j = 0; j < tblinfo[i].numatts; j++)
@@ -2865,6 +2931,13 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices,
 
 			strcpy(id1, fmtId(indinfo[i].indexrelname, force_quotes));
 			strcpy(id2, fmtId(indinfo[i].indrelname, force_quotes));
+
+			if (drop_schema)
+			  {
+			    sprintf(q, "DROP INDEX %s;\n", id1);
+			    fputs(q, fout);
+			  }
+
 			fprintf(fout, "CREATE %s INDEX %s on %s using %s (",
 			  (strcmp(indinfo[i].indisunique, "t") == 0) ? "UNIQUE" : "",
 					id1,
@@ -3125,6 +3198,12 @@ dumpSequence(FILE *fout, TableInfo tbinfo)
 
 	PQclear(res);
 
+	if (drop_schema)
+	  {
+	    sprintf(query, "DROP SEQUENCE %s;\n", fmtId(tbinfo.relname, force_quotes));
+	    fputs(query, fout);
+	  }
+
 	sprintf(query,
 			"CREATE SEQUENCE %s start %d increment %d maxvalue %d "
 			"minvalue %d  cache %d %s;\n",
diff --git a/src/man/pg_dump.1 b/src/man/pg_dump.1
index 0f65e3f764f..ec369b0b4bd 100644
--- a/src/man/pg_dump.1
+++ b/src/man/pg_dump.1
@@ -1,6 +1,6 @@
 .\" This is -*-nroff-*-
 .\" XXX standard disclaimer belongs here....
-.\" $Header: /cvsroot/pgsql/src/man/Attic/pg_dump.1,v 1.15 1998/10/07 02:49:10 momjian Exp $
+.\" $Header: /cvsroot/pgsql/src/man/Attic/pg_dump.1,v 1.16 1999/01/21 22:53:37 momjian Exp $
 .TH PG_DUMP UNIX 7/15/98 PostgreSQL PostgreSQL
 .SH NAME
 pg_dump - dumps out a Postgres database into a script file
@@ -10,6 +10,9 @@ pg_dump - dumps out a Postgres database into a script file
 .BR "-a"
 ]
 [\c
+.BR "-c"
+]
+[\c
 .BR "-d"
 ]
 [\c
@@ -74,6 +77,9 @@ pg_dump understands the following options:
 .BR "-a" ""
 Dump out only the data, no schema
 .TP
+.BR "-c" ""
+Clean(drop) schema prior to create
+.TP
 .BR "-d" ""
 Dump data as proper insert strings
 .TP
-- 
GitLab