mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-02 13:48:43 +00:00
Fix pipeline error migration (#2735)
Co-authored-by: Patrick Schratz <patrick.schratz@gmail.com>
This commit is contained in:
parent
cc252b4ac9
commit
731d4713c0
1 changed files with 28 additions and 29 deletions
|
@ -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")
|
||||||
|
|
Loading…
Reference in a new issue