klesh commented on code in PR #3471:
URL: https://github.com/apache/incubator-devlake/pull/3471#discussion_r999057282


##########
plugins/jira/models/migrationscripts/20220716_add_init_tables.go:
##########
@@ -90,94 +87,54 @@ func (*addInitTables) Up(ctx context.Context, db *gorm.DB) 
errors.Error {
                &archived.JiraSprintIssue{},
                &archived.JiraWorklog{},
        ); err != nil {
-               return errors.Convert(err)
-       }
-
-       // In order to avoid postgres reporting "cached plan must not change 
result type",
-       // we have to avoid loading the _tool_jira_connections table before our 
migration. The trick is to rename it before loading.
-       // so need to use JiraConnectionOld, JiraConnectionNew and 
JiraConnection to operate.
-       // step1: create _tool_jira_connections_new table
-       // step2: rename _tool_jira_connections to _tool_jira_connections_old
-       // step3: select data from _tool_jira_connections_old and insert date 
to _tool_jira_connections_new
-       // step4: rename _tool_jira_connections_new to _tool_jira_connections
-
-       // step1
-       if err = db.Migrator().CreateTable(&JiraConnectionNew{}); err != nil {
-               return errors.Convert(err)
-       }
-       //nolint:errcheck
-       defer db.Migrator().DropTable(&JiraConnectionNew{})
-
-       // step2
-       if err = db.Migrator().RenameTable("_tool_jira_connections", 
"_tool_jira_connections_old"); err != nil {
-               return errors.Convert(err)
-       }
-       defer func() {
-               if err != nil {
-                       err = 
db.Migrator().RenameTable("_tool_jira_connections_old", 
"_tool_jira_connections")
-               } else {
-                       err = db.Migrator().DropTable(&JiraConnectionV011Old{})
-               }
-       }()
-
-       // step3
-       var result *gorm.DB
-       var jiraConns []JiraConnectionV011Old
-       result = db.Find(&jiraConns)
-       if result.Error != nil {
-               return errors.Convert(result.Error)
+               return err
        }
-
-       for _, v := range jiraConns {
-               conn := &JiraConnectionNew{}
-               conn.ID = v.ID
-               conn.Name = v.Name
-               conn.Endpoint = v.Endpoint
-               conn.Proxy = v.Proxy
-               conn.RateLimitPerHour = v.RateLimit
-
-               c := config.GetConfig()
-               encKey := c.GetString(core.EncodeKeyEnvStr)
-               if encKey == "" {
-                       return errors.BadInput.New("jira v0.11 invalid encKey")
-               }
-               var auth string
-               if auth, err = core.Decrypt(encKey, v.BasicAuthEncoded); err != 
nil {
-                       return errors.Convert(err)
-               }
-               var pk []byte
-               pk, err = base64.StdEncoding.DecodeString(auth)
-               if err != nil {
-                       return errors.Convert(err)
-               }
-               originInfo := strings.Split(string(pk), ":")
-               if len(originInfo) == 2 {
-                       conn.Username = originInfo[0]
-                       conn.Password, err = core.Encrypt(encKey, originInfo[1])
-                       if err != nil {
-                               return errors.Convert(err)
+       err = migrationhelper.TransformTable(
+               basicRes,
+               script,
+               "_tool_jira_connections",
+               func(old *jiraConnectionV011Old) (*jiraConnection20220716, 
errors.Error) {

Review Comment:
   Can we use `jiraConnection20220716Before` and `jiraConnection20220716After`, 
I think they are more descriptive. What do you think?



##########
plugins/jira/models/migrationscripts/20220716_add_init_tables.go:
##########
@@ -90,94 +87,54 @@ func (*addInitTables) Up(ctx context.Context, db *gorm.DB) 
errors.Error {
                &archived.JiraSprintIssue{},
                &archived.JiraWorklog{},
        ); err != nil {
-               return errors.Convert(err)
-       }
-
-       // In order to avoid postgres reporting "cached plan must not change 
result type",
-       // we have to avoid loading the _tool_jira_connections table before our 
migration. The trick is to rename it before loading.
-       // so need to use JiraConnectionOld, JiraConnectionNew and 
JiraConnection to operate.
-       // step1: create _tool_jira_connections_new table
-       // step2: rename _tool_jira_connections to _tool_jira_connections_old
-       // step3: select data from _tool_jira_connections_old and insert date 
to _tool_jira_connections_new
-       // step4: rename _tool_jira_connections_new to _tool_jira_connections
-
-       // step1
-       if err = db.Migrator().CreateTable(&JiraConnectionNew{}); err != nil {
-               return errors.Convert(err)
-       }
-       //nolint:errcheck
-       defer db.Migrator().DropTable(&JiraConnectionNew{})
-
-       // step2
-       if err = db.Migrator().RenameTable("_tool_jira_connections", 
"_tool_jira_connections_old"); err != nil {
-               return errors.Convert(err)
-       }
-       defer func() {
-               if err != nil {
-                       err = 
db.Migrator().RenameTable("_tool_jira_connections_old", 
"_tool_jira_connections")
-               } else {
-                       err = db.Migrator().DropTable(&JiraConnectionV011Old{})
-               }
-       }()
-
-       // step3
-       var result *gorm.DB
-       var jiraConns []JiraConnectionV011Old
-       result = db.Find(&jiraConns)
-       if result.Error != nil {
-               return errors.Convert(result.Error)
+               return err
        }
-
-       for _, v := range jiraConns {
-               conn := &JiraConnectionNew{}
-               conn.ID = v.ID
-               conn.Name = v.Name
-               conn.Endpoint = v.Endpoint
-               conn.Proxy = v.Proxy
-               conn.RateLimitPerHour = v.RateLimit
-
-               c := config.GetConfig()
-               encKey := c.GetString(core.EncodeKeyEnvStr)
-               if encKey == "" {
-                       return errors.BadInput.New("jira v0.11 invalid encKey")
-               }
-               var auth string
-               if auth, err = core.Decrypt(encKey, v.BasicAuthEncoded); err != 
nil {
-                       return errors.Convert(err)
-               }
-               var pk []byte
-               pk, err = base64.StdEncoding.DecodeString(auth)
-               if err != nil {
-                       return errors.Convert(err)
-               }
-               originInfo := strings.Split(string(pk), ":")
-               if len(originInfo) == 2 {
-                       conn.Username = originInfo[0]
-                       conn.Password, err = core.Encrypt(encKey, originInfo[1])
-                       if err != nil {
-                               return errors.Convert(err)
+       err = migrationhelper.TransformTable(
+               basicRes,
+               script,
+               "_tool_jira_connections",
+               func(old *jiraConnectionV011Old) (*jiraConnection20220716, 
errors.Error) {
+                       conn := &jiraConnection20220716{}
+                       conn.ID = old.ID
+                       conn.Name = old.Name
+                       conn.Endpoint = old.Endpoint
+                       conn.Proxy = old.Proxy
+                       conn.RateLimitPerHour = old.RateLimit
+
+                       c := config.GetConfig()
+                       encKey := c.GetString(core.EncodeKeyEnvStr)

Review Comment:
   Please move 104~107 to the outer function so we check it only once.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to