-
Notifications
You must be signed in to change notification settings - Fork 52
Open
Labels
bugSomething isn't workingSomething isn't working
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
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working