Skip to content

Conversation

@tianzhou
Copy link
Contributor

@tianzhou tianzhou commented Jan 27, 2026

PostgreSQL's CREATE POLICY syntax requires parentheses around USING and WITH CHECK expressions. When pg_get_expr returns simple function calls without outer parentheses (e.g., is_admin()), pgschema was outputting invalid SQL like USING is_admin() instead of USING (is_admin()).

Added ensureParentheses() helper to wrap expressions in parentheses if not already wrapped.

Fix #259

PostgreSQL's CREATE POLICY syntax requires parentheses around USING and
WITH CHECK expressions. When pg_get_expr returns simple function calls
without outer parentheses (e.g., is_admin()), pgschema was outputting
invalid SQL like `USING is_admin()` instead of `USING (is_admin())`.

Added ensureParentheses() helper to wrap expressions in parentheses if
not already wrapped.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings January 27, 2026 15:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR ensures that generated CREATE POLICY / ALTER POLICY statements always use the PostgreSQL-required parenthesized syntax for USING and WITH CHECK expressions, even when pg_get_expr omits outer parentheses. It also adds file-based diff fixtures to validate the new behavior against a real-world reproduction of Issue #259.

Changes:

  • Updated internal/diff/policy.go to route USING and WITH CHECK expressions through a new ensureParentheses helper so that emitted SQL uses USING (expr) / WITH CHECK (expr) consistently.
  • Added an is_admin() helper function and an admin_only RLS policy in testdata/diff/create_policy/add_policy along with updated old.sql, new.sql, diff.sql, plan.sql, plan.txt, and plan.json fixtures to cover function-call policies and verify correct parentheses in generated SQL.
  • Refreshed the JSON plan fingerprint and policy entries to include the newly created admin_only policy.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
testdata/diff/create_policy/add_policy/plan.txt Adds the admin_only policy to the human-readable diff plan, ensuring the new policy appears in the expected migration steps.
testdata/diff/create_policy/add_policy/plan.sql Updates the expected SQL plan to include CREATE POLICY admin_only ... USING (is_admin());, validating correct parenthesization in generated SQL.
testdata/diff/create_policy/add_policy/plan.json Updates the plan JSON hash and adds a table.policy entry for public.users.admin_only, aligning structured expectations with the new policy.
testdata/diff/create_policy/add_policy/old.sql Introduces the is_admin() function in the “old” schema to set up the pre-diff state for Issue #259.
testdata/diff/create_policy/add_policy/new.sql Adds the admin_only RLS policy using USING (is_admin()); in the “new” schema, providing input DDL for the new regression test.
testdata/diff/create_policy/add_policy/diff.sql Extends the expected migration SQL diff to include the CREATE POLICY admin_only ... USING (is_admin()); statement.
internal/diff/policy.go Wraps policy.Using and policy.WithCheck with ensureParentheses in CREATE/ALTER policy generation and introduces the helper implementation, ensuring compliance with PostgreSQL’s USING (expr) / WITH CHECK (expr) syntax while preserving already-wrapped expressions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tianzhou tianzhou merged commit 2c987bd into main Jan 27, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing parentheses in CREATE POLICY USING clause output

1 participant