Skip to content

Commit 9ee44a9

Browse files
committed
Deparser: Fix handling of constraint key named "value" in ALTER TABLE
This was being ignored in all cases, but it should only be ignored if the Constraint object is originating in an ALTER DOMAIN statement. pganalyze/pg_query_go#148
1 parent 7be1aed commit 9ee44a9

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/postgres_deparse.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ typedef enum DeparseNodeContext {
9393
DEPARSE_NODE_CONTEXT_A_EXPR,
9494
DEPARSE_NODE_CONTEXT_CREATE_TYPE,
9595
DEPARSE_NODE_CONTEXT_ALTER_TYPE,
96+
DEPARSE_NODE_CONTEXT_ALTER_DOMAIN,
9697
DEPARSE_NODE_CONTEXT_SET_STATEMENT,
9798
DEPARSE_NODE_CONTEXT_FUNC_EXPR,
9899
DEPARSE_NODE_CONTEXT_SELECT_SETOP,
@@ -231,7 +232,7 @@ static void deparseJsonFuncExpr(DeparseState *state, JsonFuncExpr *json_func_exp
231232
static void deparseJsonQuotesClauseOpt(DeparseState *state, JsonQuotes quotes);
232233
static void deparseJsonOnErrorClauseOpt(DeparseState *state, JsonBehavior *behavior);
233234
static void deparseJsonOnEmptyClauseOpt(DeparseState *state, JsonBehavior *behavior);
234-
static void deparseConstraint(DeparseState *state, Constraint *constraint);
235+
static void deparseConstraint(DeparseState *state, Constraint *constraint, DeparseNodeContext context);
235236
static void deparseSchemaStmt(DeparseState *state, Node *node);
236237
static void deparseExecuteStmt(DeparseState *state, ExecuteStmt *execute_stmt);
237238
static void deparseTriggerTransition(DeparseState *state, TriggerTransition *trigger_transition);
@@ -4769,7 +4770,7 @@ static void deparseColumnDef(DeparseState *state, ColumnDef *column_def)
47694770

47704771
foreach(lc, column_def->constraints)
47714772
{
4772-
deparseConstraint(state, castNode(Constraint, lfirst(lc)));
4773+
deparseConstraint(state, castNode(Constraint, lfirst(lc)), DEPARSE_NODE_CONTEXT_NONE);
47734774
deparseAppendStringInfoChar(state, ' ');
47744775
}
47754776

@@ -5332,7 +5333,7 @@ static void deparseCreateDomainStmt(DeparseState *state, CreateDomainStmt *creat
53325333

53335334
foreach(lc, create_domain_stmt->constraints)
53345335
{
5335-
deparseConstraint(state, castNode(Constraint, lfirst(lc)));
5336+
deparseConstraint(state, castNode(Constraint, lfirst(lc)), DEPARSE_NODE_CONTEXT_NONE);
53365337
deparseAppendStringInfoChar(state, ' ');
53375338
}
53385339

@@ -5381,7 +5382,7 @@ static void deparseCreateExtensionStmt(DeparseState *state, CreateExtensionStmt
53815382
}
53825383

53835384
// "ColConstraintElem" and "ConstraintElem" in gram.y
5384-
static void deparseConstraint(DeparseState *state, Constraint *constraint)
5385+
static void deparseConstraint(DeparseState *state, Constraint *constraint, DeparseNodeContext context)
53855386
{
53865387
ListCell *lc;
53875388

@@ -5487,7 +5488,7 @@ static void deparseConstraint(DeparseState *state, Constraint *constraint)
54875488
{
54885489
bool valueOnly = false;
54895490

5490-
if (list_length(constraint->keys) == 1) {
5491+
if (context == DEPARSE_NODE_CONTEXT_ALTER_DOMAIN && list_length(constraint->keys) == 1) {
54915492
Node* firstKey = constraint->keys->elements[0].ptr_value;
54925493
valueOnly = IsA(firstKey, String) && !strcmp("value", ((String*)firstKey)->sval);
54935494
}
@@ -5997,7 +5998,7 @@ static void deparseTableElement(DeparseState *state, Node *node)
59975998
deparseTableLikeClause(state, castNode(TableLikeClause, node));
59985999
break;
59996000
case T_Constraint:
6000-
deparseConstraint(state, castNode(Constraint, node));
6001+
deparseConstraint(state, castNode(Constraint, node), DEPARSE_NODE_CONTEXT_NONE);
60016002
break;
60026003
default:
60036004
Assert(false);
@@ -7286,7 +7287,7 @@ static void deparseAlterTableCmd(DeparseState *state, AlterTableCmd *alter_table
72867287
case AT_AddIdentity:
72877288
case AT_AddConstraint:
72887289
case AT_AlterConstraint:
7289-
deparseConstraint(state, castNode(Constraint, alter_table_cmd->def));
7290+
deparseConstraint(state, castNode(Constraint, alter_table_cmd->def), DEPARSE_NODE_CONTEXT_NONE);
72907291
deparseAppendStringInfoChar(state, ' ');
72917292
break;
72927293
case AT_SetIdentity:
@@ -7452,7 +7453,7 @@ static void deparseAlterDomainStmt(DeparseState *state, AlterDomainStmt *alter_d
74527453
break;
74537454
case 'C':
74547455
deparseAppendStringInfoString(state, "ADD ");
7455-
deparseConstraint(state, castNode(Constraint, alter_domain_stmt->def));
7456+
deparseConstraint(state, castNode(Constraint, alter_domain_stmt->def), DEPARSE_NODE_CONTEXT_ALTER_DOMAIN);
74567457
break;
74577458
case 'X':
74587459
deparseAppendStringInfoString(state, "DROP CONSTRAINT ");

test/deparse_tests.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ const char* tests[] = {
416416
"CREATE TABLE my_table (created_at timestamptz NOT NULL DEFAULT '1 hour'::interval + (current_timestamp AT TIME ZONE 'UTC'))",
417417
"ALTER TABLE my_table ADD COLUMN created_at timestamptz NOT NULL DEFAULT '1 hour'::interval + (current_timestamp AT TIME ZONE 'UTC')",
418418
"CREATE TABLE my_table (created_at int NOT NULL DEFAULT 1 + 2)",
419-
"/* Comment 1 */ SELECT 1; /* Comment 2 */ SELECT 2"
419+
"/* Comment 1 */ SELECT 1; /* Comment 2 */ SELECT 2",
420+
"ALTER TABLE ONLY public.api_keys ADD CONSTRAINT api_keys_value_unique UNIQUE (value)"
420421
};
421422

422423
size_t testsLength = __LINE__ - 4;

0 commit comments

Comments
 (0)