Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bingou
Boris Verkhovskiy <boris.verk@gmail.com>
Christian Jorgensen <chr.jorgensen1@gmail.com>
Christopher Manouvrier <chris@dovetailapp.com>
cnbei <cnbay@outlook.com>
Damon Davison <ddavison@avalere.com>
Daniël van Eeden <daniel.van.eeden@pingcap.com>
Davut Can Abacigil <can@teamsql.io>
Expand Down
4 changes: 4 additions & 0 deletions src/languages/postgresql/postgresql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const reservedClauses = expandPhrases([
'OFFSET',
'FETCH {FIRST | NEXT}',
'FOR {UPDATE | NO KEY UPDATE | SHARE | KEY SHARE} [OF]',
// CREATE/ALTER POLICY (issue #928)
'FOR {SELECT | INSERT | DELETE}',
// Data manipulation
// - insert:
'INSERT INTO',
Expand Down Expand Up @@ -265,6 +267,8 @@ const reservedKeywordPhrases = expandPhrases([
'IS [NOT] DISTINCT FROM',
'NULLS {FIRST | LAST}',
'WITH ORDINALITY',
// CREATE/ALTER POLICY: do not treat WITH CHECK as a CTE WITH clause (issue #928)
'WITH CHECK',
]);

const reservedDataTypePhrases = expandPhrases([
Expand Down
58 changes: 58 additions & 0 deletions test/postgresql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,64 @@ describe('PostgreSqlFormatter', () => {
`);
});

// Issue #928
it('formats CREATE POLICY FOR SELECT and FOR INSERT consistently', () => {
expect(
format(`
CREATE POLICY "Allow users to select their own records" ON public.user_achievements
FOR SELECT
TO authenticated
USING (user_id = auth.uid());

CREATE POLICY "Allow users to insert their own records" ON public.user_achievements
FOR INSERT
TO authenticated
WITH CHECK (user_id = auth.uid());
`)
).toBe(dedent`
CREATE POLICY "Allow users to select their own records" ON public.user_achievements
FOR SELECT
TO authenticated USING (user_id = auth.uid ());

CREATE POLICY "Allow users to insert their own records" ON public.user_achievements
FOR INSERT
TO authenticated WITH CHECK (user_id = auth.uid ());
`);
});

it('formats CREATE POLICY FOR DELETE and WITH CHECK', () => {
expect(
format(`
CREATE POLICY p_del ON public.t
FOR DELETE
TO authenticated
USING (user_id = auth.uid());

CREATE POLICY p_ins ON public.t
WITH CHECK (true);
`)
).toBe(dedent`
CREATE POLICY p_del ON public.t
FOR DELETE
TO authenticated USING (user_id = auth.uid ());

CREATE POLICY p_ins ON public.t WITH CHECK (true);
`);
});

it('formats ALTER POLICY USING and WITH CHECK', () => {
expect(
format(`
ALTER POLICY p ON public.t
TO authenticated
USING (user_id = 1)
WITH CHECK (user_id = 1);
`)
).toBe(dedent`
ALTER POLICY p ON public.t TO authenticated USING (user_id = 1) WITH CHECK (user_id = 1);
`);
});

// Issue #711
it('supports OPERATOR() syntax', () => {
expect(format(`SELECT foo OPERATOR(public.===) bar;`)).toBe(dedent`
Expand Down