Hello

patch with updated comment

regards

Pavel



2014-01-30 Jeevan Chalke <jeevan.cha...@enterprisedb.com>:

> Hi Pavel,
>
>  it should be fixed by
>>> http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=b152c6cd0de1827ba58756e24e18110cf902182acommit
>>>
>>
> Ok. Good.
> Sorry I didn't update my sources. Done now. Thanks
>
>
>>
>>>
>>>>
>>>> Also, I didn't quite understand these lines of comments:
>>>>
>>>>                         /*
>>>>                          * Descriptor string (te-desc) should not be
>>>> same as object
>>>>                          * specifier for DROP STATEMENT. The DROP
>>>> DEFAULT has not
>>>>                          * IF EXISTS clause - has not sense.
>>>>                          */
>>>>
>>>> Will you please rephrase ?
>>>>
>>>
>>> I can try it - .
>>>
>>> A content of te->desc is usually substring of DROP STATEMENT with one
>>> related exception - CONSTRAINT.
>>> Independent to previous sentence - ALTER TABLE ALTER COLUMN DROP DEFAULT
>>> doesn't support IF EXISTS - and therefore it should not be injected.
>>>
>>
>> is it ok?
>>
>
> Fine with me.
>
> Thanks
>
> --
> Jeevan B Chalke
> Principal Software Engineer, Product Development
> EnterpriseDB Corporation
> The Enterprise PostgreSQL Company
>
>
commit e72e3ae9003d2c8eea0e687122a2f31d21674b81
Author: Pavel Stehule <pavel.steh...@gooddata.com>
Date:   Thu Jan 30 11:28:40 2014 +0100

    fix comment

diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml
new file mode 100644
index 8d45f24..c39767b
*** a/doc/src/sgml/ref/pg_dump.sgml
--- b/doc/src/sgml/ref/pg_dump.sgml
*************** PostgreSQL documentation
*** 650,655 ****
--- 650,665 ----
       </varlistentry>
  
       <varlistentry>
+       <term><option>--if-exists</option></term>
+       <listitem>
+        <para>
+         It uses conditional commands (with <literal>IF EXISTS</literal>
+         clause) for cleaning database schema.
+        </para>
+       </listitem>
+      </varlistentry>
+ 
+      <varlistentry>
        <term><option>--disable-dollar-quoting</></term>
        <listitem>
         <para>
diff --git a/doc/src/sgml/ref/pg_dumpall.sgml b/doc/src/sgml/ref/pg_dumpall.sgml
new file mode 100644
index 5c6a101..ba6583d
*** a/doc/src/sgml/ref/pg_dumpall.sgml
--- b/doc/src/sgml/ref/pg_dumpall.sgml
*************** PostgreSQL documentation
*** 301,306 ****
--- 301,316 ----
       </varlistentry>
  
       <varlistentry>
+       <term><option>--if-exists</option></term>
+       <listitem>
+        <para>
+         It uses conditional commands (with <literal>IF EXISTS</literal>
+         clause) for cleaning database schema.
+        </para>
+       </listitem>
+      </varlistentry>
+ 
+      <varlistentry>
        <term><option>--inserts</option></term>
        <listitem>
         <para>
diff --git a/doc/src/sgml/ref/pg_restore.sgml b/doc/src/sgml/ref/pg_restore.sgml
new file mode 100644
index 717da42..55326d5
*** a/doc/src/sgml/ref/pg_restore.sgml
--- b/doc/src/sgml/ref/pg_restore.sgml
***************
*** 490,495 ****
--- 490,505 ----
       </varlistentry>
  
       <varlistentry>
+       <term><option>--if-exists</option></term>
+       <listitem>
+        <para>
+         It uses conditional commands (with <literal>IF EXISTS</literal>
+         clause) for cleaning database schema.
+        </para>
+       </listitem>
+      </varlistentry>
+ 
+      <varlistentry>
        <term><option>--no-data-for-failed-tables</option></term>
        <listitem>
         <para>
diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h
new file mode 100644
index 6927968..83f7216
*** a/src/bin/pg_dump/pg_backup.h
--- b/src/bin/pg_dump/pg_backup.h
*************** typedef struct _restoreOptions
*** 113,118 ****
--- 113,119 ----
  	char	   *superuser;		/* Username to use as superuser */
  	char	   *use_role;		/* Issue SET ROLE to this */
  	int			dropSchema;
+ 	int			if_exists;
  	const char *filename;
  	int			dataOnly;
  	int			schemaOnly;
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
new file mode 100644
index 7fc0288..a7061d3
*** a/src/bin/pg_dump/pg_backup_archiver.c
--- b/src/bin/pg_dump/pg_backup_archiver.c
*************** RestoreArchive(Archive *AHX)
*** 413,420 ****
  				/* Select owner and schema as necessary */
  				_becomeOwner(AH, te);
  				_selectOutputSchema(AH, te->namespace);
! 				/* Drop it */
! 				ahprintf(AH, "%s", te->dropStmt);
  			}
  		}
  
--- 413,496 ----
  				/* Select owner and schema as necessary */
  				_becomeOwner(AH, te);
  				_selectOutputSchema(AH, te->namespace);
! 
! 				if (*te->dropStmt != '\0')
! 				{
! 					/* Inject IF EXISTS clause to DROP part when it is required. */
! 					if (ropt->if_exists)
! 					{
! 						char buffer[40];
! 						char *mark;
! 						char *dropStmt = te->dropStmt;
! 						PQExpBuffer ftStmt = createPQExpBuffer();
! 
! 						/* ALTER TABLE should be enahnced to ALTER TABLE IF EXISTS */
! 						if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
! 						{
! 							/*
! 							 * Prepare fault tolerant statement, but
! 							 * ensure unique IF EXISTS option.
! 							 */
! 							if (strncmp(dropStmt + 11, " IF EXISTS", 10) != 0)
! 							{
! 								appendPQExpBuffer(ftStmt, "ALTER TABLE IF EXISTS");
! 								dropStmt = dropStmt + 11;
! 							}
! 						}
! 
! 						/*
! 						 * A content of te->desc is usually substring of DROP STATEMENT
! 						 * with one related exception - CONSTRAINTs.
! 						 *
! 						 * Independent to previous sentence - ALTER TABLE ALTER COLUMN
! 						 * DROP DEFAULT doesn't support IF EXISTS - and therefore it
! 						 * should not be injected.
! 						 */
! 						if (strcmp(te->desc, "DEFAULT") != 0)
! 						{
! 							if (strcmp(te->desc, "CONSTRAINT") == 0 ||
! 									 strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
! 									 strcmp(te->desc, "FK CONSTRAINT") == 0)
! 								strcpy(buffer, "DROP CONSTRAINT");
! 							else
! 								snprintf(buffer, sizeof(buffer), "DROP %s",
! 													 te->desc);
! 
! 							mark = strstr(dropStmt, buffer);
! 						}
! 						else
! 							mark = NULL;
! 
! 						if (mark != NULL)
! 						{
! 							size_t l = strlen(buffer);
! 
! 							/*
! 							 * Insert IF EXISTS clause when it is not
! 							 * used yet.
! 							 */
! 							if (strncmp(mark + l, " IF EXISTS", 10) != 0)
! 							{
! 								*mark = '\0';
! 
! 								appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
! 													    dropStmt,
! 													    buffer,
! 													    mark + l);
! 							}
! 							else
! 								appendPQExpBuffer(ftStmt, "%s", dropStmt);
! 						}
! 						else
! 							appendPQExpBuffer(ftStmt, "%s", dropStmt);
! 
! 						ahprintf(AH, "%s", ftStmt->data);
! 
! 						destroyPQExpBuffer(ftStmt);
! 					}
! 					else
! 						ahprintf(AH, "%s", te->dropStmt);
! 				}
  			}
  		}
  
*************** _getObjectDescription(PQExpBuffer buf, T
*** 2942,2950 ****
  		strcmp(type, "OPERATOR CLASS") == 0 ||
  		strcmp(type, "OPERATOR FAMILY") == 0)
  	{
! 		/* Chop "DROP " off the front and make a modifiable copy */
! 		char	   *first = pg_strdup(te->dropStmt + 5);
! 		char	   *last;
  
  		/* point to last character in string */
  		last = first + strlen(first) - 1;
--- 3018,3056 ----
  		strcmp(type, "OPERATOR CLASS") == 0 ||
  		strcmp(type, "OPERATOR FAMILY") == 0)
  	{
! 		char	    *first;
! 		char	    *last;
! 
! 		/*
! 		 * Object description is based on dropStmt statement which may have
! 		 * IF EXISTS clause.  Thus we need to update an offset such that it
! 		 * won't be included in the object description.
! 		 */
! 		if (strstr(te->dropStmt, "IF EXISTS") != NULL)
! 		{
! 			char buffer[40];
! 			size_t   l;
! 
! 			/* Be sure, so IF EXISTS is used as DROP stmt option. */
! 			snprintf(buffer, sizeof(buffer), "DROP %s IF EXISTS", type);
! 
! 			l = strlen(buffer);
! 
! 			if (strncmp(te->dropStmt, buffer, l) == 0)
! 			{
! 				/* append command type to target type */
! 				appendPQExpBufferStr(buf, type);
! 
! 				/* skip first n chars, and create a modifiable copy */
! 				first = pg_strdup(te->dropStmt + l);
! 			}
! 			else
! 				/* DROP IF EXISTS pattern is not applicable on dropStmt */
! 				first = pg_strdup(te->dropStmt + 5);
! 		}
! 		else
! 			/* IF EXISTS clause was not used, simple solution */
! 			first = pg_strdup(te->dropStmt + 5);
  
  		/* point to last character in string */
  		last = first + strlen(first) - 1;
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
new file mode 100644
index ebbb5b7..2f4f3a1
*** a/src/bin/pg_dump/pg_dump.c
--- b/src/bin/pg_dump/pg_dump.c
*************** static int	binary_upgrade = 0;
*** 136,141 ****
--- 136,142 ----
  static int	disable_dollar_quoting = 0;
  static int	dump_inserts = 0;
  static int	column_inserts = 0;
+ static int	if_exists = 0;
  static int	no_security_labels = 0;
  static int	no_synchronized_snapshots = 0;
  static int	no_unlogged_table_data = 0;
*************** main(int argc, char **argv)
*** 349,354 ****
--- 350,356 ----
  		{"disable-dollar-quoting", no_argument, &disable_dollar_quoting, 1},
  		{"disable-triggers", no_argument, &disable_triggers, 1},
  		{"exclude-table-data", required_argument, NULL, 4},
+ 		{"if-exists", no_argument, &if_exists, 1},
  		{"inserts", no_argument, &dump_inserts, 1},
  		{"lock-wait-timeout", required_argument, NULL, 2},
  		{"no-tablespaces", no_argument, &outputNoTablespaces, 1},
*************** main(int argc, char **argv)
*** 577,582 ****
--- 579,587 ----
  		exit_nicely(1);
  	}
  
+ 	if (if_exists && !outputClean)
+ 		exit_horribly(NULL, "option --if-exists requires -c/--clean option\n");
+ 
  	/* Identify archive format to emit */
  	archiveFormat = parseArchiveFormat(format, &archiveMode);
  
*************** main(int argc, char **argv)
*** 809,814 ****
--- 814,820 ----
  	ropt->dropSchema = outputClean;
  	ropt->dataOnly = dataOnly;
  	ropt->schemaOnly = schemaOnly;
+ 	ropt->if_exists = if_exists;
  	ropt->dumpSections = dumpSections;
  	ropt->aclsSkip = aclsSkip;
  	ropt->superuser = outputSuperuser;
*************** help(const char *progname)
*** 890,895 ****
--- 896,902 ----
  	printf(_("  --disable-dollar-quoting     disable dollar quoting, use SQL standard quoting\n"));
  	printf(_("  --disable-triggers           disable triggers during data-only restore\n"));
  	printf(_("  --exclude-table-data=TABLE   do NOT dump data for the named table(s)\n"));
+ 	printf(_("  --if-exists                  use IF EXISTS when dropping objects\n"));
  	printf(_("  --inserts                    dump data as INSERT commands, rather than COPY\n"));
  	printf(_("  --no-security-labels         do not dump security label assignments\n"));
  	printf(_("  --no-synchronized-snapshots  do not use synchronized snapshots in parallel jobs\n"));
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
new file mode 100644
index 193c1a0..335fdde
*** a/src/bin/pg_dump/pg_dumpall.c
--- b/src/bin/pg_dump/pg_dumpall.c
*************** static int	binary_upgrade = 0;
*** 73,78 ****
--- 73,79 ----
  static int	column_inserts = 0;
  static int	disable_dollar_quoting = 0;
  static int	disable_triggers = 0;
+ static int	if_exists = 0;
  static int	inserts = 0;
  static int	no_tablespaces = 0;
  static int	use_setsessauth = 0;
*************** main(int argc, char *argv[])
*** 119,124 ****
--- 120,126 ----
  		{"column-inserts", no_argument, &column_inserts, 1},
  		{"disable-dollar-quoting", no_argument, &disable_dollar_quoting, 1},
  		{"disable-triggers", no_argument, &disable_triggers, 1},
+ 		{"if-exists", no_argument, &if_exists, 1},
  		{"inserts", no_argument, &inserts, 1},
  		{"lock-wait-timeout", required_argument, NULL, 2},
  		{"no-tablespaces", no_argument, &no_tablespaces, 1},
*************** main(int argc, char *argv[])
*** 352,357 ****
--- 354,361 ----
  		appendPQExpBufferStr(pgdumpopts, " --disable-dollar-quoting");
  	if (disable_triggers)
  		appendPQExpBufferStr(pgdumpopts, " --disable-triggers");
+ 	if (if_exists)
+ 		appendPQExpBufferStr(pgdumpopts, " --if-exists");
  	if (inserts)
  		appendPQExpBufferStr(pgdumpopts, " --inserts");
  	if (no_tablespaces)
*************** help(void)
*** 564,569 ****
--- 568,574 ----
  	printf(_("  --column-inserts             dump data as INSERT commands with column names\n"));
  	printf(_("  --disable-dollar-quoting     disable dollar quoting, use SQL standard quoting\n"));
  	printf(_("  --disable-triggers           disable triggers during data-only restore\n"));
+ 	printf(_("  --if-exists                  use IF EXISTS when dropping objects\n"));
  	printf(_("  --inserts                    dump data as INSERT commands, rather than COPY\n"));
  	printf(_("  --no-security-labels         do not dump security label assignments\n"));
  	printf(_("  --no-tablespaces             do not dump tablespace assignments\n"));
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
new file mode 100644
index 2648003..74f9c2a
*** a/src/bin/pg_dump/pg_restore.c
--- b/src/bin/pg_dump/pg_restore.c
*************** main(int argc, char **argv)
*** 76,81 ****
--- 76,82 ----
  	Archive    *AH;
  	char	   *inputFileSpec;
  	static int	disable_triggers = 0;
+ 	static int	if_exists = 0;
  	static int	no_data_for_failed_tables = 0;
  	static int	outputNoTablespaces = 0;
  	static int	use_setsessauth = 0;
*************** main(int argc, char **argv)
*** 116,121 ****
--- 117,123 ----
  		 * the following options don't have an equivalent short option letter
  		 */
  		{"disable-triggers", no_argument, &disable_triggers, 1},
+ 		{"if-exists", no_argument, &if_exists, 1},
  		{"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
  		{"no-tablespaces", no_argument, &outputNoTablespaces, 1},
  		{"role", required_argument, NULL, 2},
*************** main(int argc, char **argv)
*** 342,347 ****
--- 344,357 ----
  	opts->use_setsessauth = use_setsessauth;
  	opts->no_security_labels = no_security_labels;
  
+ 	if (if_exists && !opts->dropSchema)
+ 	{
+ 		fprintf(stderr, _("%s: option --if-exists requires -c/--clean option\n"),
+ 				progname);
+ 		exit_nicely(1);
+ 	}
+ 	opts->if_exists = if_exists;
+ 
  	if (opts->formatName)
  	{
  		switch (opts->formatName[0])
*************** usage(const char *progname)
*** 456,461 ****
--- 466,472 ----
  	printf(_("  -x, --no-privileges          skip restoration of access privileges (grant/revoke)\n"));
  	printf(_("  -1, --single-transaction     restore as a single transaction\n"));
  	printf(_("  --disable-triggers           disable triggers during data-only restore\n"));
+ 	printf(_("  --if-exists                  use IF EXISTS when dropping objects\n"));
  	printf(_("  --no-data-for-failed-tables  do not restore data of tables that could not be\n"
  			 "                               created\n"));
  	printf(_("  --no-security-labels         do not restore security labels\n"));
-- 
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