Commit ec7c4c1b authored by Bruce Momjian's avatar Bruce Momjian

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.

Fabien COELHO
parent be6bbcef
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * 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 ...@@ -57,6 +57,11 @@ typedef struct _Archive
int remoteVersion; int remoteVersion;
int minRemoteVersion; int minRemoteVersion;
int maxRemoteVersion; 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 */ /* The rest is private */
} Archive; } Archive;
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * 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,...) ...@@ -1197,6 +1197,24 @@ die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...)
va_end(ap); 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 static void
_moveAfter(ArchiveHandle *AH, TocEntry *pos, TocEntry *te) _moveAfter(ArchiveHandle *AH, TocEntry *pos, TocEntry *te)
...@@ -1651,6 +1669,10 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt, ...@@ -1651,6 +1669,10 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
die_horribly(AH, modulename, "unrecognized file format \"%d\"\n", 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; return AH;
} }
...@@ -2011,6 +2033,7 @@ _doSetSessionAuth(ArchiveHandle *AH, const char *user) ...@@ -2011,6 +2033,7 @@ _doSetSessionAuth(ArchiveHandle *AH, const char *user)
res = PQexec(AH->connection, cmd->data); res = PQexec(AH->connection, cmd->data);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) 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", die_horribly(AH, modulename, "could not set session user to \"%s\": %s",
user, PQerrorMessage(AH->connection)); user, PQerrorMessage(AH->connection));
...@@ -2042,8 +2065,9 @@ _doSetWithOids(ArchiveHandle *AH, const bool withOids) ...@@ -2042,8 +2065,9 @@ _doSetWithOids(ArchiveHandle *AH, const bool withOids)
res = PQexec(AH->connection, cmd->data); res = PQexec(AH->connection, cmd->data);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
die_horribly(AH, modulename, "could not set default_with_oids: %s", warn_or_die_horribly(AH, modulename,
PQerrorMessage(AH->connection)); "could not set default_with_oids: %s",
PQerrorMessage(AH->connection));
PQclear(res); PQclear(res);
} }
...@@ -2181,8 +2205,9 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName) ...@@ -2181,8 +2205,9 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
res = PQexec(AH->connection, qry->data); res = PQexec(AH->connection, qry->data);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
die_horribly(AH, modulename, "could not set search_path to \"%s\": %s", warn_or_die_horribly(AH, modulename,
schemaName, PQerrorMessage(AH->connection)); "could not set search_path to \"%s\": %s",
schemaName, PQerrorMessage(AH->connection));
PQclear(res); PQclear(res);
} }
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
* *
* *
* IDENTIFICATION * 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 ...@@ -281,6 +281,7 @@ typedef struct _tocEntry
extern const char *progname; extern const char *progname;
extern void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(printf, 3, 4))); 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 write_msg(const char *modulename, const char *fmt,...) __attribute__((format(printf, 2, 3)));
extern void WriteTOC(ArchiveHandle *AH); extern void WriteTOC(ArchiveHandle *AH);
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* Implements the basic DB functions used by the archiver. * Implements the basic DB functions used by the archiver.
* *
* IDENTIFICATION * 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) ...@@ -316,8 +316,8 @@ _executeSqlCommand(ArchiveHandle *AH, PGconn *conn, PQExpBuffer qry, char *desc)
AH->pgCopyIn = 1; AH->pgCopyIn = 1;
} }
else else
die_horribly(AH, modulename, "%s: %s", warn_or_die_horribly(AH, modulename, "%s: %s",
desc, PQerrorMessage(AH->connection)); desc, PQerrorMessage(AH->connection));
} }
PQclear(res); PQclear(res);
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -77,6 +77,7 @@ main(int argc, char **argv)
{ {
RestoreOptions *opts; RestoreOptions *opts;
int c; int c;
int exit_code;
Archive *AH; Archive *AH;
char *inputFileSpec; char *inputFileSpec;
extern int optind; extern int optind;
...@@ -323,6 +324,11 @@ main(int argc, char **argv) ...@@ -323,6 +324,11 @@ main(int argc, char **argv)
/* Let the archiver know how noisy to be */ /* Let the archiver know how noisy to be */
AH->verbose = opts->verbose; 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) if (opts->tocFile)
SortTocFromFile(AH, opts); SortTocFromFile(AH, opts);
...@@ -331,9 +337,17 @@ main(int argc, char **argv) ...@@ -331,9 +337,17 @@ main(int argc, char **argv)
else else
RestoreArchive(AH, opts); 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); CloseArchive(AH);
return 0; return exit_code;
} }
static void static void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment