You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by wa...@apache.org on 2022/11/10 09:46:25 UTC
[incubator-devlake] branch release-v0.14 updated: fix(migrationscript): change column instead of table when the table has auto (#3712)
This is an automated email from the ASF dual-hosted git repository.
warren pushed a commit to branch release-v0.14
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
The following commit(s) were added to refs/heads/release-v0.14 by this push:
new 3da56b5d fix(migrationscript): change column instead of table when the table has auto (#3712)
3da56b5d is described below
commit 3da56b5d3437ae99eeb2126fb0026de837c8db72
Author: Warren Chen <yi...@merico.dev>
AuthorDate: Thu Nov 10 17:46:20 2022 +0800
fix(migrationscript): change column instead of table when the table has auto (#3712)
---
.../migrationscripts/20220903_encrypt_blueprint.go | 85 +++++++++++-----------
.../migrationscripts/20220904_encrypt_pipeline.go | 73 +++++++------------
.../20220916_modify_jenkins_build.go | 26 +++----
3 files changed, 79 insertions(+), 105 deletions(-)
diff --git a/models/migrationscripts/20220903_encrypt_blueprint.go b/models/migrationscripts/20220903_encrypt_blueprint.go
index 0031526f..0115aa39 100644
--- a/models/migrationscripts/20220903_encrypt_blueprint.go
+++ b/models/migrationscripts/20220903_encrypt_blueprint.go
@@ -27,52 +27,54 @@ import (
"gorm.io/gorm"
)
+type Blueprint0903Before struct {
+ NewPlan string `json:"plan"`
+ NewSettings string `json:"settings"`
+}
+
+func (Blueprint0903Before) TableName() string {
+ return "_devlake_blueprints"
+}
+
type Blueprint0903Temp struct {
- Name string `json:"name" validate:"required"`
- Mode string `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
- Enable bool `json:"enable"`
+ NewPlan string `json:"plan"`
+ NewSettings string `json:"settings"`
+ Name string `json:"name" validate:"required"`
+ Mode string `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
+ Plan json.RawMessage
+ Enable bool `json:"enable"`
//please check this https://crontab.guru/ for detail
CronConfig string `json:"cronConfig" format:"* * * * *" example:"0 0 * * 1"`
IsManual bool `json:"isManual"`
+ Settings json.RawMessage
archived.Model `swaggerignore:"true"`
- Plan string `json:"plan"`
- Settings string `json:"settings"`
}
func (Blueprint0903Temp) TableName() string {
- return "_devlake_blueprints_tmp"
+ return "_devlake_blueprints"
}
-type BlueprintOldVersion struct {
- Name string `json:"name" validate:"required"`
- Mode string `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
- Plan json.RawMessage `json:"plan"`
- Enable bool `json:"enable"`
- //please check this https://crontab.guru/ for detail
- CronConfig string `json:"cronConfig" format:"* * * * *" example:"0 0 * * 1"`
- IsManual bool `json:"isManual"`
- Settings json.RawMessage `json:"settings" swaggertype:"array,string" example:"please check api: /blueprints/<PLUGIN_NAME>/blueprint-setting"`
- archived.Model `swaggerignore:"true"`
+type Blueprint0903TempAfter struct {
+ Plan string
+ Settings string
+ NewPlan string
+ NewSettings string
}
-func (BlueprintOldVersion) TableName() string {
+func (Blueprint0903TempAfter) TableName() string {
return "_devlake_blueprints"
}
type encryptBLueprint struct{}
func (*encryptBLueprint) Up(ctx context.Context, db *gorm.DB) errors.Error {
- err := db.Migrator().CreateTable(&Blueprint0903Temp{})
+ err := db.AutoMigrate(&Blueprint0903Before{})
if err != nil {
return errors.Convert(err)
}
- //nolint:errcheck
- defer db.Migrator().DropTable(&Blueprint0903Temp{})
-
var result *gorm.DB
- var blueprintList []BlueprintOldVersion
+ var blueprintList []Blueprint0903Temp
result = db.Find(&blueprintList)
-
if result.Error != nil {
return errors.Convert(result.Error)
}
@@ -84,40 +86,37 @@ func (*encryptBLueprint) Up(ctx context.Context, db *gorm.DB) errors.Error {
if encKey == "" {
return errors.BadInput.New("invalid encKey")
}
- encryptedPlan, err := core.Encrypt(encKey, string(v.Plan))
+ v.NewPlan, err = core.Encrypt(encKey, string(v.Plan))
if err != nil {
- return err
+ return errors.Convert(err)
}
- encryptedSettings, err := core.Encrypt(encKey, string(v.Settings))
+ v.NewSettings, err = core.Encrypt(encKey, string(v.Settings))
if err != nil {
- return err
+ return errors.Convert(err)
}
- newBlueprint := &Blueprint0903Temp{
- Name: v.Name,
- Mode: v.Mode,
- Enable: v.Enable,
- CronConfig: v.CronConfig,
- IsManual: v.IsManual,
- Model: archived.Model{ID: v.ID},
- Plan: encryptedPlan,
- Settings: encryptedSettings,
- }
- err = errors.Convert(db.Create(newBlueprint).Error)
+ err = errors.Convert(db.Save(&v).Error)
if err != nil {
- return err
+ return errors.Convert(err)
}
}
-
- err = db.Migrator().DropTable(&BlueprintOldVersion{})
+ err = db.Migrator().DropColumn(&Blueprint0903Temp{}, "plan")
if err != nil {
return errors.Convert(err)
}
- err = db.Migrator().RenameTable(Blueprint0903Temp{}, BlueprintOldVersion{})
+ err = db.Migrator().DropColumn(&Blueprint0903Temp{}, "settings")
if err != nil {
return errors.Convert(err)
}
-
+ err = db.Migrator().RenameColumn(&Blueprint0903TempAfter{}, "new_plan", "plan")
+ if err != nil {
+ return errors.Convert(err)
+ }
+ err = db.Migrator().RenameColumn(&Blueprint0903TempAfter{}, "new_settings", "settings")
+ if err != nil {
+ return errors.Convert(err)
+ }
+ _ = db.Find(&blueprintList)
return nil
}
diff --git a/models/migrationscripts/20220904_encrypt_pipeline.go b/models/migrationscripts/20220904_encrypt_pipeline.go
index b2e9e26e..6b77a8be 100644
--- a/models/migrationscripts/20220904_encrypt_pipeline.go
+++ b/models/migrationscripts/20220904_encrypt_pipeline.go
@@ -19,22 +19,29 @@ package migrationscripts
import (
"context"
+ "github.com/apache/incubator-devlake/config"
+ "github.com/apache/incubator-devlake/plugins/core"
"time"
- "github.com/apache/incubator-devlake/config"
"github.com/apache/incubator-devlake/errors"
"github.com/apache/incubator-devlake/models/common"
- "github.com/apache/incubator-devlake/models/migrationscripts/archived"
- "github.com/apache/incubator-devlake/plugins/core"
"gorm.io/datatypes"
"gorm.io/gorm"
)
+type Pipeline0904TempBefore struct {
+ NewPlan string
+}
+
+func (Pipeline0904TempBefore) TableName() string {
+ return "_devlake_pipelines"
+}
+
type Pipeline0904Temp struct {
common.Model
Name string `json:"name" gorm:"index"`
BlueprintId uint64 `json:"blueprintId"`
- Plan string `json:"plan" encrypt:"yes"`
+ NewPlan string `json:"plan" encrypt:"yes"`
TotalTasks int `json:"totalTasks"`
FinishedTasks int `json:"finishedTasks"`
BeganAt *time.Time `json:"beganAt"`
@@ -43,88 +50,64 @@ type Pipeline0904Temp struct {
Message string `json:"message"`
SpentSeconds int `json:"spentSeconds"`
Stage int `json:"stage"`
+ Plan datatypes.JSON
}
func (Pipeline0904Temp) TableName() string {
- return "_devlake_pipeline_0904_tmp"
+ return "_devlake_pipelines"
}
-type PipelineOldVersion struct {
- archived.Model
- Name string `json:"name" gorm:"index"`
- BlueprintId uint64 `json:"blueprintId"`
- Plan datatypes.JSON `json:"plan"`
- TotalTasks int `json:"totalTasks"`
- FinishedTasks int `json:"finishedTasks"`
- BeganAt *time.Time `json:"beganAt"`
- FinishedAt *time.Time `json:"finishedAt" gorm:"index"`
- Status string `json:"status"`
- Message string `json:"message"`
- SpentSeconds int `json:"spentSeconds"`
- Stage int `json:"stage"`
+type Pipeline0904TempAfter struct {
+ NewPlan string
+ Plan string
}
-func (PipelineOldVersion) TableName() string {
+func (Pipeline0904TempAfter) TableName() string {
return "_devlake_pipelines"
}
type encryptPipeline struct{}
func (*encryptPipeline) Up(ctx context.Context, db *gorm.DB) errors.Error {
- err := db.Migrator().CreateTable(&Pipeline0904Temp{})
+ err := db.AutoMigrate(&Pipeline0904TempBefore{})
if err != nil {
return errors.Convert(err)
}
- //nolint:errcheck
- defer db.Migrator().DropTable(&Pipeline0904Temp{})
-
var result *gorm.DB
- var pipelineList []PipelineOldVersion
+ var pipelineList []Pipeline0904Temp
result = db.Find(&pipelineList)
if result.Error != nil {
return errors.Convert(result.Error)
}
- // Encrypt all pipelines.plan which had been stored before v0.14
+ // Encrypt all pipelines.plan&settings which had been stored before v0.14
for _, v := range pipelineList {
c := config.GetConfig()
encKey := c.GetString(core.EncodeKeyEnvStr)
if encKey == "" {
return errors.BadInput.New("invalid encKey")
}
- encryptedPlan, err := core.Encrypt(encKey, string(v.Plan))
+ v.NewPlan, err = core.Encrypt(encKey, string(v.Plan))
if err != nil {
- return err
- }
- newPipeline := &Pipeline0904Temp{
- Name: v.Name,
- BlueprintId: v.BlueprintId,
- FinishedTasks: v.FinishedTasks,
- BeganAt: v.BeganAt,
- FinishedAt: v.FinishedAt,
- Status: v.Status,
- Message: v.Message,
- SpentSeconds: v.SpentSeconds,
- Stage: v.Stage,
- Plan: encryptedPlan,
+ return errors.Convert(err)
}
- err = errors.Convert(db.Create(newPipeline).Error)
+
+ err = errors.Convert(db.Save(&v).Error)
if err != nil {
- return err
+ return errors.Convert(err)
}
}
- err = db.Migrator().DropTable(&PipelineOldVersion{})
+ err = db.Migrator().DropColumn(&Pipeline0904Temp{}, "plan")
if err != nil {
return errors.Convert(err)
}
-
- err = db.Migrator().RenameTable(Pipeline0904Temp{}, PipelineOldVersion{})
+ err = db.Migrator().RenameColumn(&Pipeline0904TempAfter{}, "new_plan", "plan")
if err != nil {
return errors.Convert(err)
}
-
+ _ = db.Find(&pipelineList)
return nil
}
diff --git a/plugins/jenkins/models/migrationscripts/20220916_modify_jenkins_build.go b/plugins/jenkins/models/migrationscripts/20220916_modify_jenkins_build.go
index b912ad18..396fc2b6 100644
--- a/plugins/jenkins/models/migrationscripts/20220916_modify_jenkins_build.go
+++ b/plugins/jenkins/models/migrationscripts/20220916_modify_jenkins_build.go
@@ -20,14 +20,11 @@ package migrationscripts
import (
"context"
"fmt"
- "reflect"
"strings"
"time"
"github.com/apache/incubator-devlake/errors"
"github.com/apache/incubator-devlake/models/migrationscripts/archived"
- "github.com/apache/incubator-devlake/plugins/helper"
- "github.com/apache/incubator-devlake/plugins/jenkins/api"
"gorm.io/gorm"
)
@@ -77,7 +74,7 @@ type JenkinsBuil0916 struct {
}
func (JenkinsBuil0916) TableName() string {
- return "_tool_jenkins_builds"
+ return "_tool_jenkins_builds_new"
}
type JenkinsBuildRepo0916 struct {
@@ -92,23 +89,14 @@ func (*modifyJenkinsBuild) Up(ctx context.Context, db *gorm.DB) errors.Error {
if err != nil {
return errors.Convert(err)
}
- err = db.Migrator().RenameTable(&JenkinsBuildOld{}, "_tool_jenkins_builds_old")
- if err != nil {
- return errors.Default.Wrap(err, "fail to rename _tool_jenkins_builds")
- }
err = db.Migrator().RenameTable(&JenkinsBuildRepo0916{}, "_tool_jenkins_build_commits")
if err != nil {
- return errors.Default.Wrap(err, "fail to rename _tool_jenkins_builds")
+ return errors.Default.Wrap(err, "fail to rename _tool_jenkins_build_commits")
}
err = db.Migrator().AutoMigrate(&JenkinsBuil0916{})
if err != nil {
- return errors.Default.Wrap(err, "fail to create _tool_jenkins_builds")
- }
- batch, err := helper.NewBatchSave(api.BasicRes, reflect.TypeOf(&JenkinsBuil0916{}), 300)
- if err != nil {
- return errors.Default.Wrap(err, "error getting batch from table")
+ return errors.Default.Wrap(err, "fail to create _tool_jenkins_builds_new")
}
- defer batch.Close()
for cursor.Next() {
build := JenkinsBuildOld{}
err = db.ScanRows(cursor, &build)
@@ -137,15 +125,19 @@ func (*modifyJenkinsBuild) Up(ctx context.Context, db *gorm.DB) errors.Error {
} else {
newBuild.FullDisplayName = fmt.Sprintf("%s %s", build.JobName, build.DisplayName)
}
- err = batch.Add(newBuild)
+ err = db.Save(newBuild).Error
if err != nil {
return errors.Convert(err)
}
}
+ err = db.Migrator().DropTable(&JenkinsBuildOld{})
+ if err != nil {
+ return errors.Convert(err)
+ }
+ err = db.Migrator().RenameTable(&JenkinsBuil0916{}, "_tool_jenkins_builds")
if err != nil {
return errors.Convert(err)
}
-
return nil
}