This is an automated email from the ASF dual-hosted git repository.

mappjzc pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 9dd7046a fix: fix migrator bug (#3135)
9dd7046a is described below

commit 9dd7046ae5082ad45a9db7b27eff42c29dd07e76
Author: mappjzc <[email protected]>
AuthorDate: Wed Sep 21 16:49:53 2022 +0800

    fix: fix migrator bug (#3135)
    
    * fix: fix migrator bug
    
    Add Re Executability to addCommitFilePathLength.
    Fix a type error on modifyJenkinsBuild.
    Add error message on migrator execute.
    
    Nddtfjiang <[email protected]>
    
    * refactor: ignorerecovery for errorscripts
    
    Ignore providing recovery for error execution scripts.
    Use defer to rollback the fail db request.
    
    Nddtfjiang <[email protected]>
---
 migration/migrator.go                              |  7 +--
 .../20220913_commitfile_add_length.go              | 59 ++++++++++++++++++++--
 .../20220916_modify_jenkins_build.go               |  9 ++--
 3 files changed, 63 insertions(+), 12 deletions(-)

diff --git a/migration/migrator.go b/migration/migrator.go
index 72434a6d..ca899298 100644
--- a/migration/migrator.go
+++ b/migration/migrator.go
@@ -20,10 +20,11 @@ package migration
 import (
        "context"
        "fmt"
-       "github.com/apache/incubator-devlake/errors"
        "sort"
        "sync"
 
+       "github.com/apache/incubator-devlake/errors"
+
        "gorm.io/gorm"
 )
 
@@ -82,11 +83,11 @@ func (m *migrator) execute(ctx context.Context) 
errors.Error {
        for _, script := range m.pending {
                err := script.Up(ctx, m.db)
                if err != nil {
-                       return err
+                       return errors.Default.Wrap(err, fmt.Sprintf("error Up() 
on script [%s]", script.Name()))
                }
                err = m.bookKeep(script)
                if err != nil {
-                       return err
+                       return errors.Default.Wrap(err, fmt.Sprintf("error 
bookKeep() on script [%s]", script.Name()))
                }
        }
        return nil
diff --git a/models/migrationscripts/20220913_commitfile_add_length.go 
b/models/migrationscripts/20220913_commitfile_add_length.go
index 9b5a5cb0..a65937d4 100644
--- a/models/migrationscripts/20220913_commitfile_add_length.go
+++ b/models/migrationscripts/20220913_commitfile_add_length.go
@@ -21,6 +21,7 @@ import (
        "context"
        "crypto/sha256"
        "encoding/hex"
+       "fmt"
        "reflect"
        "strings"
 
@@ -67,17 +68,41 @@ func (CommitFileComponentBak) TableName() string {
 
 type addCommitFilePathLength struct{}
 
-func (*addCommitFilePathLength) Up(ctx context.Context, db *gorm.DB) 
errors.Error {
-       err := db.Migrator().RenameTable(&CommitFile{}, 
&CommitFileAddLengthBak{})
+func (*addCommitFilePathLength) Up(ctx context.Context, db *gorm.DB) (errs 
errors.Error) {
+       var err error
+
+       // rename the commit_file_bak to cache old table
+       err = db.Migrator().RenameTable(&CommitFile{}, 
&CommitFileAddLengthBak{})
        if err != nil {
                return errors.Default.Wrap(err, "error no rename commit_file to 
commit_files_bak")
        }
 
+       // rollback for rename back
+       defer func() {
+               if errs != nil {
+                       err = 
db.Migrator().RenameTable(&CommitFileAddLengthBak{}, &CommitFile{})
+                       if err != nil {
+                               errs = errors.Default.Wrap(err, 
fmt.Sprintf("fail to rollback table commit_file_bak , you must to rollback by 
yourself. %s", err.Error()))
+                       }
+               }
+       }()
+
+       // create new commit_files table
        err = db.Migrator().AutoMigrate(&CommitFileAddLength{})
        if err != nil {
                return errors.Default.Wrap(err, "error on auto migrate 
commit_file")
        }
 
+       // rollback for create new table
+       defer func() {
+               if errs != nil {
+                       err = db.Migrator().DropTable(&CommitFile{})
+                       if err != nil {
+                               errs = errors.Default.Wrap(err, 
fmt.Sprintf("fail to rollback table CommitFile , you must to rollback by 
yourself. %s", err.Error()))
+                       }
+               }
+       }()
+
        // update old id to new id and write to the new table
        cursor, err := db.Model(&CommitFileAddLengthBak{}).Rows()
        if err != nil {
@@ -85,6 +110,7 @@ func (*addCommitFilePathLength) Up(ctx context.Context, db 
*gorm.DB) errors.Erro
        }
        defer cursor.Close()
 
+       // caculate and save the data to new table
        batch, err := helper.NewBatchSave(api.BasicRes, 
reflect.TypeOf(&CommitFileAddLength{}), 200)
        if err != nil {
                return errors.Default.Wrap(err, "error getting batch from table 
commit_file")
@@ -108,20 +134,42 @@ func (*addCommitFilePathLength) Up(ctx context.Context, 
db *gorm.DB) errors.Erro
 
                err = batch.Add(&cf)
                if err != nil {
-                       return errors.Default.Wrap(err, "error on batch add")
+                       return errors.Default.Wrap(err, "error on commit_files 
batch add")
                }
        }
 
+       // rename the commit_file_components_bak
        err = db.Migrator().RenameTable(&CommitFileComponent{}, 
&CommitFileComponentBak{})
        if err != nil {
                return errors.Default.Wrap(err, "error no rename 
commit_file_components to commit_file_components_bak")
        }
 
+       // rollback for rename back
+       defer func() {
+               if errs != nil {
+                       err = 
db.Migrator().RenameTable(&CommitFileComponentBak{}, &CommitFileComponent{})
+                       if err != nil {
+                               errs = errors.Default.Wrap(err, 
fmt.Sprintf("fail to rollback table commit_file_components_bak , you must to 
rollback by yourself. %s", err.Error()))
+                       }
+               }
+       }()
+
+       // create new commit_file_components table
        err = db.Migrator().AutoMigrate(&CommitFileComponent{})
        if err != nil {
                return errors.Default.Wrap(err, "error on auto migrate 
commit_file")
        }
 
+       // rollback for create new table
+       defer func() {
+               if errs != nil {
+                       err = db.Migrator().DropTable(&CommitFileComponent{})
+                       if err != nil {
+                               errs = errors.Default.Wrap(err, 
fmt.Sprintf("fail to rollback table commit_file_components , you must to 
rollback by yourself. %s", err.Error()))
+                       }
+               }
+       }()
+
        // update old id to new id and write to the new table
        cursor2, err := db.Model(&CommitFileComponentBak{}).Rows()
        if err != nil {
@@ -129,6 +177,7 @@ func (*addCommitFilePathLength) Up(ctx context.Context, db 
*gorm.DB) errors.Erro
        }
        defer cursor2.Close()
 
+       // caculate and save the data to new table
        batch2, err := helper.NewBatchSave(api.BasicRes, 
reflect.TypeOf(&CommitFileComponent{}), 500)
        if err != nil {
                return errors.Default.Wrap(err, "error getting batch from table 
commit_file_components")
@@ -169,7 +218,7 @@ func (*addCommitFilePathLength) Up(ctx context.Context, db 
*gorm.DB) errors.Erro
 
                err = batch2.Add(&cfc)
                if err != nil {
-                       return errors.Default.Wrap(err, "error on batch add")
+                       return errors.Default.Wrap(err, "error on 
commit_file_components batch add")
                }
        }
 
@@ -180,7 +229,7 @@ func (*addCommitFilePathLength) Up(ctx context.Context, db 
*gorm.DB) errors.Erro
        }
        err = db.Migrator().DropTable(&CommitFileComponentBak{})
        if err != nil {
-               return errors.Default.Wrap(err, "error no drop 
commit_files_bak")
+               return errors.Default.Wrap(err, "error no drop 
commit_file_components_bak")
        }
 
        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 94294332..b912ad18 100644
--- a/plugins/jenkins/models/migrationscripts/20220916_modify_jenkins_build.go
+++ b/plugins/jenkins/models/migrationscripts/20220916_modify_jenkins_build.go
@@ -20,14 +20,15 @@ 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"
-       "reflect"
-       "strings"
-       "time"
 )
 
 type modifyJenkinsBuild struct{}
@@ -136,7 +137,7 @@ 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 = batch.Add(newBuild)
                if err != nil {
                        return errors.Convert(err)
                }

Reply via email to