From 8bb3f8d37c78dc704ff4316c750e143528151b48 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 12 Dec 2021 13:18:01 +0100 Subject: [PATCH] fix: importing archived lists or namespaces --- .../migration/create_from_structure.go | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/pkg/modules/migration/create_from_structure.go b/pkg/modules/migration/create_from_structure.go index 8be8304cf..7905e80fa 100644 --- a/pkg/modules/migration/create_from_structure.go +++ b/pkg/modules/migration/create_from_structure.go @@ -51,13 +51,29 @@ func insertFromStructure(s *xorm.Session, str []*models.NamespaceWithListsAndTas labels := make(map[string]*models.Label) + archivedLists := []int64{} + archivedNamespaces := []int64{} + // Create all namespaces for _, n := range str { + n.ID = 0 + + // Saving the archived status to archive the namespace again after creating it + var wasArchived bool + if n.IsArchived { + n.IsArchived = false + wasArchived = true + } + err = n.Create(s, user) if err != nil { return } + if wasArchived { + archivedNamespaces = append(archivedNamespaces, n.ID) + } + log.Debugf("[creating structure] Created namespace %d", n.ID) log.Debugf("[creating structure] Creating %d lists", len(n.Lists)) @@ -70,12 +86,24 @@ func insertFromStructure(s *xorm.Session, str []*models.NamespaceWithListsAndTas originalBackgroundInformation := l.BackgroundInformation needsDefaultBucket := false + // Saving the archived status to archive the list again after creating it + var wasArchived bool + if l.IsArchived { + wasArchived = true + l.IsArchived = false + } + l.NamespaceID = n.ID + l.ID = 0 err = l.Create(s, user) if err != nil { return } + if wasArchived { + archivedLists = append(archivedLists, l.ID) + } + log.Debugf("[creating structure] Created list %d", l.ID) backgroundFile, is := originalBackgroundInformation.(*bytes.Buffer) @@ -216,7 +244,7 @@ func insertFromStructure(s *xorm.Session, str []*models.NamespaceWithListsAndTas TaskID: t.ID, } err = lt.Create(s, user) - if err != nil { + if err != nil && !models.IsErrLabelIsAlreadyOnTask(err) { return err } log.Debugf("[creating structure] Associated task %d with label %d", t.ID, lb.ID) @@ -251,6 +279,26 @@ func insertFromStructure(s *xorm.Session, str []*models.NamespaceWithListsAndTas } } + if len(archivedLists) > 0 { + _, err = s. + Cols("is_archived"). + In("id", archivedLists). + Update(&models.List{IsArchived: true}) + if err != nil { + return err + } + } + + if len(archivedNamespaces) > 0 { + _, err = s. + Cols("is_archived"). + In("id", archivedNamespaces). + Update(&models.Namespace{IsArchived: true}) + if err != nil { + return err + } + } + log.Debugf("[creating structure] Done inserting new task structure") return nil