Skip to content

Commit 271f5a3

Browse files
committed
workload/schemachange: properly escape role names in policy statements
When ALTER POLICY is executed with unescaped role identifiers that start with numbers, it can cause SQL syntax errors like "trailing junk after numeric literal". This change adds proper SQL identifier escaping for role names in ALTER POLICY statements by using lexbase.EscapeSQLIdent, which ensures that identifiers are properly quoted with double quotes. The change also avoids a `InFailedSQLTransaction` error by checking `row.Err()` in the `findExistingPolicy` function. Fixes: #145277 Release note: none
1 parent 0229d90 commit 271f5a3

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

pkg/workload/schemachange/operation_generator.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5115,6 +5115,10 @@ func findExistingPolicy(
51155115
policyExists = true
51165116
}
51175117

5118+
if rows.Err() != nil {
5119+
return nil, false, rows.Err()
5120+
}
5121+
51185122
return &policyWithInfo, policyExists, nil
51195123
}
51205124

@@ -5236,7 +5240,7 @@ func (og *operationGenerator) alterPolicy(ctx context.Context, tx pgx.Tx) (*opSt
52365240
usesDummyRole = true
52375241
}
52385242

5239-
sqlStatement.WriteString(fmt.Sprintf(" TO %s", roles))
5243+
sqlStatement.WriteString(fmt.Sprintf(" TO %s", lexbase.EscapeSQLIdent(roles)))
52405244
default: // USING and/or WITH CHECK expressions
52415245
// For case 2 and 3, we generate USING, WITH CHECK, or both
52425246
includeUsing = alterType == 2 || og.randIntn(2) == 0
@@ -5278,9 +5282,8 @@ func (og *operationGenerator) alterPolicy(ctx context.Context, tx pgx.Tx) (*opSt
52785282
opStmt.sql = sqlStatement.String()
52795283

52805284
opStmt.expectedExecErrors.addAll(codesWithConditions{
5281-
{code: pgcode.UndefinedObject, condition: !policyExists},
5285+
{code: pgcode.UndefinedObject, condition: !policyExists || usesDummyRole},
52825286
{code: pgcode.UndefinedTable, condition: !tableExists},
5283-
{code: pgcode.UndefinedObject, condition: usesDummyRole},
52845287
})
52855288

52865289
return opStmt, nil

0 commit comments

Comments
 (0)