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