2023-11-03 10:44:03 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package migration
|
|
|
|
|
|
|
|
import (
|
2023-11-28 09:31:54 +00:00
|
|
|
"src.techknowlogick.com/xormigrate"
|
2023-11-03 10:44:03 +00:00
|
|
|
"xorm.io/xorm"
|
2023-11-07 07:04:33 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/errors"
|
2023-11-03 10:44:03 +00:00
|
|
|
)
|
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
// perPage027 set the size of the slice to read per page
|
|
|
|
var perPage027 = 100
|
2023-11-06 12:20:28 +00:00
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
type pipeline027 struct {
|
2023-11-06 12:20:28 +00:00
|
|
|
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
|
2023-11-03 10:44:03 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
func (pipeline027) TableName() string {
|
2023-11-03 10:44:03 +00:00
|
|
|
return "pipelines"
|
|
|
|
}
|
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
type PipelineError027 struct {
|
2023-11-12 17:23:48 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
IsWarning bool `json:"is_warning"`
|
|
|
|
Data any `json:"data"`
|
2023-11-03 10:44:03 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
var convertToNewPipelineErrorFormat = xormigrate.Migration{
|
|
|
|
ID: "convert-to-new-pipeline-error-format",
|
|
|
|
Long: true,
|
|
|
|
MigrateSession: func(sess *xorm.Session) (err error) {
|
2023-11-03 10:44:03 +00:00
|
|
|
// make sure pipeline_error column exists
|
2023-11-28 09:31:54 +00:00
|
|
|
if err := sess.Sync(new(pipeline027)); err != nil {
|
2023-11-03 10:44:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-06 12:20:28 +00:00
|
|
|
page := 0
|
2023-11-28 09:31:54 +00:00
|
|
|
oldPipelines := make([]*pipeline027, 0, perPage027)
|
2023-11-03 10:44:03 +00:00
|
|
|
|
2023-11-06 12:20:28 +00:00
|
|
|
for {
|
|
|
|
oldPipelines = oldPipelines[:0]
|
2023-11-03 10:44:03 +00:00
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
err := sess.Limit(perPage027, page*perPage027).Cols("pipeline_id", "pipeline_error").Where("pipeline_error != ''").Find(&oldPipelines)
|
2023-11-06 12:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-03 10:44:03 +00:00
|
|
|
|
2023-11-06 12:20:28 +00:00
|
|
|
for _, oldPipeline := range oldPipelines {
|
2023-11-28 09:31:54 +00:00
|
|
|
var newPipeline pipeline027
|
2023-11-06 12:20:28 +00:00
|
|
|
newPipeline.ID = oldPipeline.ID
|
2023-11-03 10:44:03 +00:00
|
|
|
newPipeline.Errors = []*errors.PipelineError{{
|
|
|
|
Type: "generic",
|
|
|
|
Message: oldPipeline.Error,
|
|
|
|
}}
|
2023-11-06 12:20:28 +00:00
|
|
|
|
|
|
|
if _, err := sess.ID(oldPipeline.ID).Cols("pipeline_errors").Update(newPipeline); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-03 10:44:03 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 09:31:54 +00:00
|
|
|
if len(oldPipelines) < perPage027 {
|
2023-11-06 12:20:28 +00:00
|
|
|
break
|
2023-11-03 10:44:03 +00:00
|
|
|
}
|
2023-11-06 12:20:28 +00:00
|
|
|
|
|
|
|
page++
|
2023-11-03 10:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dropTableColumns(sess, "pipelines", "pipeline_error")
|
|
|
|
},
|
|
|
|
}
|