Fix pipeline error migration (#2735)

Co-authored-by: Patrick Schratz <patrick.schratz@gmail.com>
This commit is contained in:
Anbraten 2023-11-06 13:20:28 +01:00 committed by GitHub
parent cc252b4ac9
commit 731d4713c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,16 +16,19 @@ package migration
import ( import (
"github.com/woodpecker-ci/woodpecker/pipeline/errors" "github.com/woodpecker-ci/woodpecker/pipeline/errors"
"github.com/woodpecker-ci/woodpecker/server/model"
"xorm.io/xorm" "xorm.io/xorm"
) )
type oldPipeline026 struct { // perPage026 set the size of the slice to read per page
ID int64 `json:"id" xorm:"pk autoincr 'pipeline_id'"` var perPage026 = 100
Error string `json:"error" xorm:"LONGTEXT 'pipeline_error'"`
type pipeline026 struct {
ID int64 `json:"id" xorm:"pk autoincr 'pipeline_id'"`
Error string `json:"error" xorm:"LONGTEXT 'pipeline_error'"` // old error format
Errors []*errors.PipelineError `json:"errors" xorm:"json 'pipeline_errors'"` // new error format
} }
func (oldPipeline026) TableName() string { func (pipeline026) TableName() string {
return "pipelines" return "pipelines"
} }
@ -36,48 +39,44 @@ type PipelineError026 struct {
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
type newPipeline026 struct {
ID int64 `json:"id" xorm:"pk autoincr 'pipeline_id'"`
Errors []*errors.PipelineError `json:"errors" xorm:"json 'pipeline_errors'"`
}
func (newPipeline026) TableName() string {
return "pipelines"
}
var convertToNewPipelineErrorFormat = task{ var convertToNewPipelineErrorFormat = task{
name: "convert-to-new-pipeline-error-format", name: "convert-to-new-pipeline-error-format",
required: true, required: true,
fn: func(sess *xorm.Session) (err error) { fn: func(sess *xorm.Session) (err error) {
// make sure pipeline_error column exists // make sure pipeline_error column exists
if err := sess.Sync(new(oldPipeline026)); err != nil { if err := sess.Sync(new(pipeline026)); err != nil {
return err return err
} }
// add new pipeline_errors column page := 0
if err := sess.Sync(new(model.Pipeline)); err != nil { oldPipelines := make([]*pipeline026, 0, perPage026)
return err
}
var oldPipelines []*oldPipeline026 for {
if err := sess.Find(&oldPipelines); err != nil { oldPipelines = oldPipelines[:0]
return err
}
for _, oldPipeline := range oldPipelines { err := sess.Limit(perPage026, page*perPage026).Cols("pipeline_id", "pipeline_error").Where("pipeline_error != ''").Find(&oldPipelines)
if err != nil {
return err
}
var newPipeline newPipeline026 for _, oldPipeline := range oldPipelines {
newPipeline.ID = oldPipeline.ID var newPipeline pipeline026
if oldPipeline.Error != "" { newPipeline.ID = oldPipeline.ID
newPipeline.Errors = []*errors.PipelineError{{ newPipeline.Errors = []*errors.PipelineError{{
Type: "generic", Type: "generic",
Message: oldPipeline.Error, Message: oldPipeline.Error,
}} }}
if _, err := sess.ID(oldPipeline.ID).Cols("pipeline_errors").Update(newPipeline); err != nil {
return err
}
} }
if _, err := sess.ID(oldPipeline.ID).Cols("pipeline_errors").Update(&newPipeline); err != nil { if len(oldPipelines) < perPage026 {
return err break
} }
page++
} }
return dropTableColumns(sess, "pipelines", "pipeline_error") return dropTableColumns(sess, "pipelines", "pipeline_error")