You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by GitBox <gi...@apache.org> on 2022/09/22 15:29:00 UTC

[GitHub] [incubator-devlake] mappjzc opened a new pull request, #3169: refactor: migration helper

mappjzc opened a new pull request, #3169:
URL: https://github.com/apache/incubator-devlake/pull/3169

   # Summary
   Add ReBuildTable
   Add ReBuildTableWithOutBak
   <!--
   Thanks for submitting a pull request!
   
   We appreciate you spending the time to work on these changes.
   Please fill out as many sections below as possible.
   -->
   
   ### Does this close any open issues?
   Closes #3158 
   
   ### Screenshots
   Include any relevant screenshots here.
   
   ### Other Information
   Any other information that is important to this PR.
   


-- 
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: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-devlake] klesh commented on a diff in pull request #3169: refactor: migration helper

Posted by GitBox <gi...@apache.org>.
klesh commented on code in PR #3169:
URL: https://github.com/apache/incubator-devlake/pull/3169#discussion_r978250978


##########
helpers/migrationhelper/helper.go:
##########
@@ -0,0 +1,185 @@
+/*
+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 migrationhelper
+
+import (
+	"fmt"
+	"reflect"
+
+	"github.com/apache/incubator-devlake/errors"
+	"github.com/apache/incubator-devlake/plugins/gitlab/api"
+	"github.com/apache/incubator-devlake/plugins/helper"
+	"gorm.io/gorm"
+	"gorm.io/gorm/schema"
+)
+
+// ReBuildTable method can be used when we need to change the table structure and reprocess all the data in the table.
+func ReBuildTable(db *gorm.DB, old schema.Tabler, oldBak schema.Tabler, new schema.Tabler, callback_transform func(old schema.Tabler) schema.Tabler) (errs errors.Error) {
+	var err error
+
+	// param cheking
+	errs = paramCheckingForReBuildTable(db, old, oldBak, new, callback_transform)
+	if errs != nil {
+		return errors.Default.Wrap(errs, "ReBuildTable param cheking error")
+	}
+
+	// rename the old to oldBak for cache old table
+	err = db.Migrator().RenameTable(old, oldBak)
+	if err != nil {
+		return errors.Default.Wrap(err, fmt.Sprintf("error no rename [%s] to [%s]", old.TableName(), oldBak.TableName()))
+	}
+
+	// rollback for rename back
+	defer func() {
+		if errs != nil {
+			err = db.Migrator().RenameTable(oldBak, old)
+			if err != nil {
+				errs = errors.Default.Wrap(err, fmt.Sprintf("fail to rollback table [%s] , you must to rollback by yourself. %s", oldBak.TableName(), err.Error()))
+			}
+		}
+	}()
+
+	return ReBuildTableWithOutBak(db, oldBak, new, callback_transform)
+}
+
+// ReBuildTableWithOutBak method can be used when we need to change the table structure and reprocess all the data in the table.
+// It request the old table and the new table with different table name.
+func ReBuildTableWithOutBak(db *gorm.DB, old schema.Tabler, new schema.Tabler, callback_transform func(old schema.Tabler) schema.Tabler) (errs errors.Error) {

Review Comment:
   How about `TransformRowsBetweenTables`
   `old` and `new` can be renamed to `src` and `dest`



##########
helpers/migrationhelper/helper.go:
##########
@@ -0,0 +1,185 @@
+/*
+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 migrationhelper
+
+import (
+	"fmt"
+	"reflect"
+
+	"github.com/apache/incubator-devlake/errors"
+	"github.com/apache/incubator-devlake/plugins/gitlab/api"
+	"github.com/apache/incubator-devlake/plugins/helper"
+	"gorm.io/gorm"
+	"gorm.io/gorm/schema"
+)
+
+// ReBuildTable method can be used when we need to change the table structure and reprocess all the data in the table.
+func ReBuildTable(db *gorm.DB, old schema.Tabler, oldBak schema.Tabler, new schema.Tabler, callback_transform func(old schema.Tabler) schema.Tabler) (errs errors.Error) {

Review Comment:
   How about we rename this to `TransformRowsInPlace`?
   For `oldBak`, seems like we need only the name, can we replace it with a plain string? and I think it would be better, if we generate the `bak table name` by hashing the `script.name + script.version` automatically rather than letting the caller pass it in directly.



-- 
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: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-devlake] klesh commented on pull request #3169: refactor: migration helper

Posted by GitBox <gi...@apache.org>.
klesh commented on PR #3169:
URL: https://github.com/apache/incubator-devlake/pull/3169#issuecomment-1272240736

   Don't forget to update the document afterward.


-- 
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: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-devlake] klesh merged pull request #3169: refactor: migration helper

Posted by GitBox <gi...@apache.org>.
klesh merged PR #3169:
URL: https://github.com/apache/incubator-devlake/pull/3169


-- 
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: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org