From 4bc5df440cee2cd1b68127b98e42fee965a23632 Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" <12751435+giautm@users.noreply.github.com> Date: Tue, 26 Nov 2024 21:28:30 +0800 Subject: [PATCH] sql/sqlx: handle `AddFunc`/`DropFunc` depends on `ModifyTable` (#3234) --- sql/internal/sqlx/plan.go | 17 +++++++++++++++++ sql/internal/sqlx/sqlx_oss.go | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/sql/internal/sqlx/plan.go b/sql/internal/sqlx/plan.go index b687c3ba0ab..bd87066e3f8 100644 --- a/sql/internal/sqlx/plan.go +++ b/sql/internal/sqlx/plan.go @@ -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 } @@ -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: diff --git a/sql/internal/sqlx/sqlx_oss.go b/sql/internal/sqlx/sqlx_oss.go index 834686f14b8..24785e802dd 100644 --- a/sql/internal/sqlx/sqlx_oss.go +++ b/sql/internal/sqlx/sqlx_oss.go @@ -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. +}