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

klesh 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 8c5449567 refactor: refactor gitee migration scripts (#3506)
8c5449567 is described below

commit 8c5449567c8595385ebd232613a92b44539ca8a6
Author: mappjzc <[email protected]>
AuthorDate: Fri Oct 21 10:15:04 2022 +0800

    refactor: refactor gitee migration scripts (#3506)
    
    Refactor Gitee MigrationScripts
    
    Nddtfjiang <[email protected]>
---
 plugins/gitee/impl/impl.go                         |  6 +-
 .../migrationscripts/20220714_add_init_tables.go   | 69 ++++++++++------------
 plugins/gitee/models/migrationscripts/register.go  |  6 +-
 3 files changed, 37 insertions(+), 44 deletions(-)

diff --git a/plugins/gitee/impl/impl.go b/plugins/gitee/impl/impl.go
index cc5fbc0a6..834a65d7a 100644
--- a/plugins/gitee/impl/impl.go
+++ b/plugins/gitee/impl/impl.go
@@ -19,9 +19,9 @@ package impl
 
 import (
        "fmt"
+
        "github.com/apache/incubator-devlake/errors"
 
-       "github.com/apache/incubator-devlake/migration"
        "github.com/apache/incubator-devlake/plugins/core"
        "github.com/apache/incubator-devlake/plugins/gitee/api"
        "github.com/apache/incubator-devlake/plugins/gitee/models"
@@ -36,7 +36,7 @@ var _ core.PluginMeta = (*Gitee)(nil)
 var _ core.PluginInit = (*Gitee)(nil)
 var _ core.PluginTask = (*Gitee)(nil)
 var _ core.PluginApi = (*Gitee)(nil)
-var _ core.Migratable = (*Gitee)(nil)
+var _ core.PluginMigration = (*Gitee)(nil)
 var _ core.CloseablePluginTask = (*Gitee)(nil)
 
 type Gitee string
@@ -188,7 +188,7 @@ func (plugin Gitee) RootPkgPath() string {
        return "github.com/apache/incubator-devlake/plugins/gitee"
 }
 
-func (plugin Gitee) MigrationScripts() []migration.Script {
+func (plugin Gitee) MigrationScripts() []core.MigrationScript {
        return migrationscripts.All()
 }
 
diff --git a/plugins/gitee/models/migrationscripts/20220714_add_init_tables.go 
b/plugins/gitee/models/migrationscripts/20220714_add_init_tables.go
index 1564b6060..440063279 100644
--- a/plugins/gitee/models/migrationscripts/20220714_add_init_tables.go
+++ b/plugins/gitee/models/migrationscripts/20220714_add_init_tables.go
@@ -18,41 +18,21 @@ limitations under the License.
 package migrationscripts
 
 import (
-       "context"
-       "fmt"
-       "github.com/apache/incubator-devlake/errors"
+       "strconv"
 
-       "github.com/apache/incubator-devlake/config"
-       "gorm.io/gorm/clause"
+       "github.com/apache/incubator-devlake/errors"
+       "github.com/apache/incubator-devlake/helpers/migrationhelper"
 
        "github.com/apache/incubator-devlake/plugins/core"
 
        
"github.com/apache/incubator-devlake/plugins/gitee/models/migrationscripts/archived"
-       "gorm.io/gorm"
 )
 
 type addInitTables struct{}
 
-func (*addInitTables) Up(ctx context.Context, db *gorm.DB) errors.Error {
-       rawTableList := []string{
-               "_raw_gitee_api_commit",
-               "_raw_gitee_api_issues",
-               "_raw_gitee_api_pull_requests",
-               "_raw_gitee_api_pull_request_commits",
-               "_raw_gitee_api_pull_request_reviews",
-               "_raw_gitee_api_repo",
-               "_raw_gitee_api_comments",
-               "_raw_gitee_api_commits",
-               "_raw_gitee_issue_comments",
-       }
-       for _, v := range rawTableList {
-               err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s CASCADE", 
v)).Error
-               if err != nil {
-                       return errors.Convert(err)
-               }
-       }
-
-       err := db.Migrator().DropTable(
+func (*addInitTables) Up(baseRes core.BasicRes) errors.Error {
+       db := baseRes.GetDal()
+       err := db.DropTables(
                &archived.GiteeRepo{},
                &archived.GiteeCommit{},
                &archived.GiteeRepoCommit{},
@@ -68,13 +48,23 @@ func (*addInitTables) Up(ctx context.Context, db *gorm.DB) 
errors.Error {
                &archived.GiteeReviewer{},
                &archived.GiteeConnection{},
                "_tool_gitee_users",
+               "_raw_gitee_api_commit",
+               "_raw_gitee_api_issues",
+               "_raw_gitee_api_pull_requests",
+               "_raw_gitee_api_pull_request_commits",
+               "_raw_gitee_api_pull_request_reviews",
+               "_raw_gitee_api_repo",
+               "_raw_gitee_api_comments",
+               "_raw_gitee_api_commits",
+               "_raw_gitee_issue_comments",
        )
 
        if err != nil {
-               return errors.Convert(err)
+               return err
        }
 
-       err = db.Migrator().AutoMigrate(
+       err = migrationhelper.AutoMigrateTables(
+               baseRes,
                &archived.GiteeRepo{},
                &archived.GiteeCommit{},
                &archived.GiteeRepoCommit{},
@@ -93,27 +83,30 @@ func (*addInitTables) Up(ctx context.Context, db *gorm.DB) 
errors.Error {
        )
 
        if err != nil {
-               return errors.Convert(err)
+               return err
        }
 
        conn := &archived.GiteeConnection{}
-       v := config.GetConfig()
-       encKey := v.GetString(core.EncodeKeyEnvStr)
+       encKey := baseRes.GetConfig(core.EncodeKeyEnvStr)
 
        conn.Name = "init gitee connection"
        conn.ID = 1
-       conn.Endpoint = v.GetString("GITEE_ENDPOINT")
-       conn.Token, err = core.Encrypt(encKey, v.GetString("GITEE_AUTH"))
+       conn.Endpoint = baseRes.GetConfig("GITEE_ENDPOINT")
+       conn.Token, err = core.Encrypt(encKey, baseRes.GetConfig("GITEE_AUTH"))
        if err != nil {
-               return errors.Convert(err)
+               return err
        }
-       conn.Proxy = v.GetString("GITEE_PROXY")
-       conn.RateLimitPerHour = v.GetInt("GITEE_API_REQUESTS_PER_HOUR")
+       conn.Proxy = baseRes.GetConfig("GITEE_PROXY")
 
-       err = db.Clauses(clause.OnConflict{DoNothing: true}).Create(conn).Error
+       var err1 error
+       conn.RateLimitPerHour, err1 = 
strconv.Atoi(baseRes.GetConfig("GITEE_API_REQUESTS_PER_HOUR"))
+       if err1 != nil {
+               conn.RateLimitPerHour = 1000
+       }
 
+       err = db.CreateIfNotExist(conn)
        if err != nil {
-               return errors.Convert(err)
+               return err
        }
        return nil
 }
diff --git a/plugins/gitee/models/migrationscripts/register.go 
b/plugins/gitee/models/migrationscripts/register.go
index c1365f7d4..c0c766cd6 100644
--- a/plugins/gitee/models/migrationscripts/register.go
+++ b/plugins/gitee/models/migrationscripts/register.go
@@ -18,12 +18,12 @@ limitations under the License.
 package migrationscripts
 
 import (
-       "github.com/apache/incubator-devlake/migration"
+       "github.com/apache/incubator-devlake/plugins/core"
 )
 
 // All return all the migration scripts
-func All() []migration.Script {
-       return []migration.Script{
+func All() []core.MigrationScript {
+       return []core.MigrationScript{
                new(addInitTables),
        }
 }

Reply via email to