From 731d4713c00ee13d4608cf1fdf6e97394d97c74d Mon Sep 17 00:00:00 2001 From: Anbraten Date: Mon, 6 Nov 2023 13:20:28 +0100 Subject: [PATCH] Fix pipeline error migration (#2735) Co-authored-by: Patrick Schratz --- ...6_convert_to_new_pipeline_errors_format.go | 57 +++++++++---------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/server/store/datastore/migration/026_convert_to_new_pipeline_errors_format.go b/server/store/datastore/migration/026_convert_to_new_pipeline_errors_format.go index 8a4bd687a..b18b00ab9 100644 --- a/server/store/datastore/migration/026_convert_to_new_pipeline_errors_format.go +++ b/server/store/datastore/migration/026_convert_to_new_pipeline_errors_format.go @@ -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")