Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 104e7da

Browse files
committed
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.
1 parent 2abefd9 commit 104e7da

File tree

10 files changed

+45
-5
lines changed

10 files changed

+45
-5
lines changed

doc/src/sgml/ref/alter_domain.sgml

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
3030
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
3131
ADD <replaceable class="PARAMETER">domain_constraint</replaceable> [ NOT VALID ]
3232
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
33-
DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
33+
DROP CONSTRAINT [ IF EXISTS ] <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
3434
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
3535
VALIDATE CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable>
3636
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
@@ -92,10 +92,12 @@ ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
9292
</varlistentry>
9393

9494
<varlistentry>
95-
<term>DROP CONSTRAINT</term>
95+
<term>DROP CONSTRAINT [ IF EXISTS ]</term>
9696
<listitem>
9797
<para>
9898
This form drops constraints on a domain.
99+
If <literal>IF EXISTS</literal> is specified and the constraint
100+
does not exist, no error is thrown. In this case a notice is issued instead.
99101
</para>
100102
</listitem>
101103
</varlistentry>

src/backend/commands/typecmds.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -2264,7 +2264,7 @@ AlterDomainNotNull(List *names, bool notNull)
22642264
*/
22652265
void
22662266
AlterDomainDropConstraint(List *names, const char *constrName,
2267-
DropBehavior behavior)
2267+
DropBehavior behavior, bool missing_ok)
22682268
{
22692269
TypeName *typename;
22702270
Oid domainoid;
@@ -2274,6 +2274,7 @@ AlterDomainDropConstraint(List *names, const char *constrName,
22742274
SysScanDesc conscan;
22752275
ScanKeyData key[1];
22762276
HeapTuple contup;
2277+
bool found = false;
22772278

22782279
/* Make a TypeName so we can use standard type lookup machinery */
22792280
typename = makeTypeNameFromNameList(names);
@@ -2317,13 +2318,27 @@ AlterDomainDropConstraint(List *names, const char *constrName,
23172318
conobj.objectSubId = 0;
23182319

23192320
performDeletion(&conobj, behavior);
2321+
found = true;
23202322
}
23212323
}
23222324
/* Clean up after the scan */
23232325
systable_endscan(conscan);
23242326
heap_close(conrel, RowExclusiveLock);
23252327

23262328
heap_close(rel, NoLock);
2329+
2330+
if (!found)
2331+
{
2332+
if (!missing_ok)
2333+
ereport(ERROR,
2334+
(errcode(ERRCODE_UNDEFINED_OBJECT),
2335+
errmsg("constraint \"%s\" of domain \"%s\" does not exist",
2336+
constrName, TypeNameToString(typename))));
2337+
else
2338+
ereport(NOTICE,
2339+
(errmsg("constraint \"%s\" of domain \"%s\" does not exist, skipping",
2340+
constrName, TypeNameToString(typename))));
2341+
}
23272342
}
23282343

23292344
/*

src/backend/nodes/copyfuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,7 @@ _copyAlterDomainStmt(const AlterDomainStmt *from)
25682568
COPY_STRING_FIELD(name);
25692569
COPY_NODE_FIELD(def);
25702570
COPY_SCALAR_FIELD(behavior);
2571+
COPY_SCALAR_FIELD(missing_ok);
25712572

25722573
return newnode;
25732574
}

src/backend/nodes/equalfuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,7 @@ _equalAlterDomainStmt(const AlterDomainStmt *a, const AlterDomainStmt *b)
10341034
COMPARE_STRING_FIELD(name);
10351035
COMPARE_NODE_FIELD(def);
10361036
COMPARE_SCALAR_FIELD(behavior);
1037+
COMPARE_SCALAR_FIELD(missing_ok);
10371038

10381039
return true;
10391040
}

src/backend/parser/gram.y

+12
Original file line numberDiff line numberDiff line change
@@ -7616,6 +7616,18 @@ AlterDomainStmt:
76167616
n->typeName = $3;
76177617
n->name = $6;
76187618
n->behavior = $7;
7619+
n->missing_ok = false;
7620+
$$ = (Node *)n;
7621+
}
7622+
/* ALTER DOMAIN <domain> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
7623+
| ALTER DOMAIN_P any_name DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
7624+
{
7625+
AlterDomainStmt *n = makeNode(AlterDomainStmt);
7626+
n->subtype = 'X';
7627+
n->typeName = $3;
7628+
n->name = $8;
7629+
n->behavior = $9;
7630+
n->missing_ok = true;
76197631
$$ = (Node *)n;
76207632
}
76217633
/* ALTER DOMAIN <domain> VALIDATE CONSTRAINT <name> */

src/backend/tcop/utility.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,8 @@ standard_ProcessUtility(Node *parsetree,
768768
case 'X': /* DROP CONSTRAINT */
769769
AlterDomainDropConstraint(stmt->typeName,
770770
stmt->name,
771-
stmt->behavior);
771+
stmt->behavior,
772+
stmt->missing_ok);
772773
break;
773774
case 'V': /* VALIDATE CONSTRAINT */
774775
AlterDomainValidateConstraint(stmt->typeName,

src/include/commands/typecmds.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern void AlterDomainNotNull(List *names, bool notNull);
3333
extern void AlterDomainAddConstraint(List *names, Node *constr);
3434
extern void AlterDomainValidateConstraint(List *names, char *constrName);
3535
extern void AlterDomainDropConstraint(List *names, const char *constrName,
36-
DropBehavior behavior);
36+
DropBehavior behavior, bool missing_ok);
3737

3838
extern List *GetDomainConstraints(Oid typeOid);
3939

src/include/nodes/parsenodes.h

+1
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,7 @@ typedef struct AlterDomainStmt
12641264
char *name; /* column or constraint name to act on */
12651265
Node *def; /* definition of default or constraint */
12661266
DropBehavior behavior; /* RESTRICT or CASCADE for DROP cases */
1267+
bool missing_ok; /* skip error if missing? */
12671268
} AlterDomainStmt;
12681269

12691270

src/test/regress/expected/domain.out

+4
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ alter domain con drop constraint t;
358358
insert into domcontest values (-5); --fails
359359
ERROR: value for domain con violates check constraint "con_check"
360360
insert into domcontest values (42);
361+
alter domain con drop constraint nonexistent;
362+
ERROR: constraint "nonexistent" of domain "con" does not exist
363+
alter domain con drop constraint if exists nonexistent;
364+
NOTICE: constraint "nonexistent" of domain "con" does not exist, skipping
361365
-- Test ALTER DOMAIN .. CONSTRAINT .. NOT VALID
362366
create domain things AS INT;
363367
CREATE TABLE thethings (stuff things);

src/test/regress/sql/domain.sql

+3
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ alter domain con drop constraint t;
259259
insert into domcontest values (-5); --fails
260260
insert into domcontest values (42);
261261

262+
alter domain con drop constraint nonexistent;
263+
alter domain con drop constraint if exists nonexistent;
264+
262265
-- Test ALTER DOMAIN .. CONSTRAINT .. NOT VALID
263266
create domain things AS INT;
264267
CREATE TABLE thethings (stuff things);

0 commit comments

Comments
 (0)