fix(migration): enable insert from structure work recursively
This commit is contained in:
parent
4b55e2ce03
commit
abe5f72493
@ -50,74 +50,107 @@ func insertFromStructure(s *xorm.Session, str []*models.ProjectWithTasksAndBucke
|
|||||||
log.Debugf("[creating structure] Creating %d projects", len(str))
|
log.Debugf("[creating structure] Creating %d projects", len(str))
|
||||||
|
|
||||||
labels := make(map[string]*models.Label)
|
labels := make(map[string]*models.Label)
|
||||||
|
|
||||||
archivedProjects := []int64{}
|
archivedProjects := []int64{}
|
||||||
|
|
||||||
// Create all projects
|
// Create all projects
|
||||||
for _, p := range str {
|
for _, p := range str {
|
||||||
p.ID = 0
|
p.ID = 0
|
||||||
|
err = createProjectWithChildren(s, p, 0, &archivedProjects, labels, user)
|
||||||
err = p.Create(s, user)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("[creating structure] Created project %d", p.ID)
|
if len(archivedProjects) > 0 {
|
||||||
log.Debugf("[creating structure] Creating %d projects", len(p.ChildProjects))
|
_, err = s.
|
||||||
|
Cols("is_archived").
|
||||||
|
In("id", archivedProjects).
|
||||||
|
Update(&models.Project{IsArchived: true})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("[creating structure] Done inserting new task structure")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createProjectWithChildren(s *xorm.Session, project *models.ProjectWithTasksAndBuckets, parentProjectID int64, archivedProjectIDs *[]int64, labels map[string]*models.Label, user *user.User) (err error) {
|
||||||
|
err = createProjectWithEverything(s, project, parentProjectID, archivedProjectIDs, labels, user)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("[creating structure] Created project %d", project.ID)
|
||||||
|
|
||||||
|
if len(project.ChildProjects) > 0 {
|
||||||
|
log.Debugf("[creating structure] Creating %d projects", len(project.ChildProjects))
|
||||||
|
|
||||||
// Create all projects
|
// Create all projects
|
||||||
for _, l := range p.ChildProjects {
|
for _, cp := range project.ChildProjects {
|
||||||
// The tasks and bucket slices are going to be reset during the creation of the project so we rescue it here
|
err = createProjectWithChildren(s, cp, project.ID, archivedProjectIDs, labels, user)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func createProjectWithEverything(s *xorm.Session, project *models.ProjectWithTasksAndBuckets, parentProjectID int64, archivedProjects *[]int64, labels map[string]*models.Label, user *user.User) (err error) {
|
||||||
|
// The tasks and bucket slices are going to be reset during the creation of the project, so we rescue it here
|
||||||
// to be able to still loop over them aftere the project was created.
|
// to be able to still loop over them aftere the project was created.
|
||||||
tasks := l.Tasks
|
tasks := project.Tasks
|
||||||
originalBuckets := l.Buckets
|
originalBuckets := project.Buckets
|
||||||
originalBackgroundInformation := l.BackgroundInformation
|
originalBackgroundInformation := project.BackgroundInformation
|
||||||
needsDefaultBucket := false
|
needsDefaultBucket := false
|
||||||
|
|
||||||
// Saving the archived status to archive the project again after creating it
|
// Saving the archived status to archive the project again after creating it
|
||||||
var wasArchived bool
|
var wasArchived bool
|
||||||
if l.IsArchived {
|
if project.IsArchived {
|
||||||
wasArchived = true
|
wasArchived = true
|
||||||
l.IsArchived = false
|
project.IsArchived = false
|
||||||
}
|
}
|
||||||
|
|
||||||
l.ParentProjectID = p.ID
|
project.ParentProjectID = parentProjectID
|
||||||
l.ID = 0
|
project.ID = 0
|
||||||
err = l.Create(s, user)
|
err = project.Create(s, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if wasArchived {
|
if wasArchived {
|
||||||
archivedProjects = append(archivedProjects, l.ID)
|
*archivedProjects = append(*archivedProjects, project.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("[creating structure] Created project %d", l.ID)
|
log.Debugf("[creating structure] Created project %d", project.ID)
|
||||||
|
|
||||||
bf, is := originalBackgroundInformation.(*bytes.Buffer)
|
bf, is := originalBackgroundInformation.(*bytes.Buffer)
|
||||||
if is {
|
if is {
|
||||||
|
|
||||||
backgroundFile := bytes.NewReader(bf.Bytes())
|
backgroundFile := bytes.NewReader(bf.Bytes())
|
||||||
|
|
||||||
log.Debugf("[creating structure] Creating a background file for project %d", l.ID)
|
log.Debugf("[creating structure] Creating a background file for project %d", project.ID)
|
||||||
|
|
||||||
err = handler.SaveBackgroundFile(s, user, &l.Project, backgroundFile, "", uint64(backgroundFile.Len()))
|
err = handler.SaveBackgroundFile(s, user, &project.Project, backgroundFile, "", uint64(backgroundFile.Len()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("[creating structure] Created a background file for project %d", l.ID)
|
log.Debugf("[creating structure] Created a background file for project %d", project.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create all buckets
|
// Create all buckets
|
||||||
buckets := make(map[int64]*models.Bucket) // old bucket id is the key
|
buckets := make(map[int64]*models.Bucket) // old bucket id is the key
|
||||||
if len(l.Buckets) > 0 {
|
if len(project.Buckets) > 0 {
|
||||||
log.Debugf("[creating structure] Creating %d buckets", len(l.Buckets))
|
log.Debugf("[creating structure] Creating %d buckets", len(project.Buckets))
|
||||||
}
|
}
|
||||||
for _, bucket := range originalBuckets {
|
for _, bucket := range originalBuckets {
|
||||||
oldID := bucket.ID
|
oldID := bucket.ID
|
||||||
bucket.ID = 0 // We want a new id
|
bucket.ID = 0 // We want a new id
|
||||||
bucket.ProjectID = l.ID
|
bucket.ProjectID = project.ID
|
||||||
err = bucket.Create(s, user)
|
err = bucket.Create(s, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -145,7 +178,7 @@ func insertFromStructure(s *xorm.Session, str []*models.ProjectWithTasksAndBucke
|
|||||||
for _, t := range tasks {
|
for _, t := range tasks {
|
||||||
setBucketOrDefault(&t.Task)
|
setBucketOrDefault(&t.Task)
|
||||||
|
|
||||||
t.ProjectID = l.ID
|
t.ProjectID = project.ID
|
||||||
err = t.Create(s, user)
|
err = t.Create(s, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -216,8 +249,7 @@ func insertFromStructure(s *xorm.Session, str []*models.ProjectWithTasksAndBucke
|
|||||||
var exists bool
|
var exists bool
|
||||||
if label == nil {
|
if label == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}lb, exists = labels[label.Title+label.HexColor]
|
||||||
lb, exists = labels[label.Title+label.HexColor]
|
|
||||||
if !exists {
|
if !exists {
|
||||||
err = label.Create(s, user)
|
err = label.Create(s, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -241,6 +273,7 @@ func insertFromStructure(s *xorm.Session, str []*models.ProjectWithTasksAndBucke
|
|||||||
|
|
||||||
for _, comment := range t.Comments {
|
for _, comment := range t.Comments {
|
||||||
comment.TaskID = t.ID
|
comment.TaskID = t.ID
|
||||||
|
comment.ID = 0
|
||||||
err = comment.Create(s, user)
|
err = comment.Create(s, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -251,34 +284,27 @@ func insertFromStructure(s *xorm.Session, str []*models.ProjectWithTasksAndBucke
|
|||||||
|
|
||||||
// All tasks brought their own bucket with them, therefore the newly created default bucket is just extra space
|
// All tasks brought their own bucket with them, therefore the newly created default bucket is just extra space
|
||||||
if !needsDefaultBucket {
|
if !needsDefaultBucket {
|
||||||
b := &models.Bucket{ProjectID: l.ID}
|
b := &models.Bucket{ProjectID: project.ID}
|
||||||
bucketsIn, _, _, err := b.ReadAll(s, user, "", 1, 1)
|
bucketsIn, _, _, err := b.ReadAll(s, user, "", 1, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buckets := bucketsIn.([]*models.Bucket)
|
buckets := bucketsIn.([]*models.Bucket)
|
||||||
err = buckets[0].Delete(s, user)
|
var newBacklogBucket *models.Bucket
|
||||||
|
for _, b := range buckets {
|
||||||
|
if b.Title == "Backlog" {
|
||||||
|
newBacklogBucket = b
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = newBacklogBucket.Delete(s, user)
|
||||||
if err != nil && !models.IsErrCannotRemoveLastBucket(err) {
|
if err != nil && !models.IsErrCannotRemoveLastBucket(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Tasks = tasks
|
project.Tasks = tasks
|
||||||
l.Buckets = originalBuckets
|
project.Buckets = originalBuckets
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(archivedProjects) > 0 {
|
|
||||||
_, err = s.
|
|
||||||
Cols("is_archived").
|
|
||||||
In("id", archivedProjects).
|
|
||||||
Update(&models.Project{IsArchived: true})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("[creating structure] Done inserting new task structure")
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,13 @@ func TestInsertFromStructure(t *testing.T) {
|
|||||||
Title: "Test1",
|
Title: "Test1",
|
||||||
Description: "Lorem Ipsum",
|
Description: "Lorem Ipsum",
|
||||||
},
|
},
|
||||||
|
Tasks: []*models.TaskWithComments{
|
||||||
|
{
|
||||||
|
Task: models.Task{
|
||||||
|
Title: "Task on parent",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
ChildProjects: []*models.ProjectWithTasksAndBuckets{
|
ChildProjects: []*models.ProjectWithTasksAndBuckets{
|
||||||
{
|
{
|
||||||
Project: models.Project{
|
Project: models.Project{
|
||||||
@ -141,6 +148,9 @@ func TestInsertFromStructure(t *testing.T) {
|
|||||||
"title": testStructure[0].ChildProjects[0].Tasks[6].Title,
|
"title": testStructure[0].ChildProjects[0].Tasks[6].Title,
|
||||||
"bucket_id": 1111, // No task with that bucket should exist
|
"bucket_id": 1111, // No task with that bucket should exist
|
||||||
})
|
})
|
||||||
|
db.AssertExists(t, "tasks", map[string]interface{}{
|
||||||
|
"title": testStructure[0].Tasks[0].Title,
|
||||||
|
}, false)
|
||||||
assert.NotEqual(t, 0, testStructure[0].ChildProjects[0].Tasks[0].BucketID) // Should get the default bucket
|
assert.NotEqual(t, 0, testStructure[0].ChildProjects[0].Tasks[0].BucketID) // Should get the default bucket
|
||||||
assert.NotEqual(t, 0, testStructure[0].ChildProjects[0].Tasks[6].BucketID) // Should get the default bucket
|
assert.NotEqual(t, 0, testStructure[0].ChildProjects[0].Tasks[6].BucketID) // Should get the default bucket
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user