Skip to content

Conversation

@tianzhou
Copy link
Contributor

@tianzhou tianzhou commented Dec 3, 2025

Fix #185

Copilot AI review requested due to automatic review settings December 3, 2025 06:57
Copy link
Contributor

Copilot AI left a 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 viewDependsOnTable function 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.

Comment on lines +361 to +363
// Check for unqualified table name
if strings.Contains(def, tableNameLower) {
return true
Copy link

Copilot AI Dec 3, 2025

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.

Copilot uses AI. Check for mistakes.
for _, diff := range diffs {
// Handle materialized views that require recreation (DROP + CREATE)
if diff.RequiresRecreate {
viewKey := diff.New.Schema + "." + diff.New.Name
Copy link

Copilot AI Dec 3, 2025

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.

Suggested change
viewKey := diff.New.Schema + "." + diff.New.Name
viewKey := diff.Old.Schema + "." + diff.Old.Name

Copilot uses AI. Check for mistakes.
@tianzhou tianzhou merged commit 1de3510 into main Dec 3, 2025
8 checks passed
@tianzhou tianzhou deleted the order branch December 5, 2025 15:44
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.

Renaming table used in materialized view generates DDL in wrong order.

1 participant