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

zhangliang2022 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 5722a751 feat: modify refs_commits_diffs table name and struct (#3703)
5722a751 is described below

commit 5722a751eca93bbc6eb409eba829dead836d844c
Author: abeizn <[email protected]>
AuthorDate: Thu Nov 10 20:49:40 2022 +0800

    feat: modify refs_commits_diffs table name and struct (#3703)
    
    * feat: modifity refs-commits-diffs name and struct
    
    * feat: modifity refs-commits-diffs name and struct
    
    * feat: modifity refs-commits-diffs name and struct
    
    * feat: modifity refs-commits-diffs name and struct
    
    * feat: modifity refs-commits-diffs name and struct
    
    * feat: modifity refs-commits-diffs name and struct
    
    * feat: modifity refs-commits-diffs name and struct
    
    * feat: modifity refs-commits-diffs name and struct
---
 helpers/migrationhelper/migrationhelper.go         |  39 ++++++++
 .../20221109_modfiy_commits_diffs.go               | 103 +++++++++++++++++++++
 models/migrationscripts/register.go                |   1 +
 3 files changed, 143 insertions(+)

diff --git a/helpers/migrationhelper/migrationhelper.go 
b/helpers/migrationhelper/migrationhelper.go
index 8870a74e..5bd6a394 100644
--- a/helpers/migrationhelper/migrationhelper.go
+++ b/helpers/migrationhelper/migrationhelper.go
@@ -261,6 +261,45 @@ func TransformTable[S any, D any](
        return err
 }
 
+// CopyTableColumn can copy data from src table to dst table
+func CopyTableColumn[S any, D any](
+       basicRes core.BasicRes,
+       srcTableName string,
+       dstTableName string,
+       transform func(*S) (*D, errors.Error),
+) (err errors.Error) {
+       db := basicRes.GetDal()
+       cursor, err := db.Cursor(dal.From(srcTableName))
+       if err != nil {
+               return errors.Default.Wrap(err, fmt.Sprintf("failed to load 
data from src table [%s]", srcTableName))
+       }
+       defer cursor.Close()
+       batch, err := helper.NewBatchSave(basicRes, reflect.TypeOf(new(D)), 
200, dstTableName)
+       if err != nil {
+               return errors.Default.Wrap(err, fmt.Sprintf("failed to 
instantiate BatchSave for table [%s]", srcTableName))
+       }
+       defer batch.Close()
+
+       srcTable := new(S)
+       for cursor.Next() {
+               err = db.Fetch(cursor, srcTable)
+               if err != nil {
+                       return errors.Default.Wrap(err, fmt.Sprintf("fail to 
load record from table [%s]", srcTableName))
+               }
+
+               dst, err := transform(srcTable)
+               if err != nil {
+                       return errors.Default.Wrap(err, fmt.Sprintf("failed to 
transform row %v", srcTable))
+               }
+               err = batch.Add(dst)
+               if err != nil {
+                       return errors.Default.Wrap(err, fmt.Sprintf("push to 
BatchSave failed %v", dstTableName))
+               }
+       }
+
+       return err
+}
+
 func hashScript(script core.MigrationScript) string {
        hasher := md5.New()
        _, err := hasher.Write([]byte(fmt.Sprintf("%s:%v", script.Name(), 
script.Version())))
diff --git a/models/migrationscripts/20221109_modfiy_commits_diffs.go 
b/models/migrationscripts/20221109_modfiy_commits_diffs.go
new file mode 100644
index 00000000..e412ebe8
--- /dev/null
+++ b/models/migrationscripts/20221109_modfiy_commits_diffs.go
@@ -0,0 +1,103 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You 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 migrationscripts
+
+import (
+       "github.com/apache/incubator-devlake/errors"
+       "github.com/apache/incubator-devlake/helpers/migrationhelper"
+       "github.com/apache/incubator-devlake/plugins/core"
+)
+
+var _ core.MigrationScript = (*modifyCommitsDiffs)(nil)
+
+type modifyCommitsDiffs struct{}
+
+type FinishedCommitsDiffs20221109 struct {
+       NewCommitSha string `gorm:"primaryKey;type:varchar(40)"`
+       OldCommitSha string `gorm:"primaryKey;type:varchar(40)"`
+}
+
+func (FinishedCommitsDiffs20221109) TableName() string {
+       return "finished_commits_diffs"
+}
+
+type RefsCommitsDiff20221109 struct {
+       NewRefId        string `gorm:"primaryKey;type:varchar(255)"`
+       OldRefId        string `gorm:"primaryKey;type:varchar(255)"`
+       CommitSha       string `gorm:"primaryKey;type:varchar(40)"`
+       NewRefCommitSha string `gorm:"type:varchar(40)"`
+       OldRefCommitSha string `gorm:"type:varchar(40)"`
+       SortingIndex    int
+}
+
+func (RefsCommitsDiff20221109) TableName() string {
+       return "refs_commits_diffs"
+}
+
+type CommitsDiff20221109 struct {
+       CommitSha    string `gorm:"primaryKey;type:varchar(40)"`
+       NewCommitSha string `gorm:"primaryKey;type:varchar(40)"`
+       OldCommitSha string `gorm:"primaryKey;type:varchar(40)"`
+       SortingIndex int
+}
+
+func (CommitsDiff20221109) TableName() string {
+       return "commits_diffs"
+}
+
+func (script *modifyCommitsDiffs) Up(basicRes core.BasicRes) errors.Error {
+       db := basicRes.GetDal()
+       // create table
+       err := db.AutoMigrate(&CommitsDiff20221109{})
+       if err != nil {
+               return err
+       }
+
+       err = db.AutoMigrate(&FinishedCommitsDiffs20221109{})
+       if err != nil {
+               return err
+       }
+
+       // copy data
+       err = migrationhelper.CopyTableColumn(
+               basicRes,
+               RefsCommitsDiff20221109{}.TableName(),
+               CommitsDiff20221109{}.TableName(),
+               func(s *RefsCommitsDiff20221109) (*CommitsDiff20221109, 
errors.Error) {
+                       dst := CommitsDiff20221109{}
+                       dst.CommitSha = s.CommitSha
+                       dst.NewCommitSha = s.NewRefCommitSha
+                       dst.OldCommitSha = s.OldRefCommitSha
+
+                       return &dst, nil
+               },
+       )
+       if err != nil {
+               return err
+       }
+
+       return db.DropTables(&RefsCommitsDiff20221109{})
+}
+
+func (*modifyCommitsDiffs) Version() uint64 {
+       return 20221109232735
+}
+
+func (*modifyCommitsDiffs) Name() string {
+       return "modify commits diffs"
+}
diff --git a/models/migrationscripts/register.go 
b/models/migrationscripts/register.go
index 8f12061e..4144b2d3 100644
--- a/models/migrationscripts/register.go
+++ b/models/migrationscripts/register.go
@@ -54,5 +54,6 @@ func All() []core.MigrationScript {
                new(createCollectorState),
                new(removeCicdPipelineRelation),
                new(addCicdScope),
+               new(modifyCommitsDiffs),
        }
 }

Reply via email to