Add support for ALTER TABLE IF EXISTS ... RENAME CONSTRAINT
authorBruce Momjian <bruce@momjian.us>
Tue, 24 Mar 2015 23:52:47 +0000 (19:52 -0400)
committerBruce Momjian <bruce@momjian.us>
Tue, 24 Mar 2015 23:52:47 +0000 (19:52 -0400)
Also add regression test.  Previously this was documented to work, but
didn't.

src/backend/commands/tablecmds.c
src/backend/parser/gram.y
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index a9f79431e5c1f70ca2d8c55766e8d2e961ec0965..32e19c5e4fc02010009fc99a24e168d4f6f65737 100644 (file)
@@ -2497,9 +2497,16 @@ RenameConstraint(RenameStmt *stmt)
    {
        /* lock level taken here should match rename_constraint_internal */
        relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
-                                        false, false,
+                                        stmt->missing_ok, false,
                                         RangeVarCallbackForRenameAttribute,
                                         NULL);
+       if (!OidIsValid(relid))
+       {
+           ereport(NOTICE,
+                   (errmsg("relation \"%s\" does not exist, skipping",
+                           stmt->relation->relname)));
+           return InvalidObjectAddress;
+       }
    }
 
    return
index 873ca79492dc7a61d9769ce0cc608570c59b367c..2c4a5ccece8619e904f01bb96728ba54e32ffe3d 100644 (file)
@@ -7706,6 +7706,17 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                    n->relation = $3;
                    n->subname = $6;
                    n->newname = $8;
+                   n->missing_ok = false;
+                   $$ = (Node *)n;
+               }
+           | ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
+               {
+                   RenameStmt *n = makeNode(RenameStmt);
+                   n->renameType = OBJECT_TABCONSTRAINT;
+                   n->relation = $5;
+                   n->subname = $8;
+                   n->newname = $10;
+                   n->missing_ok = true;
                    $$ = (Node *)n;
                }
            | ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
index 51db1b679279544b6aadfa995fad2c9b771f6d8f..6a31df7a296e3fda6a5e3395dcec0b94e425a2db 100644 (file)
@@ -289,6 +289,8 @@ Inherits: constraint_rename_test
 
 DROP TABLE constraint_rename_test2;
 DROP TABLE constraint_rename_test;
+ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; -- ok
+NOTICE:  relation "constraint_not_exist" does not exist, skipping
 ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a);
 NOTICE:  relation "constraint_rename_test" does not exist, skipping
 -- FOREIGN KEY CONSTRAINT adding TEST
index d0d29ee62b73bdb3d08544db11f361294682bbfc..b5ee7b087df8cc225f19c0a866558387ae156d9c 100644 (file)
@@ -228,6 +228,7 @@ ALTER TABLE constraint_rename_test RENAME CONSTRAINT con3 TO con3foo; -- ok
 \d constraint_rename_test2
 DROP TABLE constraint_rename_test2;
 DROP TABLE constraint_rename_test;
+ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; -- ok
 ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a);
 
 -- FOREIGN KEY CONSTRAINT adding TEST