chore: migration improvments

This commit is contained in:
kolaente 2023-09-01 16:32:09 +02:00
parent 9fa905e220
commit 5866016ebc
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B

View File

@ -24,12 +24,14 @@ import (
)
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:"-"`
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"`
IsDoneBucket bool `xorm:"BOOL" json:"is_done_bucket"`
Position float64 `xorm:"double null" json:"position"`
Created time.Time `xorm:"created not null" json:"created"`
Updated time.Time `xorm:"updated not null" json:"updated"`
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
}
func (buckets20230824132533) TableName() string {
@ -62,13 +64,10 @@ func init() {
Description: "",
Migrate: func(tx *xorm.Engine) (err error) {
projects := []*project20230824132533{}
err = tx.Find(&projects)
if err != nil {
return
}
tasks := []*task20230824132533{}
err = tx.Find(&tasks)
err = tx.
Join("LEFT", "buckets", "buckets.project_id = projects.id").
Where("buckets.id is null").
Find(&projects)
if err != nil {
return
}
@ -78,9 +77,8 @@ func init() {
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
ProjectID: project.ID,
Title: "Backlog",
CreatedByID: project.OwnerID,
}
@ -89,16 +87,13 @@ func init() {
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
}
// We can put all tasks from that project in the new bucket because we know
// it is the only bucket in the project
_, err = tx.Where("project_id = ?", project.ID).
Cols("bucket_id").
Update(&task20230824132533{BucketID: buckets[project.ID].ID})
if err != nil {
return
}
}