-
Notifications
You must be signed in to change notification settings - Fork 29
fix: materialized view drop order that depends on table #188
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
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 PR fixes the ordering of materialized view drops when those views depend on tables being modified or dropped. The fix ensures that materialized views are dropped before their dependent tables are altered, preventing PostgreSQL dependency errors.
Key changes:
- Added pre-drop phase for materialized views that depend on affected tables
- Modified the migration SQL generation flow to handle pre-dropped views
- Introduced
viewDependsOnTablefunction to detect table dependencies in view definitions
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
internal/diff/diff.go |
Implements pre-drop logic for materialized views and updates migration generation flow to track pre-dropped views |
internal/diff/view.go |
Updates generateModifyViewsSQL to skip DROP for pre-dropped views and adds viewDependsOnTable helper function |
testdata/diff/dependency/table_to_view/plan.txt |
Test data showing expected plan output for table-to-view dependency scenario |
testdata/diff/dependency/table_to_view/plan.sql |
Test data with expected SQL for table-to-view dependency scenario |
testdata/diff/dependency/table_to_view/plan.json |
Test data with JSON plan for table-to-view dependency scenario |
testdata/diff/dependency/table_to_view/old.sql |
Test data with old schema state for table-to-view scenario |
testdata/diff/dependency/table_to_view/new.sql |
Test data with new schema state for table-to-view scenario |
testdata/diff/dependency/table_to_view/diff.sql |
Test data with expected diff SQL for table-to-view scenario |
testdata/diff/dependency/table_to_materialized_view/plan.txt |
Test data showing expected plan output with pre-drop of materialized view |
testdata/diff/dependency/table_to_materialized_view/plan.sql |
Test data with expected SQL showing DROP before ALTER TABLE |
testdata/diff/dependency/table_to_materialized_view/plan.json |
Test data with JSON plan showing proper operation ordering |
testdata/diff/dependency/table_to_materialized_view/old.sql |
Test data with old schema state including materialized view |
testdata/diff/dependency/table_to_materialized_view/new.sql |
Test data with new schema state with modified table and view |
testdata/diff/dependency/table_to_materialized_view/diff.sql |
Test data with expected diff SQL showing proper drop order |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Check for unqualified table name | ||
| if strings.Contains(def, tableNameLower) { | ||
| return true |
Copilot
AI
Dec 3, 2025
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.
The strings.Contains check for unqualified table names can produce false positives. For example, if tableName is "product", it will incorrectly match "products" or "product_items" in the view definition. Consider using word boundary matching (e.g., with regex \b) or a more sophisticated SQL parsing approach to avoid matching partial table names.
| for _, diff := range diffs { | ||
| // Handle materialized views that require recreation (DROP + CREATE) | ||
| if diff.RequiresRecreate { | ||
| viewKey := diff.New.Schema + "." + diff.New.Name |
Copilot
AI
Dec 3, 2025
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.
The view key is constructed using diff.New.Schema + "." + diff.New.Name, but in generatePreDropMaterializedViewsSQL (line 952 of diff.go), the key is constructed using viewDiff.Old.Schema + "." + viewDiff.Old.Name. This inconsistency could cause the pre-dropped view check to fail if the view is renamed or moved to a different schema. Consider using the Old view's schema and name for both key construction and lookup to ensure consistency.
| viewKey := diff.New.Schema + "." + diff.New.Name | |
| viewKey := diff.Old.Schema + "." + diff.Old.Name |
Fix #185