Skip to content

Conversation

@vshulev
Copy link
Collaborator

@vshulev vshulev commented Jan 22, 2026

Summary

This adds support for tracking dependencies between RLS policies and user-defined functions that they reference in USING/CHECK expressions.

Previously, pg-schema-diff could create policies before the functions they depend on, causing migration failures like:

ERROR: function is_admin(uuid) does not exist

This happened because the dependency graph didn't track policy→function relationships, so when both a function and a policy referencing it were added, the policy could be created first.

Changes

  • Query pg_depend to extract function dependencies from policies (similar to existing cross-table policy dependency tracking)
  • Add FunctionDependencies field to the Policy struct
  • Update dependency graph to order function creation before policies that use them
  • Update dependency graph to order policy deletion before function deletion
  • Add acceptance tests for policy-to-function dependencies (add and drop scenarios)
  • Add schema test for function dependency extraction

Test plan

  • All existing tests pass
  • New acceptance tests for adding policy with new function
  • New acceptance tests for dropping policy and function it references
  • New acceptance tests for non-public schema scenarios
  • Schema test verifying FunctionDependencies extraction

🤖 Generated with Claude Code

This adds support for tracking dependencies between RLS policies and
user-defined functions that they reference in USING/CHECK expressions.

Previously, pg-schema-diff could create policies before the functions
they depend on, causing migration failures when a policy's USING or
CHECK expression calls a user-defined function.

Changes:
- Query pg_depend to extract function dependencies from policies
- Add FunctionDependencies field to the Policy struct
- Update dependency graph to order function creation before policies
- Update dependency graph to order policy deletion before function deletion

This follows the same pattern used for cross-table policy dependencies
(issue #266).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@gemini-code-assist
Copy link

Summary of Changes

Hello @vshulev, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances pg-schema-diff by introducing robust dependency tracking for Row Level Security (RLS) policies that utilize user-defined functions. Previously, migrations could fail due to incorrect ordering, where policies were created before their required functions. The changes ensure that the schema migration process correctly identifies and orders these dependencies, guaranteeing that functions are always in place before policies that reference them are applied, and are removed only after dependent policies are gone.

Highlights

  • Policy-to-Function Dependency Tracking: Implemented a mechanism to track dependencies between RLS policies and user-defined functions referenced in their USING/CHECK expressions.
  • Migration Order Correction: Resolved issues where pg-schema-diff could create policies before their dependent functions, preventing migration failures.
  • Dependency Graph Enhancement: Updated the dependency graph to ensure functions are created before policies that use them, and policies are dropped before their dependent functions.
  • Schema and Query Updates: Modified the pg_policy query to extract function dependencies and updated the Policy struct to store this information.
  • Comprehensive Testing: Added new acceptance tests covering add and drop scenarios for policy-to-function dependencies, including non-public schema cases, and a schema test for dependency extraction.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully implements policy-to-function dependency tracking, addressing a critical issue where RLS policies could be created before their dependent functions, leading to migration failures. The changes are comprehensive, covering SQL query modifications to extract function dependencies, updates to the Go schema representation, and crucial adjustments to the SQL generation logic to ensure correct ordering of policy and function DDL statements. The addition of new acceptance and schema tests thoroughly validates the new functionality for both add and drop scenarios, including non-public schemas. The code is well-structured, clearly commented, and directly resolves the identified problem, significantly improving the robustness of schema migrations involving RLS policies and user-defined functions.

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.

2 participants