forked from vikunja/vikunja
fix(kanban): Created migration to create a default bucket for projects that do not already have any buckets
This commit is contained in:
parent
ebca8abd30
commit
9fa905e220
|
@ -17,23 +17,92 @@
|
|||
package migration
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"src.techknowlogick.com/xormigrate"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type buckets20230824132533 struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"bucket"`
|
||||
Title string `xorm:"text not null" valid:"required" minLength:"1" json:"title"`
|
||||
ProjectID int64 `xorm:"int(11) not null" json:"project_id" param:"project"`
|
||||
Created time.Time `xorm:"created not null" json:"created"`
|
||||
Updated int64 `xorm:"updated not null" json:"updated"`
|
||||
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
|
||||
}
|
||||
|
||||
func (buckets20230824132533) TableName() string {
|
||||
return "buckets"
|
||||
}
|
||||
|
||||
type project20230824132533 struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"project"`
|
||||
OwnerID int64 `xorm:"int(11) INDEX not null" json:"-"`
|
||||
}
|
||||
|
||||
func (project *project20230824132533) TableName() string {
|
||||
return "projects"
|
||||
}
|
||||
|
||||
type task20230824132533 struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"projecttask"`
|
||||
ProjectID int64 `xorm:"int(11) INDEX not null" json:"project_id" param:"project"`
|
||||
Updated time.Time `xorm:"updated not null" json:"updated"`
|
||||
BucketID int64 `xorm:"int(11) null" json:"bucket_id"`
|
||||
}
|
||||
|
||||
func (task *task20230824132533) TableName() string {
|
||||
return "tasks"
|
||||
}
|
||||
|
||||
func init() {
|
||||
migrations = append(migrations, &xormigrate.Migration{
|
||||
ID: "20230824132533",
|
||||
Description: "",
|
||||
Migrate: func(tx *xorm.Engine) error {
|
||||
return tx.Sync2(buckets20230824132533{})
|
||||
Migrate: func(tx *xorm.Engine) (err error) {
|
||||
projects := []*project20230824132533{}
|
||||
err = tx.Find(&projects)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tasks := []*task20230824132533{}
|
||||
err = tx.Find(&tasks)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// This map contains all buckets with their project ids as key
|
||||
buckets := make(map[int64]*buckets20230824132533, len(projects))
|
||||
for _, project := range projects {
|
||||
|
||||
buckets[project.ID] = &buckets20230824132533{
|
||||
ProjectID: project.ID,
|
||||
Title: "Backlog",
|
||||
// The bucket creator is just the same as the project's one
|
||||
CreatedByID: project.OwnerID,
|
||||
}
|
||||
|
||||
_, err = tx.Insert(buckets[project.ID])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, task := range tasks {
|
||||
if task.ProjectID != project.ID {
|
||||
continue
|
||||
}
|
||||
|
||||
task.BucketID = buckets[project.ID].ID
|
||||
_, err = tx.Where("id = ?", task.ID).Update(task)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
},
|
||||
Rollback: func(tx *xorm.Engine) error {
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue
Block a user