-
Notifications
You must be signed in to change notification settings - Fork 29
fix: function to function dependency #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Add a new sqlc query to retrieve function-to-function dependencies from PostgreSQL's pg_depend system catalog. This query identifies functions that depend on other functions (deptype='n' for normal dependencies), which is essential for topologically sorting functions so that dependencies are created before dependents. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ames Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Replace alphabetical sorting with topological sorting in both generateCreateFunctionsSQL and generateDropFunctionsSQL. This ensures functions are ordered so dependencies come before dependents. Also fixes function dependency normalization in plan.go - the Dependencies field on ir.Function now gets normalized when replacing temporary schema names with target schema names, enabling proper dependency lookup during topological sorting. Updates test case to use SQL-standard function with DEFAULT parameter that references another function, which PostgreSQL tracks via pg_depend and is idempotent. Fixes #246 - functions that reference other functions are now ordered correctly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Updates expected output files for drop_function test case to reflect the new reverse topological ordering (dependents dropped before dependencies). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request fixes issue #246 where functions that reference other functions through dependencies (e.g., default parameter values) were not being dumped in the correct order, causing errors when the schema was applied.
Changes:
- Added function-to-function dependency tracking via PostgreSQL's
pg_dependsystem catalog - Implemented topological sorting for function creation and drop operations to respect dependencies
- Added test coverage for the function dependency ordering scenario
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
ir/queries/queries.sql |
Added GetFunctionDependencies query to fetch function-to-function dependencies from pg_depend |
ir/queries/queries.sql.go |
Generated code from the new SQL query |
ir/ir.go |
Added Dependencies field to Function struct to store function dependency keys |
ir/inspector.go |
Added buildFunctionDependencies function to populate function dependencies from the database |
internal/diff/topological.go |
Implemented topologicallySortFunctions using Kahn's algorithm for dependency ordering |
internal/diff/function.go |
Modified create/drop function generation to use topological sort instead of alphabetical sort |
cmd/plan/plan.go |
Added normalization of function dependencies during schema name replacement |
testdata/diff/dependency/function_to_function/* |
New test case demonstrating the fix for the reported issue |
testdata/diff/create_function/drop_function/* |
Updated test expectations reflecting new drop order (reverse topological) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Addresses PR review feedback - adds documentation consistent with topologicallySortTables and topologicallySortTypes explaining why cycle breaking is safe for mutually recursive functions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* chore: test case * feat(ir): add GetFunctionDependencies query for pg_depend lookup Add a new sqlc query to retrieve function-to-function dependencies from PostgreSQL's pg_depend system catalog. This query identifies functions that depend on other functions (deptype='n' for normal dependencies), which is essential for topologically sorting functions so that dependencies are created before dependents. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(ir): add Dependencies field to Function struct * feat(ir): populate function dependencies from pg_depend Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(ir): use pg_get_function_identity_arguments for normalized type names Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(diff): add topologicallySortFunctions for dependency ordering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(diff): use topological sort for function ordering Replace alphabetical sorting with topological sorting in both generateCreateFunctionsSQL and generateDropFunctionsSQL. This ensures functions are ordered so dependencies come before dependents. Also fixes function dependency normalization in plan.go - the Dependencies field on ir.Function now gets normalized when replacing temporary schema names with target schema names, enabling proper dependency lookup during topological sorting. Updates test case to use SQL-standard function with DEFAULT parameter that references another function, which PostgreSQL tracks via pg_depend and is idempotent. Fixes pgplex#246 - functions that reference other functions are now ordered correctly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test: update expected output for topological function ordering Updates expected output files for drop_function test case to reflect the new reverse topological ordering (dependents dropped before dependencies). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs: add detailed cycle-breaking comment to topologicallySortFunctions Addresses PR review feedback - adds documentation consistent with topologicallySortTables and topologicallySortTypes explaining why cycle breaking is safe for mutually recursive functions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Fix #246