From 0599955676c920355406f7791c232efac3c12bf4 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Wed, 3 Jan 2024 04:29:30 +0200 Subject: [PATCH] sort cascadeDelete refs for deterministic tests output --- daos/record.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/daos/record.go b/daos/record.go index 9b6b55df..f16fe03f 100644 --- a/daos/record.go +++ b/daos/record.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "fmt" + "sort" "strings" "github.com/pocketbase/dbx" @@ -660,9 +661,23 @@ func (dao *Dao) DeleteRecord(record *models.Record) error { func (dao *Dao) cascadeRecordDelete(mainRecord *models.Record, refs map[*models.Collection][]*schema.SchemaField) error { uniqueJsonEachAlias := "__je__" + security.PseudorandomString(4) - for refCollection, fields := range refs { - if refCollection.IsView() { - continue // skip view collections + // @todo consider changing refs to a slice + // + // Sort the refs keys to ensure that the cascade events firing order is always the same. + // This is not necessary for the operation to function correctly but it helps having deterministic output during testing. + sortedRefKeys := make([]*models.Collection, 0, len(refs)) + for k := range refs { + sortedRefKeys = append(sortedRefKeys, k) + } + sort.Slice(sortedRefKeys, func(i, j int) bool { + return sortedRefKeys[i].Name < sortedRefKeys[j].Name + }) + + for _, refCollection := range sortedRefKeys { + fields, ok := refs[refCollection] + + if refCollection.IsView() || !ok { + continue // skip missing or view collections } for _, field := range fields {