Add kanban position task property and migrate existing

This commit is contained in:
kolaente 2021-07-25 15:48:17 +02:00
parent d9b38b85f6
commit c8d47e90cd
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 82 additions and 3 deletions

View File

@ -0,0 +1,70 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package migration
import (
"math"
"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"
)
type tasks20210725153703 struct {
ID int64 `xorm:"bigint autoincr not null unique pk" json:"id" param:"listtask"`
Position float64 `xorm:"double null" json:"position"`
KanbanPosition float64 `xorm:"double null" json:"kanban_position"`
}
func (tasks20210725153703) TableName() string {
return "tasks"
}
func init() {
migrations = append(migrations, &xormigrate.Migration{
ID: "20210725153703",
Description: "",
Migrate: func(tx *xorm.Engine) error {
err := tx.Sync2(tasks20210725153703{})
if err != nil {
return err
}
tasks := []*tasks20210725153703{}
err = tx.Where("position is not null").Find(&tasks)
if err != nil {
return err
}
// Migrate all old kanban positions to the kanban positions property
for _, task := range tasks {
task.KanbanPosition = task.Position
task.Position = float64(task.ID) * math.Pow(2, 16)
_, err = tx.
Where("id = ?", task.ID).
Cols("kanban_position", "position").
Update(task)
if err != nil {
return err
}
}
return nil
},
Rollback: func(tx *xorm.Engine) error {
return nil
},
})
}

View File

@ -118,6 +118,8 @@ type Task struct {
// A 64-Bit float leaves plenty of room to initially give tasks a position with 2^16 difference to the previous task
// which also leaves a lot of room for rearranging and sorting later.
Position float64 `xorm:"double null" json:"position"`
// The position of tasks in the kanban board. See the docs for the `position` property on how to use this.
KanbanPosition float64 `xorm:"double null" json:"kanban_position"`
// The user who initially created the task.
CreatedBy *user.User `xorm:"-" json:"created_by" valid:"-"`
@ -836,6 +838,14 @@ func setTaskBucket(s *xorm.Session, task *Task, originalTask *Task, doCheckBucke
return nil
}
func calculateDefaultTaskPosition(t *Task, position float64) float64 {
if position == 0 {
return float64(t.ID+1) * math.Pow(2, 16)
}
return position
}
// Create is the implementation to create a list task
// @Summary Create a task
// @Description Inserts a task into a list.
@ -895,9 +905,8 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err
t.Index = latestTask.Index + 1
// If no position was supplied, set a default one
if t.Position == 0 {
t.Position = float64(latestTask.ID+1) * math.Pow(2, 16)
}
t.Position = calculateDefaultTaskPosition(latestTask, t.Position)
t.KanbanPosition = calculateDefaultTaskPosition(latestTask, t.KanbanPosition)
if _, err = s.Insert(t); err != nil {
return err
}