Skip to content

Commit b4d981d

Browse files
committed
Support ALTER POLICY statement
1 parent 826ed46 commit b4d981d

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

src/cst/AlterAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ export type AlterTriggerAction =
163163
| AlterActionDependsOnExtension
164164
| AlterActionNoDependsOnExtension;
165165

166+
export type AlterPolicyAction = AlterActionRename;
167+
166168
export interface AlterActionRename extends BaseNode {
167169
type: "alter_action_rename";
168170
renameKw: Keyword<"RENAME"> | [Keyword<"RENAME">, Keyword<"TO" | "AS">];

src/cst/Policy.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { AlterPolicyAction } from "./AlterAction";
12
import { BaseNode, Keyword } from "./Base";
23
import { Grantee } from "./Dcl";
34
import { Identifier, EntityName, Expr, ParenExpr, ListExpr } from "./Expr";
45

56
export type AllPolicyNodes = AllPolicyStatements | CreatePolicyClause;
67

7-
export type AllPolicyStatements = CreatePolicyStmt | DropPolicyStmt;
8+
export type AllPolicyStatements =
9+
| CreatePolicyStmt
10+
| AlterPolicyStmt
11+
| DropPolicyStmt;
812

913
// CREATE POLICY name ON table
1014
export interface CreatePolicyStmt extends BaseNode {
@@ -61,6 +65,20 @@ export interface PolicyCheckClause extends BaseNode {
6165
expr: ParenExpr<Expr>;
6266
}
6367

68+
export interface AlterPolicyStmt extends BaseNode {
69+
type: "alter_policy_stmt";
70+
alterPolicyKw: [Keyword<"ALTER">, Keyword<"POLICY">];
71+
name: Identifier;
72+
onKw: Keyword<"ON">;
73+
table: EntityName;
74+
actions: (
75+
| AlterPolicyAction
76+
| PolicyRolesClause
77+
| PolicyUsingClause
78+
| PolicyCheckClause
79+
)[];
80+
}
81+
6482
export interface DropPolicyStmt extends BaseNode {
6583
type: "drop_policy_stmt";
6684
dropPolicyKw: [Keyword<"DROP">, Keyword<"POLICY">];

src/parser.pegjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ ddl_statement_postgres
171171
/ set_role_stmt
172172
/ reset_role_stmt
173173
/ create_policy_stmt
174+
/ alter_policy_stmt
174175
/ drop_policy_stmt
175176

176177
dml_statement
@@ -4704,6 +4705,24 @@ policy_check_clause
47044705
return loc({ type: "policy_check_clause", withKw: read(withKw), checkKw: read(checkKw), expr });
47054706
}
47064707

4708+
alter_policy_stmt
4709+
= kw:(ALTER __ POLICY __) name:(ident __) onKw:(ON __) table:entity_name actions:(__ alter_policy_action)+ {
4710+
return loc({
4711+
type: "alter_policy_stmt",
4712+
alterPolicyKw: read(kw),
4713+
name: read(name),
4714+
onKw: read(onKw),
4715+
table,
4716+
actions: actions.map(read),
4717+
});
4718+
}
4719+
4720+
alter_policy_action
4721+
= alter_action_rename
4722+
/ policy_roles_clause
4723+
/ policy_using_clause
4724+
/ policy_check_clause
4725+
47074726
drop_policy_stmt
47084727
= kw:(DROP __ POLICY __) ifExistsKw:(if_exists __)? name:(ident __) onKw:(ON __) table:entity_name behaviorKw:(__ (CASCADE / RESTRICT))? {
47094728
return loc({

src/showNode/policy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export const policyMap: FullTransformMap<string, AllPolicyNodes> = {
1515
policy_using_clause: (node) => show([node.usingKw, node.expr]),
1616
policy_check_clause: (node) => show([node.withKw, node.checkKw, node.expr]),
1717

18+
// ALTER POLICY
19+
alter_policy_stmt: (node) =>
20+
show([node.alterPolicyKw, node.name, node.onKw, node.table, node.actions]),
21+
1822
// DROP POLICY
1923
drop_policy_stmt: (node) =>
2024
show([

test/ddl/policy.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,41 @@ describe("policy", () => {
4848
});
4949
});
5050

51+
describe("ALTER POLICY", () => {
52+
it("supports ALTER POLICY .. RENAME TO", () => {
53+
testWc("ALTER POLICY foo ON my_table RENAME TO bar");
54+
testWc("ALTER POLICY foo ON my_schema.my_table RENAME TO bar");
55+
});
56+
57+
it("supports ALTER POLICY .. TO role", () => {
58+
testWc("ALTER POLICY foo ON my_table TO my_role");
59+
testWc("ALTER POLICY foo ON my_table TO PUBLIC");
60+
testWc("ALTER POLICY foo ON my_table TO CURRENT_USER");
61+
testWc("ALTER POLICY foo ON my_table TO CURRENT_ROLE");
62+
testWc("ALTER POLICY foo ON my_table TO SESSION_USER");
63+
testWc("ALTER POLICY foo ON my_table TO bar, baz, SESSION_USER");
64+
});
65+
66+
it("supports ALTER POLICY .. USING ()", () => {
67+
testWc("ALTER POLICY foo ON my_table USING (age > 10)");
68+
});
69+
70+
it("supports ALTER POLICY .. WITH CHECK ()", () => {
71+
testWc("ALTER POLICY foo ON my_table WITH CHECK (name <> '')");
72+
});
73+
74+
it("supports combination of ALTER POLICY-s", () => {
75+
testWc(`
76+
ALTER POLICY foo ON my_table
77+
TO my_role
78+
USING (age > 10)
79+
WITH CHECK (FALSE)
80+
`);
81+
});
82+
});
83+
5184
describe("DROP POLICY", () => {
52-
it("supports CREATE POLICY .. ON ..", () => {
85+
it("supports DROP POLICY .. ON ..", () => {
5386
testWc("DROP POLICY foo ON my_table");
5487
testWc("DROP POLICY foo ON my_schema.my_table");
5588
});

0 commit comments

Comments
 (0)