Dear patchers,

please find attached a small patch so that "pg_restore" ignores some sql
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.

I tested it.

Have a nice day,

-- 
Fabien Coelho - [EMAIL PROTECTED]
*** ./src/bin/pg_dump/pg_backup.h.orig  Wed Mar 24 17:19:43 2004
--- ./src/bin/pg_dump/pg_backup.h       Sat Apr 10 13:04:21 2004
***************
*** 57,62 ****
--- 57,67 ----
        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;
  
*** ./src/bin/pg_dump/pg_backup_archiver.c.orig Wed Mar 24 17:19:43 2004
--- ./src/bin/pg_dump/pg_backup_archiver.c      Sat Apr 10 13:09:55 2004
***************
*** 1197,1202 ****
--- 1197,1220 ----
        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,1656 ****
--- 1669,1678 ----
                        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,2016 ****
--- 2033,2039 ----
                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,2049 ****
                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));
  
                PQclear(res);
        }
--- 2065,2073 ----
                res = PQexec(AH->connection, cmd->data);
  
                if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
!                       warn_or_die_horribly(AH, modulename, 
!                                                                "could not set 
default_with_oids: %s",
!                                                                
PQerrorMessage(AH->connection));
  
                PQclear(res);
        }
***************
*** 2181,2188 ****
                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));
  
                PQclear(res);
        }
--- 2205,2213 ----
                res = PQexec(AH->connection, qry->data);
  
                if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
!                       warn_or_die_horribly(AH, modulename, 
!                                                                "could not set 
search_path to \"%s\": %s",
!                                                                schemaName, 
PQerrorMessage(AH->connection));
  
                PQclear(res);
        }
*** ./src/bin/pg_dump/pg_backup_archiver.h.orig Wed Mar 24 17:19:43 2004
--- ./src/bin/pg_dump/pg_backup_archiver.h      Sat Apr 10 13:04:21 2004
***************
*** 281,286 ****
--- 281,287 ----
  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);
*** ./src/bin/pg_dump/pg_backup_db.c.orig       Wed Mar  3 22:28:54 2004
--- ./src/bin/pg_dump/pg_backup_db.c    Sat Apr 10 13:04:21 2004
***************
*** 316,323 ****
                        AH->pgCopyIn = 1;
                }
                else
!                       die_horribly(AH, modulename, "%s: %s",
!                                                desc, PQerrorMessage(AH->connection));
        }
  
        PQclear(res);
--- 316,323 ----
                        AH->pgCopyIn = 1;
                }
                else
!                       warn_or_die_horribly(AH, modulename, "%s: %s",
!                                                                desc, 
PQerrorMessage(AH->connection));
        }
  
        PQclear(res);
*** ./src/bin/pg_dump/pg_restore.c.orig Sat Dec  6 04:00:16 2003
--- ./src/bin/pg_dump/pg_restore.c      Sat Apr 10 13:44:33 2004
***************
*** 77,82 ****
--- 77,83 ----
  {
        RestoreOptions *opts;
        int                     c;
+       int                     exit_code;
        Archive    *AH;
        char       *inputFileSpec;
        extern int      optind;
***************
*** 323,328 ****
--- 324,334 ----
        /* 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,339 ****
        else
                RestoreArchive(AH, opts);
  
        CloseArchive(AH);
  
!       return 0;
  }
  
  static void
--- 337,353 ----
        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 exit_code;
  }
  
  static void
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

               http://archives.postgresql.org

Reply via email to