Skip to content

Commit

Permalink
sql/internal/sqlx: indirect drop trigger dropping (#2409)
Browse files Browse the repository at this point in the history
Triggers are not explicitly dropped as they are dropped together with the table they attach to
  • Loading branch information
a8m authored Dec 28, 2023
1 parent 3496364 commit b650570
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sql/internal/sqlx/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,14 @@ func depOfDrop(o schema.Object, c schema.Change) bool {
switch c := c.(type) {
case *schema.DropTable:
deps = c.T.Deps
for _, t := range c.T.Triggers {
deps = append(deps, t.Deps...)
}
case *schema.DropView:
deps = c.V.Deps
for _, t := range c.V.Triggers {
deps = append(deps, t.Deps...)
}
case *schema.DropFunc:
deps = c.F.Deps
case *schema.DropProc:
Expand Down
15 changes: 15 additions & 0 deletions sql/internal/sqlx/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,19 @@ func TestSortChanges(t *testing.T) {
}
planned = SortChanges(changes)
require.Equal(t, []schema.Change{changes[2], changes[1], changes[0]}, planned)

// No changes.
planned = SortChanges([]schema.Change{
&schema.DropFunc{F: f1},
&schema.DropTable{T: t1},
})
require.Equal(t, []schema.Change{&schema.DropFunc{F: f1}, &schema.DropTable{T: t1}}, planned)

// The table must be dropped before the function if one of its triggers depends on the function.
t1.Triggers = []*schema.Trigger{tr1}
planned = SortChanges([]schema.Change{
&schema.DropFunc{F: f1},
&schema.DropTable{T: t1},
})
require.Equal(t, []schema.Change{&schema.DropTable{T: t1}, &schema.DropFunc{F: f1}}, planned)
}

0 comments on commit b650570

Please sign in to comment.