Skip to content

Commit

Permalink
sql/sqlx: handle AddFunc/DropFunc depends on ModifyTable (#3234)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Nov 26, 2024
1 parent ec43618 commit 4bc5df4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions sql/internal/sqlx/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,15 @@ func dependsOn(c1, c2 schema.Change, opts SortOptions) bool {
}
return depOfAdd(c1.To.Deps, c2)
case *schema.AddFunc:
if c2, ok := c2.(*schema.ModifyTable); ok {
// Check if the modification of a table relies on the function.
if slices.ContainsFunc(c2.Changes, func(c schema.Change) bool {
add, ok := c.(*schema.AddCheck)
return ok && ContainsCall(&schema.Func{Schema: c2.T.Schema, Body: add.C.Expr}, c1.F, opts)
}) {
return false
}
}
if depOfAdd(c1.F.Deps, c2) {
return true
}
Expand Down Expand Up @@ -709,6 +718,14 @@ func dependsOn(c1, c2 schema.Change, opts SortOptions) bool {
// If f1 depends on previous definition of f2, f1 should be dropped before f2.
return true
}
case *schema.ModifyTable:
// We need to drop the check constraint before dropping the function.
if slices.ContainsFunc(c2.Changes, func(c schema.Change) bool {
drop, ok := c.(*schema.DropCheck)
return ok && ContainsCall(&schema.Func{Schema: c2.T.Schema, Body: drop.C.Expr}, c1.F, opts)
}) {
return true
}
}
return depOfDrop(c1.F, c2)
case *schema.ModifyFunc:
Expand Down
5 changes: 5 additions & 0 deletions sql/internal/sqlx/sqlx_oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ func (*Diff) askForIndexes(_ string, changes []schema.Change, _ *schema.DiffOpti
func (*Diff) fixRenames(changes schema.Changes) schema.Changes {
return changes // unimplemented.
}

// ContainsCall determines if first function calls the second function.
func ContainsCall(_, _ *schema.Func, _ SortOptions) bool {
return false // unimplemented.
}

0 comments on commit 4bc5df4

Please sign in to comment.