OSDN Git Service

Please find attached a small patch so that "pg_restore" ignores some sql
authorBruce Momjian <bruce@momjian.us>
Thu, 22 Apr 2004 02:39:10 +0000 (02:39 +0000)
committerBruce Momjian <bruce@momjian.us>
Thu, 22 Apr 2004 02:39:10 +0000 (02:39 +0000)
errors. This is the second submission, which integrates Tom comments about
localisation and exit code. I also added some comments about one sql
command which is not ignored.

Fabien COELHO

src/bin/pg_dump/pg_backup.h
src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_backup_archiver.h
src/bin/pg_dump/pg_backup_db.c
src/bin/pg_dump/pg_restore.c

index 6109c9b..3176462 100644 (file)
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.29 2004/03/24 03:06:08 momjian Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.30 2004/04/22 02:39:09 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -57,6 +57,11 @@ typedef struct _Archive
        int                     remoteVersion;
        int                     minRemoteVersion;
        int                     maxRemoteVersion;
+
+       /* error handling */
+       bool            die_on_errors;  /* whether to die on sql errors... */
+       int                     n_errors;               /* number of errors (if no die) */
+
        /* The rest is private */
 } Archive;
 
index 57b808b..d4f01f4 100644 (file)
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.85 2004/03/24 03:06:08 momjian Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.86 2004/04/22 02:39:10 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1197,6 +1197,24 @@ die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...)
        va_end(ap);
 }
 
+/* on some error, we may decide to go on... */
+void
+warn_or_die_horribly(ArchiveHandle *AH, 
+                                        const char *modulename, const char *fmt, ...)
+{
+       va_list ap;
+       va_start(ap, fmt);
+       if (AH->public.die_on_errors)
+       {
+               _die_horribly(AH, modulename, fmt, ap);
+       }
+       else
+       {
+               _write_msg(modulename, fmt, ap);
+               AH->public.n_errors++;
+       }
+       va_end(ap);
+}
 
 static void
 _moveAfter(ArchiveHandle *AH, TocEntry *pos, TocEntry *te)
@@ -1651,6 +1669,10 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
                        die_horribly(AH, modulename, "unrecognized file format \"%d\"\n", fmt);
        }
 
+       /* sql error handling */
+       AH->public.die_on_errors = true;
+       AH->public.n_errors = 0;
+
        return AH;
 }
 
@@ -2011,6 +2033,7 @@ _doSetSessionAuth(ArchiveHandle *AH, const char *user)
                res = PQexec(AH->connection, cmd->data);
 
                if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
+                       /* NOT warn_or_die_horribly... use -O instead to skip this. */
                        die_horribly(AH, modulename, "could not set session user to \"%s\": %s",
                                                 user, PQerrorMessage(AH->connection));
 
@@ -2042,8 +2065,9 @@ _doSetWithOids(ArchiveHandle *AH, const bool withOids)
                res = PQexec(AH->connection, cmd->data);
 
                if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
-                       die_horribly(AH, modulename, "could not set default_with_oids: %s",
-                                                PQerrorMessage(AH->connection));
+                       warn_or_die_horribly(AH, modulename, 
+                                                                "could not set default_with_oids: %s",
+                                                                PQerrorMessage(AH->connection));
 
                PQclear(res);
        }
@@ -2181,8 +2205,9 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
                res = PQexec(AH->connection, qry->data);
 
                if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
-                       die_horribly(AH, modulename, "could not set search_path to \"%s\": %s",
-                                                schemaName, PQerrorMessage(AH->connection));
+                       warn_or_die_horribly(AH, modulename, 
+                                                                "could not set search_path to \"%s\": %s",
+                                                                schemaName, PQerrorMessage(AH->connection));
 
                PQclear(res);
        }
index 2de300d..7466fea 100644 (file)
@@ -17,7 +17,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.57 2004/03/24 03:06:08 momjian Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.58 2004/04/22 02:39:10 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -281,6 +281,7 @@ typedef struct _tocEntry
 extern const char *progname;
 
 extern void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(printf, 3, 4)));
+extern void warn_or_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(printf, 3, 4)));
 extern void write_msg(const char *modulename, const char *fmt,...) __attribute__((format(printf, 2, 3)));
 
 extern void WriteTOC(ArchiveHandle *AH);
index b2677e8..df2f8af 100644 (file)
@@ -5,7 +5,7 @@
  *     Implements the basic DB functions used by the archiver.
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.52 2004/03/03 21:28:54 tgl Exp $
+ *       $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.53 2004/04/22 02:39:10 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -316,8 +316,8 @@ _executeSqlCommand(ArchiveHandle *AH, PGconn *conn, PQExpBuffer qry, char *desc)
                        AH->pgCopyIn = 1;
                }
                else
-                       die_horribly(AH, modulename, "%s: %s",
-                                                desc, PQerrorMessage(AH->connection));
+                       warn_or_die_horribly(AH, modulename, "%s: %s",
+                                                                desc, PQerrorMessage(AH->connection));
        }
 
        PQclear(res);
index 8e05110..d8dfcc4 100644 (file)
@@ -34,7 +34,7 @@
  *
  *
  * IDENTIFICATION
- *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.55 2003/12/06 03:00:16 tgl Exp $
+ *             $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.56 2004/04/22 02:39:10 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -77,6 +77,7 @@ main(int argc, char **argv)
 {
        RestoreOptions *opts;
        int                     c;
+       int                     exit_code;
        Archive    *AH;
        char       *inputFileSpec;
        extern int      optind;
@@ -323,6 +324,11 @@ main(int argc, char **argv)
        /* Let the archiver know how noisy to be */
        AH->verbose = opts->verbose;
 
+       /* restore keeps submitting sql commands as "pg_restore ... | psql ... "
+        * this behavior choice could be turned into an option.
+        */
+       AH->die_on_errors = false;
+
        if (opts->tocFile)
                SortTocFromFile(AH, opts);
 
@@ -331,9 +337,17 @@ main(int argc, char **argv)
        else
                RestoreArchive(AH, opts);
 
+       /* done, print a summary of ignored errors */
+       if (AH->n_errors)
+               fprintf(stderr, _("WARNING, errors ignored on restore: %d\n"), 
+                               AH->n_errors);
+
+       /* AH may be freed in CloseArchive? */
+       exit_code = AH->n_errors? 1: 0;
+
        CloseArchive(AH);
 
-       return 0;
+       return exit_code;
 }
 
 static void