Skip to content

fix(pg-delta): order RLS policy creation after referenced new tables#187

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/add-order-rls-policies
Draft

fix(pg-delta): order RLS policy creation after referenced new tables#187
Copilot wants to merge 2 commits intomainfrom
copilot/add-order-rls-policies

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 10, 2026

RLS policies can reference other relations inside USING / WITH CHECK. When both the policy target table and the referenced table are new, pg-delta could emit CREATE POLICY before the referenced table existed, causing plan/apply failures on an empty database.

  • Policy dependency extraction

    • Extend CreateRlsPolicy.requires to include table references found in policy expressions.
    • This adds the missing ordering edge without changing the planner itself.
  • Shared expression parsing

    • Export a small pg-topo helper for extracting dependencies from raw SQL expressions.
    • Reuse that helper from pg-delta instead of duplicating AST traversal logic.
  • Regression coverage

    • Add a pg-delta integration test covering:
      • two newly created tables
      • a policy on one table
      • a USING expression that references the other new table
    • Add unit coverage for policy requires and helper coverage in pg-topo.

Example of the newly tracked dependency:

CREATE TABLE app.accounts (id integer primary key);
CREATE TABLE app.users (id integer primary key);

ALTER TABLE app.accounts ENABLE ROW LEVEL SECURITY;

CREATE POLICY account_access ON app.accounts
  FOR SELECT
  USING (EXISTS (SELECT 1 FROM app.users));

With this change, pg-delta treats the policy as depending on app.users, so CREATE POLICY is ordered after the referenced table exists.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 10, 2026

🦋 Changeset detected

Latest commit: 7d2eb60

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@supabase/pg-delta Patch
@supabase/pg-topo Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI changed the title [WIP] Add order for RLS policies after referenced new tables fix(pg-delta): order RLS policy creation after referenced new tables Apr 10, 2026
Copilot AI requested a review from avallete April 10, 2026 16:45
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.

pg-delta: order RLS policies after referenced new tables

2 participants