mirror of
https://github.com/postgres/postgres.git
synced 2025-04-27 22:56:53 +03:00
Fix pg_dump's --if-exists for large objects
This was born broken in 9067310cc5dd590e36c2c3219dbf3961d7c9f8cb. Per trouble report from Joachim Wieland. Pavel Stěhule and Álvaro Herrera
This commit is contained in:
parent
6f1310024d
commit
5f4f66f671
@ -428,60 +428,75 @@ RestoreArchive(Archive *AHX)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char buffer[40];
|
|
||||||
char *mark;
|
|
||||||
char *dropStmt = pg_strdup(te->dropStmt);
|
|
||||||
char *dropStmtPtr = dropStmt;
|
|
||||||
PQExpBuffer ftStmt = createPQExpBuffer();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Need to inject IF EXISTS clause after ALTER TABLE
|
* Inject an appropriate spelling of "if exists". For
|
||||||
* part in ALTER TABLE .. DROP statement
|
* large objects, we have a separate routine that
|
||||||
|
* knows how to do it, without depending on
|
||||||
|
* te->dropStmt; use that. For other objects we need
|
||||||
|
* to parse the command.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
|
if (strncmp(te->desc, "BLOB", 4) == 0)
|
||||||
{
|
{
|
||||||
appendPQExpBuffer(ftStmt,
|
DropBlobIfExists(AH, te->catalogId.oid);
|
||||||
"ALTER TABLE IF EXISTS");
|
|
||||||
dropStmt = dropStmt + 11;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* ALTER TABLE..ALTER COLUMN..DROP DEFAULT does not
|
|
||||||
* support the IF EXISTS clause, and therefore we
|
|
||||||
* simply emit the original command for such objects.
|
|
||||||
* For other objects, we need to extract the first
|
|
||||||
* part of the DROP which includes the object type.
|
|
||||||
* Most of the time this matches te->desc, so search
|
|
||||||
* for that; however for the different kinds of
|
|
||||||
* CONSTRAINTs, we know to search for hardcoded "DROP
|
|
||||||
* CONSTRAINT" instead.
|
|
||||||
*/
|
|
||||||
if (strcmp(te->desc, "DEFAULT") == 0)
|
|
||||||
appendPQExpBuffer(ftStmt, "%s", dropStmt);
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (strcmp(te->desc, "CONSTRAINT") == 0 ||
|
char buffer[40];
|
||||||
strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
|
char *mark;
|
||||||
strcmp(te->desc, "FK CONSTRAINT") == 0)
|
char *dropStmt = pg_strdup(te->dropStmt);
|
||||||
strcpy(buffer, "DROP CONSTRAINT");
|
char *dropStmtPtr = dropStmt;
|
||||||
|
PQExpBuffer ftStmt = createPQExpBuffer();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Need to inject IF EXISTS clause after ALTER
|
||||||
|
* TABLE part in ALTER TABLE .. DROP statement
|
||||||
|
*/
|
||||||
|
if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
|
||||||
|
{
|
||||||
|
appendPQExpBuffer(ftStmt,
|
||||||
|
"ALTER TABLE IF EXISTS");
|
||||||
|
dropStmt = dropStmt + 11;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ALTER TABLE..ALTER COLUMN..DROP DEFAULT does
|
||||||
|
* not support the IF EXISTS clause, and therefore
|
||||||
|
* we simply emit the original command for such
|
||||||
|
* objects. For other objects, we need to extract
|
||||||
|
* the first part of the DROP which includes the
|
||||||
|
* object type. Most of the time this matches
|
||||||
|
* te->desc, so search for that; however for the
|
||||||
|
* different kinds of CONSTRAINTs, we know to
|
||||||
|
* search for hardcoded "DROP CONSTRAINT" instead.
|
||||||
|
*/
|
||||||
|
if (strcmp(te->desc, "DEFAULT") == 0)
|
||||||
|
appendPQExpBuffer(ftStmt, "%s", dropStmt);
|
||||||
else
|
else
|
||||||
snprintf(buffer, sizeof(buffer), "DROP %s",
|
{
|
||||||
te->desc);
|
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);
|
mark = strstr(dropStmt, buffer);
|
||||||
Assert(mark != NULL);
|
Assert(mark != NULL);
|
||||||
|
|
||||||
*mark = '\0';
|
*mark = '\0';
|
||||||
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
|
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
|
||||||
dropStmt, buffer,
|
dropStmt, buffer,
|
||||||
mark + strlen(buffer));
|
mark + strlen(buffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
ahprintf(AH, "%s", ftStmt->data);
|
||||||
|
|
||||||
|
destroyPQExpBuffer(ftStmt);
|
||||||
|
|
||||||
|
pg_free(dropStmtPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
ahprintf(AH, "%s", ftStmt->data);
|
|
||||||
|
|
||||||
destroyPQExpBuffer(ftStmt);
|
|
||||||
|
|
||||||
pg_free(dropStmtPtr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user