Migrate existing favorites

This commit is contained in:
kolaente 2021-07-09 21:58:41 +02:00
parent a8c6141a2c
commit 0bb6ed98f5
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 74 additions and 13 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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:"-"`