From 0bb6ed98f54108ccdbe0974078bdf6f1b586dc95 Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 9 Jul 2021 21:58:41 +0200 Subject: [PATCH] Migrate existing favorites --- pkg/migration/20210709211508.go | 73 ++++++++++++++++++++++++++++++--- pkg/models/favorites.go | 6 +-- pkg/models/tasks.go | 8 ++-- 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/pkg/migration/20210709211508.go b/pkg/migration/20210709211508.go index bfef937cd..3def4b161 100644 --- a/pkg/migration/20210709211508.go +++ b/pkg/migration/20210709211508.go @@ -21,23 +21,84 @@ import ( "xorm.io/xorm" ) -type taskfavorites20210709211508 struct { +type favorites20210709211508 struct { EntityID int64 `xorm:"bigint not null pk"` UserID int64 `xorm:"bigint not null pk"` - Kind int `xorm:"int not null"` + Kind int `xorm:"int not null pk"` } -func (taskfavorites20210709211508) TableName() string { +func (favorites20210709211508) TableName() string { return "favorites" } +type task20210709211508 struct { + ID int64 `xorm:"bigint autoincr not null unique pk" json:"id" param:"listtask"` + IsFavorite bool `xorm:"default false" json:"is_favorite"` + CreatedByID int64 `xorm:"bigint not null" json:"-"` // ID of the user who put that task on the list +} + +func (task20210709211508) TableName() string { + return "tasks" +} + +type list20210709211508 struct { + ID int64 `xorm:"bigint autoincr not null unique pk" json:"id" param:"listtask"` + IsFavorite bool `xorm:"default false" json:"is_favorite"` + OwnerID int64 `xorm:"bigint not null" json:"-"` // ID of the user who put that task on the list +} + +func (list20210709211508) TableName() string { + return "lists" +} + func init() { migrations = append(migrations, &xormigrate.Migration{ ID: "20210709211508", - Description: "", + Description: "Move favorites to new table", Migrate: func(tx *xorm.Engine) error { - // TODO: migrate all existing favorites - return tx.Sync2(taskfavorites20210709211508{}) + err := tx.Sync2(favorites20210709211508{}) + if err != nil { + return err + } + + // Migrate all existing favorites + tasks := []*task20210709211508{} + err = tx.Where("is_favorite = ?", true).Find(&tasks) + if err != nil { + return err + } + + for _, task := range tasks { + fav := &favorites20210709211508{ + EntityID: task.ID, + UserID: task.CreatedByID, + Kind: 1, + } + _, err = tx.Insert(fav) + if err != nil { + return err + } + } + + lists := []*list20210709211508{} + err = tx.Where("is_favorite = ?", true).Find(&lists) + if err != nil { + return err + } + + for _, list := range lists { + fav := &favorites20210709211508{ + EntityID: list.ID, + UserID: list.OwnerID, + Kind: 2, + } + _, err = tx.Insert(fav) + if err != nil { + return err + } + } + + return nil }, Rollback: func(tx *xorm.Engine) error { return nil diff --git a/pkg/models/favorites.go b/pkg/models/favorites.go index 0c0776d86..7aa093df7 100644 --- a/pkg/models/favorites.go +++ b/pkg/models/favorites.go @@ -27,14 +27,14 @@ type FavoriteKind int const ( FavoriteKindUnknown FavoriteKind = iota - FavoriteKindTask = 1 - FavoriteKindList = 2 + FavoriteKindTask + FavoriteKindList ) type Favorite struct { EntityID int64 `xorm:"bigint not null pk"` UserID int64 `xorm:"bigint not null pk"` - Kind FavoriteKind `xorm:"int not null"` + Kind FavoriteKind `xorm:"int not null pk"` } func (t *Favorite) TableName() string { diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index 057a8ec20..1190e9be1 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -59,8 +59,7 @@ type Task struct { // The time when the task is due. DueDate time.Time `xorm:"DATETIME INDEX null 'due_date'" json:"due_date"` // An array of datetimes when the user wants to be reminded of the task. - Reminders []time.Time `xorm:"-" json:"reminder_dates"` - CreatedByID int64 `xorm:"bigint not null" json:"-"` // ID of the user who put that task on the list + Reminders []time.Time `xorm:"-" json:"reminder_dates"` // The list this task belongs to. ListID int64 `xorm:"bigint INDEX not null" json:"list_id" param:"list"` // An amount in seconds this task repeats itself. If this is set, when marking the task as done, it will mark itself as "undone" and then increase all remindes and the due date by its amount. @@ -96,7 +95,7 @@ type Task struct { // All attachments this task has Attachments []*TaskAttachment `xorm:"-" json:"attachments"` - // True if a task is a favorite task. Favorite tasks show up in a separate "Important" list + // True if a task is a favorite task. Favorite tasks show up in a separate "Important" list. This value depends on the user making the call to the api. IsFavorite bool `xorm:"default false" json:"is_favorite"` // The subscription status for the user reading this task. You can only read this property, use the subscription endpoints to modify it. @@ -120,7 +119,8 @@ type Task struct { Position float64 `xorm:"double null" json:"position"` // The user who initially created the task. - CreatedBy *user.User `xorm:"-" json:"created_by" valid:"-"` + CreatedBy *user.User `xorm:"-" json:"created_by" valid:"-"` + CreatedByID int64 `xorm:"bigint not null" json:"-"` // ID of the user who put that task on the list web.CRUDable `xorm:"-" json:"-"` web.Rights `xorm:"-" json:"-"`