Skip to content

Commit 826ed46

Browse files
committed
Support DROP POLICY statement
1 parent ef94e2e commit 826ed46

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/cst/Policy.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Identifier, EntityName, Expr, ParenExpr, ListExpr } from "./Expr";
44

55
export type AllPolicyNodes = AllPolicyStatements | CreatePolicyClause;
66

7-
export type AllPolicyStatements = CreatePolicyStmt;
7+
export type AllPolicyStatements = CreatePolicyStmt | DropPolicyStmt;
88

99
// CREATE POLICY name ON table
1010
export interface CreatePolicyStmt extends BaseNode {
@@ -60,3 +60,13 @@ export interface PolicyCheckClause extends BaseNode {
6060
checkKw: Keyword<"CHECK">;
6161
expr: ParenExpr<Expr>;
6262
}
63+
64+
export interface DropPolicyStmt extends BaseNode {
65+
type: "drop_policy_stmt";
66+
dropPolicyKw: [Keyword<"DROP">, Keyword<"POLICY">];
67+
ifExistsKw?: [Keyword<"IF">, Keyword<"EXISTS">];
68+
name: Identifier;
69+
onKw: Keyword<"ON">;
70+
table: EntityName;
71+
behaviorKw?: Keyword<"CASCADE" | "RESTRICT">;
72+
}

src/parser.pegjs

Lines changed: 14 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+
/ drop_policy_stmt
174175

175176
dml_statement
176177
= compound_select_stmt
@@ -4703,6 +4704,19 @@ policy_check_clause
47034704
return loc({ type: "policy_check_clause", withKw: read(withKw), checkKw: read(checkKw), expr });
47044705
}
47054706

4707+
drop_policy_stmt
4708+
= kw:(DROP __ POLICY __) ifExistsKw:(if_exists __)? name:(ident __) onKw:(ON __) table:entity_name behaviorKw:(__ (CASCADE / RESTRICT))? {
4709+
return loc({
4710+
type: "drop_policy_stmt",
4711+
dropPolicyKw: read(kw),
4712+
ifExistsKw: read(ifExistsKw),
4713+
name: read(name),
4714+
onKw: read(onKw),
4715+
table,
4716+
behaviorKw: read(behaviorKw),
4717+
});
4718+
}
4719+
47064720
/**
47074721
* ------------------------------------------------------------------------------------ *
47084722
* *

src/showNode/policy.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,15 @@ export const policyMap: FullTransformMap<string, AllPolicyNodes> = {
1414
policy_roles_clause: (node) => show([node.toKw, node.roles]),
1515
policy_using_clause: (node) => show([node.usingKw, node.expr]),
1616
policy_check_clause: (node) => show([node.withKw, node.checkKw, node.expr]),
17+
18+
// DROP POLICY
19+
drop_policy_stmt: (node) =>
20+
show([
21+
node.dropPolicyKw,
22+
node.ifExistsKw,
23+
node.name,
24+
node.onKw,
25+
node.table,
26+
node.behaviorKw,
27+
]),
1728
};

test/ddl/policy.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ describe("policy", () => {
4747
`);
4848
});
4949
});
50+
51+
describe("DROP POLICY", () => {
52+
it("supports CREATE POLICY .. ON ..", () => {
53+
testWc("DROP POLICY foo ON my_table");
54+
testWc("DROP POLICY foo ON my_schema.my_table");
55+
});
56+
57+
it("supports IF EXISTS", () => {
58+
testWc("DROP POLICY IF EXISTS foo ON my_table");
59+
});
60+
61+
it("supports CASCADE/RESTRICT", () => {
62+
testWc("DROP POLICY foo ON my_table CASCADE");
63+
testWc("DROP POLICY foo ON my_table RESTRICT");
64+
});
65+
});
5066
});
5167

5268
notDialect("postgresql", () => {

0 commit comments

Comments
 (0)