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_members with 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 - references tenant.projects which 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
Describe the bug**
When generating a migration diff, a policy that references another table in its
USINGclause is ordered before the referenced table is created, causing the migration to fail.Expected behavior**
The diff generator should analyze policy
USINGandWITH CHECKclauses for table references and order statements so that:Correct ordering would be:
tenant.projectstenant.project_membersproject_members_org_policyTo Reproduce
Given a schema with:
tenant.project_memberswith a row-level security policy:tenant.projects(referenced by the policy above)Observed Behavior
The generated migration orders statements as:
tenant.project_membersproject_members_org_policy(fails - referencestenant.projectswhich doesn't exist yet)tenant.projectsContext
pg-schema-diff version: v1.0.5
pg-schema-diff usage: LIBRARY
Postgres version: 18