Add buckets migration

This commit is contained in:
kolaente 2020-12-16 14:44:59 +01:00
parent 90ae940a6b
commit 1c9eb823c9
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 67 additions and 2 deletions

View File

@ -50,6 +50,9 @@ type List struct {
// Deprecated: you should use the dedicated task list endpoint because it has support for pagination and filtering
Tasks []*Task `xorm:"-" json:"-"`
// Only used for migration.
Buckets []*Bucket `xorm:"-" json:"-"`
// Whether or not a list is archived.
IsArchived bool `xorm:"not null default false" json:"is_archived" query:"is_archived"`

View File

@ -45,9 +45,10 @@ func InsertFromStructure(str []*models.NamespaceWithLists, user *user.User) (err
// Create all lists
for _, l := range n.Lists {
// The tasks slice is going to be reset during the creation of the list so we rescue it here to be able
// to still loop over the tasks aftere the list was created.
// The tasks and bucket slices are going to be reset during the creation of the list so we rescue it here
// to be able to still loop over them aftere the list was created.
tasks := l.Tasks
originalBuckets := l.Buckets
l.NamespaceID = n.ID
err = l.Create(user)
@ -56,10 +57,36 @@ func InsertFromStructure(str []*models.NamespaceWithLists, user *user.User) (err
}
log.Debugf("[creating structure] Created list %d", l.ID)
// Create all buckets
buckets := make(map[int64]*models.Bucket) // old bucket id is the key
if len(l.Buckets) > 0 {
log.Debugf("[creating structure] Creating %d buckets", len(l.Buckets))
}
for _, bucket := range originalBuckets {
oldID := bucket.ID
bucket.ID = 0 // We want a new id
bucket.ListID = l.ID
err = bucket.Create(user)
if err != nil {
return
}
buckets[oldID] = bucket
log.Debugf("[creating structure] Created bucket %d, old ID was %d", bucket.ID, oldID)
}
log.Debugf("[creating structure] Creating %d tasks", len(tasks))
// Create all tasks
for _, t := range tasks {
bucket, exists := buckets[t.BucketID]
if exists {
t.BucketID = bucket.ID
} else if t.BucketID > 0 {
log.Debugf("[creating structure] No bucket created for original bucket id %d", t.BucketID)
t.BucketID = 0
}
t.ListID = l.ID
err = t.Create(user)
if err != nil {
@ -150,6 +177,9 @@ func InsertFromStructure(str []*models.NamespaceWithLists, user *user.User) (err
log.Debugf("[creating structure] Associated task %d with label %d", t.ID, lb.ID)
}
}
l.Tasks = tasks
l.Buckets = originalBuckets
}
}

View File

@ -42,6 +42,12 @@ func TestInsertFromStructure(t *testing.T) {
{
Title: "Testlist1",
Description: "Something",
Buckets: []*models.Bucket{
{
ID: 1234,
Title: "Test Bucket",
},
},
Tasks: []*models.Task{
{
Title: "Task1",
@ -92,6 +98,14 @@ func TestInsertFromStructure(t *testing.T) {
},
},
},
{
Title: "Task in a bucket",
BucketID: 1234,
},
{
Title: "Task in a nonexisting bucket",
BucketID: 1111,
},
},
},
},
@ -99,5 +113,23 @@ func TestInsertFromStructure(t *testing.T) {
}
err := InsertFromStructure(testStructure, u)
assert.NoError(t, err)
db.AssertExists(t, "namespaces", map[string]interface{}{
"title": testStructure[0].Namespace.Title,
"description": testStructure[0].Namespace.Description,
}, false)
db.AssertExists(t, "list", map[string]interface{}{
"title": testStructure[0].Lists[0].Title,
"description": testStructure[0].Lists[0].Description,
}, false)
db.AssertExists(t, "tasks", map[string]interface{}{
"title": testStructure[0].Lists[0].Tasks[5].Title,
"bucket_id": testStructure[0].Lists[0].Buckets[0].ID,
}, false)
db.AssertMissing(t, "tasks", map[string]interface{}{
"title": testStructure[0].Lists[0].Tasks[6].Title,
"bucket_id": 1111, // No task with that bucket should exist
})
assert.NotEqual(t, 0, testStructure[0].Lists[0].Tasks[0].BucketID) // Should get the default bucket
assert.NotEqual(t, 0, testStructure[0].Lists[0].Tasks[6].BucketID) // Should get the default bucket
})
}