Compare commits

...

2 Commits

1 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,111 @@
// 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 (
"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) (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
},
})
}