[BUG] Policy ordering: policy created before referenced table exists
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
Start at the migration diff generator and reproduce the issue with the tenant.projects and tenant.project_members schema described here. Trace how policy USING and WITH CHECK references are handled when ordering statements. Done means referenced tables are emitted before the policy, so the generated migration applies successfully.
Written by the indexing model from the issue text.
Description
Describe the bug**
When generating a migration diff, a policy that references another table in its USING clause is ordered before the referenced table is created, causing the migration to fail.
Expected behavior**
The diff generator should analyze policy USING and WITH CHECK clauses for table references and order statements so that:
- All tables referenced by a policy are created first
- The policy is created after its dependent tables exist
Correct ordering would be:
- CREATE TABLE
tenant.projects - CREATE TABLE
tenant.project_members - CREATE POLICY
project_members_org_policy
To Reproduce
Given a schema with:
- Table
tenant.project_memberswith a row-level security policy:
CREATE POLICY project_members_org_policy ON tenant.project_members
AS PERMISSIVE
FOR ALL
TO fun_fundament_api
USING (EXISTS (
SELECT 1 FROM projects
WHERE projects.id = project_members.project_id
AND projects.organization_id = current_setting('app.current_organization_id')::uuid
));
- Table
tenant.projects(referenced by the policy above)
Observed Behavior
The generated migration orders statements as:
- CREATE TABLE
tenant.project_members - CREATE POLICY
project_members_org_policy(fails - referencestenant.projectswhich doesn't exist yet) - ... other statements ...
- CREATE TABLE
tenant.projects
Context
pg-schema-diff version: v1.0.5
pg-schema-diff usage: LIBRARY
Postgres version: 18
- Dominant language
- Go
- Stars
- 884
- Forks
- 82
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from stripe/pg-schema-diff
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 76/100
stripe/pg-schema-diff#302 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
stripe/pg-schema-diff#283 · 1 reaction ·
-
Difficulty 4/5 3-5 days Newbie friendliness 50/100
stripe/pg-schema-diff#301 ·
-
Is the repo active? Open
Difficulty 1/5 Under an hour Newbie friendliness 15/100
stripe/pg-schema-diff#289 · 7 reactions ·
-
Difficulty 3/5 1-2 days Newbie friendliness 76/100
stripe/pg-schema-diff#284 ·
All issues in stripe/pg-schema-diff
Similar issues
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
blinklabs-io/bursa#904 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 comments ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100