Skip to content
Merged
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
8 changes: 7 additions & 1 deletion ir/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func normalizePolicyRoles(roles []string) []string {

// normalizePolicyExpression normalizes policy expressions (USING/WITH CHECK clauses)
// It preserves parentheses as they are part of the expected format for policies
// tableSchema is used to strip same-schema qualifiers from function calls (Issue #220)
// tableSchema is used to strip same-schema qualifiers from function calls and table references (Issue #220, #224)
func normalizePolicyExpression(expr string, tableSchema string) string {
if expr == "" {
return expr
Expand All @@ -248,6 +248,12 @@ func normalizePolicyExpression(expr string, tableSchema string) string {
prefix := tableSchema + "."
pattern := regexp.MustCompile(regexp.QuoteMeta(prefix) + `([a-zA-Z_][a-zA-Z0-9_]*)\(`)
expr = pattern.ReplaceAllString(expr, `${1}(`)

// Strip same-schema qualifiers from table references (Issue #224)
// Matches schema.identifier followed by whitespace, comma, closing paren, or end of string
// Example: public.users -> users (when tableSchema is "public")
tablePattern := regexp.MustCompile(regexp.QuoteMeta(prefix) + `([a-zA-Z_][a-zA-Z0-9_]*)(\s|,|\)|$)`)
expr = tablePattern.ReplaceAllString(expr, `${1}${2}`)
}

// Handle all parentheses normalization (adding required ones, removing unnecessary ones)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE POLICY select_own_orders ON orders FOR SELECT TO PUBLIC USING (user_id IN ( SELECT u.id FROM users u WHERE (u.tenant_id = 1)));
21 changes: 21 additions & 0 deletions testdata/diff/create_policy/same_schema_table_reference/new.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Test case for Issue #224: Table references in policy expressions
-- This tests that same-schema table references are properly normalized

CREATE TABLE users (
id SERIAL PRIMARY KEY,
tenant_id INTEGER NOT NULL
);

CREATE TABLE orders (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id)
);

ALTER TABLE orders ENABLE ROW LEVEL SECURITY;

-- Policy with subquery referencing another table in the same schema
-- The table reference "users" should be normalized regardless of schema prefix
CREATE POLICY select_own_orders ON orders
FOR SELECT
TO PUBLIC
USING (user_id IN (SELECT u.id FROM users u WHERE u.tenant_id = 1));
14 changes: 14 additions & 0 deletions testdata/diff/create_policy/same_schema_table_reference/old.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Test case for Issue #224: Table references in policy expressions
-- This tests that same-schema table references are properly normalized

CREATE TABLE users (
id SERIAL PRIMARY KEY,
tenant_id INTEGER NOT NULL
);

CREATE TABLE orders (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id)
);

ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
20 changes: 20 additions & 0 deletions testdata/diff/create_policy/same_schema_table_reference/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "1.0.0",
"pgschema_version": "1.5.1",
"created_at": "1970-01-01T00:00:00Z",
"source_fingerprint": {
"hash": "48bc23dfa4645111f3629340b47451db977912c1c85e523f09f66d9548435fe8"
},
"groups": [
{
"steps": [
{
"sql": "CREATE POLICY select_own_orders ON orders FOR SELECT TO PUBLIC USING (user_id IN ( SELECT u.id FROM users u WHERE (u.tenant_id = 1)));",
"type": "table.policy",
"operation": "create",
"path": "public.orders.select_own_orders"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE POLICY select_own_orders ON orders FOR SELECT TO PUBLIC USING (user_id IN ( SELECT u.id FROM users u WHERE (u.tenant_id = 1)));
13 changes: 13 additions & 0 deletions testdata/diff/create_policy/same_schema_table_reference/plan.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Plan: 1 to modify.

Summary by type:
tables: 1 to modify

Tables:
~ orders
+ select_own_orders (policy)

DDL to be executed:
--------------------------------------------------

CREATE POLICY select_own_orders ON orders FOR SELECT TO PUBLIC USING (user_id IN ( SELECT u.id FROM users u WHERE (u.tenant_id = 1)));