mirror of
https://github.com/postgres/postgres.git
synced 2025-07-18 17:42:25 +03:00
Improve ALTER DOMAIN / DROP CONSTRAINT with nonexistent constraint
ALTER DOMAIN / DROP CONSTRAINT on a nonexistent constraint name did not report any error. Now it reports an error. The IF EXISTS option was added to get the usual behavior of ignoring nonexistent objects to drop.
This commit is contained in:
@ -2264,7 +2264,7 @@ AlterDomainNotNull(List *names, bool notNull)
|
||||
*/
|
||||
void
|
||||
AlterDomainDropConstraint(List *names, const char *constrName,
|
||||
DropBehavior behavior)
|
||||
DropBehavior behavior, bool missing_ok)
|
||||
{
|
||||
TypeName *typename;
|
||||
Oid domainoid;
|
||||
@ -2274,6 +2274,7 @@ AlterDomainDropConstraint(List *names, const char *constrName,
|
||||
SysScanDesc conscan;
|
||||
ScanKeyData key[1];
|
||||
HeapTuple contup;
|
||||
bool found = false;
|
||||
|
||||
/* Make a TypeName so we can use standard type lookup machinery */
|
||||
typename = makeTypeNameFromNameList(names);
|
||||
@ -2317,6 +2318,7 @@ AlterDomainDropConstraint(List *names, const char *constrName,
|
||||
conobj.objectSubId = 0;
|
||||
|
||||
performDeletion(&conobj, behavior);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
/* Clean up after the scan */
|
||||
@ -2324,6 +2326,19 @@ AlterDomainDropConstraint(List *names, const char *constrName,
|
||||
heap_close(conrel, RowExclusiveLock);
|
||||
|
||||
heap_close(rel, NoLock);
|
||||
|
||||
if (!found)
|
||||
{
|
||||
if (!missing_ok)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
||||
errmsg("constraint \"%s\" of domain \"%s\" does not exist",
|
||||
constrName, TypeNameToString(typename))));
|
||||
else
|
||||
ereport(NOTICE,
|
||||
(errmsg("constraint \"%s\" of domain \"%s\" does not exist, skipping",
|
||||
constrName, TypeNameToString(typename))));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -2568,6 +2568,7 @@ _copyAlterDomainStmt(const AlterDomainStmt *from)
|
||||
COPY_STRING_FIELD(name);
|
||||
COPY_NODE_FIELD(def);
|
||||
COPY_SCALAR_FIELD(behavior);
|
||||
COPY_SCALAR_FIELD(missing_ok);
|
||||
|
||||
return newnode;
|
||||
}
|
||||
|
@ -1034,6 +1034,7 @@ _equalAlterDomainStmt(const AlterDomainStmt *a, const AlterDomainStmt *b)
|
||||
COMPARE_STRING_FIELD(name);
|
||||
COMPARE_NODE_FIELD(def);
|
||||
COMPARE_SCALAR_FIELD(behavior);
|
||||
COMPARE_SCALAR_FIELD(missing_ok);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -7616,6 +7616,18 @@ AlterDomainStmt:
|
||||
n->typeName = $3;
|
||||
n->name = $6;
|
||||
n->behavior = $7;
|
||||
n->missing_ok = false;
|
||||
$$ = (Node *)n;
|
||||
}
|
||||
/* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
|
||||
| ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
|
||||
{
|
||||
AlterDomainStmt *n = makeNode(AlterDomainStmt);
|
||||
n->subtype = 'X';
|
||||
n->typeName = $3;
|
||||
n->name = $8;
|
||||
n->behavior = $9;
|
||||
n->missing_ok = true;
|
||||
$$ = (Node *)n;
|
||||
}
|
||||
/* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */
|
||||
|
@ -768,7 +768,8 @@ standard_ProcessUtility(Node *parsetree,
|
||||
case 'X': /* DROP CONSTRAINT */
|
||||
AlterDomainDropConstraint(stmt->typeName,
|
||||
stmt->name,
|
||||
stmt->behavior);
|
||||
stmt->behavior,
|
||||
stmt->missing_ok);
|
||||
break;
|
||||
case 'V': /* VALIDATE CONSTRAINT */
|
||||
AlterDomainValidateConstraint(stmt->typeName,
|
||||
|
Reference in New Issue
Block a user