fix: delete folders using postorder (#113493)

* fix: delete folders using postorder

* chore: use helper function and do not add method to Folder store

- addresses other review comments fixing log messages and cleans up the unit tests

* chore: run library element tests on modes 2,3,5 only

* chore: adjust to folder.SortByPostorder(folders []*Folder)

* chore: run library panels tests in mode 2,3,5 only

* chore: run tests in all modes and increase timeout

- adjusting the modes and tweaking configs will be done separately
This commit is contained in:
Rafael Bortolon Paulovic
2025-11-06 15:04:34 +01:00
committed by GitHub
parent fbf1cdd0ce
commit e69f3c55f7
8 changed files with 314 additions and 8 deletions
+47
View File
@@ -278,3 +278,50 @@ type GetDescendantCountsQuery struct {
}
type DescendantCounts map[string]int64
// SortByPostorder returns the folders in postorder traversal order.
// That is, children folders appear before their parents in the returned slice.
func SortByPostorder(folders []*Folder) []*Folder {
if len(folders) == 0 {
return folders
}
// Build parent-to-children map
tree := make(map[string][]*Folder)
folderMap := make(map[string]*Folder)
for _, f := range folders {
folderMap[f.UID] = f
tree[f.ParentUID] = append(tree[f.ParentUID], f)
}
// Find all roots (folders whose parents are not in the result set)
var roots []*Folder
for _, f := range folders {
if folderMap[f.ParentUID] == nil {
roots = append(roots, f)
}
}
// Traverse in postorder
result := make([]*Folder, 0, len(folders))
visited := make(map[string]bool)
var traverse func(f *Folder)
traverse = func(f *Folder) {
if visited[f.UID] {
return
}
visited[f.UID] = true
// First visit all children
for _, child := range tree[f.UID] {
traverse(child)
}
// Then add current folder
result = append(result, f)
}
for _, root := range roots {
traverse(root)
}
return result
}