Skip to content

Commit

Permalink
Grammar: Add DROP POLICY statement to postgres dialect (#2024)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpy-git committed Dec 2, 2021
1 parent a4973fe commit 2dbeafa
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/sqlfluff/dialects/dialect_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,7 @@ class StatementSegment(BaseSegment):
Ref("SetStatementSegment"),
Ref("DropFunctionStatementSegment"),
Ref("CreatePolicyStatementSegment"),
Ref("DropPolicyStatementSegment"),
],
)

Expand Down Expand Up @@ -2452,3 +2453,23 @@ class CreatePolicyStatementSegment(BaseSegment):
Sequence("USING", Bracketed(Ref("ExpressionSegment")), optional=True),
Sequence("WITH", "CHECK", Bracketed(Ref("ExpressionSegment")), optional=True),
)


@postgres_dialect.segment()
class DropPolicyStatementSegment(BaseSegment):
"""A `DROP POLICY` statement.
As Specified in https://www.postgresql.org/docs/14/sql-droppolicy.html
"""

type = "drop_policy_statement"
match_grammar = StartsWith(Sequence("DROP", "POLICY"))
parse_grammar = Sequence(
"DROP",
"POLICY",
Ref("IfExistsGrammar", optional=True),
Ref("ObjectReferenceSegment"),
"ON",
Ref("TableReferenceSegment"),
OneOf("CASCADE", "RESTRICT", optional=True),
)
5 changes: 5 additions & 0 deletions test/fixtures/dialects/postgres/postgres_drop_policy.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP POLICY account_managers ON accounts;
DROP POLICY IF EXISTS account_managers ON accounts;
DROP POLICY account_managers ON accounts CASCADE;
DROP POLICY account_managers ON accounts RESTRICT;
DROP POLICY IF EXISTS account_managers ON accounts RESTRICT;
64 changes: 64 additions & 0 deletions test/fixtures/dialects/postgres/postgres_drop_policy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# YML test files are auto-generated from SQL files and should not be edited by
# hand. To help enforce this, the "hash" field in the file must match a hash
# computed by SQLFluff when running the tests. Please run
# `python test/generate_parse_fixture_yml.py` to generate them after adding or
# altering SQL files.
_hash: 5e9709e8abf96cd4c55171f63ddb5b2dbc1c2b9298c8d6ff94ac36e11a7675ef
file:
- statement:
drop_policy_statement:
- keyword: DROP
- keyword: POLICY
- object_reference:
identifier: account_managers
- keyword: 'ON'
- table_reference:
identifier: accounts
- statement_terminator: ;
- statement:
drop_policy_statement:
- keyword: DROP
- keyword: POLICY
- keyword: IF
- keyword: EXISTS
- object_reference:
identifier: account_managers
- keyword: 'ON'
- table_reference:
identifier: accounts
- statement_terminator: ;
- statement:
drop_policy_statement:
- keyword: DROP
- keyword: POLICY
- object_reference:
identifier: account_managers
- keyword: 'ON'
- table_reference:
identifier: accounts
- keyword: CASCADE
- statement_terminator: ;
- statement:
drop_policy_statement:
- keyword: DROP
- keyword: POLICY
- object_reference:
identifier: account_managers
- keyword: 'ON'
- table_reference:
identifier: accounts
- keyword: RESTRICT
- statement_terminator: ;
- statement:
drop_policy_statement:
- keyword: DROP
- keyword: POLICY
- keyword: IF
- keyword: EXISTS
- object_reference:
identifier: account_managers
- keyword: 'ON'
- table_reference:
identifier: accounts
- keyword: RESTRICT
- statement_terminator: ;

0 comments on commit 2dbeafa

Please sign in to comment.