fix: preserve array type casts in policy expressions (#345)#348
fix: preserve array type casts in policy expressions (#345)#348
Conversation
The normalizeExpressionParentheses function was stripping ::text from
array literal casts like '{nested,key}'::text[], leaving invalid SQL
'{nested,key}'[]. Fix the regex to not match ::text when followed by [].
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Greptile SummaryThis PR fixes a bug where Key changes:
Issues found:
Confidence Score: 3/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["Policy expression string\n(from pg_dump)"] --> B["normalizeExpressionParentheses()"]
B --> B1["Step 1: Wrap in outer parens\nif missing"]
B1 --> B2["Step 2: Remove redundant parens\naround function calls\n(loop until stable)"]
B2 --> B3["Step 3: Strip redundant ::text casts\nRegex: '([^']+)'::text([^[\\w]|$)\n→ preserves ::text[]"]
B3 --> C{"Was ::text\nfollowed by []?"}
C -- "Yes → [^[\\w] won't match [" --> D["Cast preserved\n'{nested,key}'::text[]"]
C -- "No → char or end-of-string captured as \\$2" --> E["Cast stripped\n'x'::text) → 'x')"]
D --> F["Normalized expression\nreturned"]
E --> F
style D fill:#c8f7c5
style E fill:#c8f7c5
|
There was a problem hiding this comment.
Pull request overview
Fixes pgschema dump output normalization so explicit ::text[] casts in policy expressions aren’t corrupted (e.g., preserving '{nested,key}'::text[] instead of producing invalid '{nested,key}'[]).
Changes:
- Tightened the
normalizeExpressionParenthesesredundant text-cast regex to avoid matching::textwhen it’s part of an array cast (::text[]). - Added a new dump integration fixture for issue #345 covering
#>>with an explicit::text[]cast in an RLS policy. - Added an integration test to run the new fixture.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
ir/normalize.go |
Updates expression normalization regex to preserve ::text[] casts in policy expressions. |
cmd/dump/dump_integration_test.go |
Adds a new integration test to validate the issue #345 fixture. |
testdata/dump/issue_345_array_cast/raw.sql |
Provides a minimal repro schema for the issue. |
testdata/dump/issue_345_array_cast/pgdump.sql |
Captures pg_dump output used as the input for the integration test. |
testdata/dump/issue_345_array_cast/pgschema.sql |
Stores the expected pgschema dump output, ensuring the cast is preserved. |
testdata/dump/issue_345_array_cast/manifest.json |
Adds fixture metadata for the new test case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Fix regex comment to accurately describe [^[\w] excluding both '[' and word characters, not just '[' - Use 'source' field instead of 'source_url' and 'notes' as array to match other dump fixture manifests Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
pgschema dumpwas stripping::textfrom explicit array literal casts like'{nested,key}'::text[], producing invalid SQL'{nested,key}'[]normalizeExpressionParenthesesfunction's regex'([^']+)'::textmatched::texteven when followed by[](array notation)'([^']+)'::text([^[\w]|$)which requires a non-[character or end-of-string after::text, preserving the trailing character in the replacementFixes #345
Test plan
TestDumpCommand_Issue345ArrayCastwith policy using#>>operator and::text[]cast🤖 Generated with Claude Code