You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ab...@apache.org on 2023/12/19 08:03:15 UTC

(incubator-devlake) branch main updated: fix(fetch): fix wrong use of db.Fetch (#6666)

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

abeizn 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 49f276f55 fix(fetch): fix wrong use of db.Fetch (#6666)
49f276f55 is described below

commit 49f276f558a2ea7124dfdc680bccbef27bf0b581
Author: Lynwee <li...@merico.dev>
AuthorDate: Tue Dec 19 16:03:10 2023 +0800

    fix(fetch): fix wrong use of db.Fetch (#6666)
---
 .../20230223_remove_createddateafter_from_collector_state.go       | 4 ++--
 .../core/models/migrationscripts/20230829_normalize_bp_settings.go | 4 ++--
 backend/helpers/migrationhelper/migrationhelper.go                 | 2 +-
 backend/helpers/pluginhelper/api/api_extractor.go                  | 3 +--
 backend/helpers/pluginhelper/api/graphql_collector.go              | 4 ++--
 backend/plugins/ae/tasks/commits_convertor.go                      | 7 ++-----
 backend/plugins/refdiff/tasks/commit_diff_calculator.go            | 2 +-
 backend/plugins/refdiff/tasks/deployment_commit_diff_calculator.go | 2 +-
 backend/plugins/refdiff/tasks/refs_pr_cherry_pick_calculator.go    | 3 +--
 backend/server/services/pipeline_runner.go                         | 4 ++--
 10 files changed, 15 insertions(+), 20 deletions(-)

diff --git a/backend/core/models/migrationscripts/20230223_remove_createddateafter_from_collector_state.go b/backend/core/models/migrationscripts/20230223_remove_createddateafter_from_collector_state.go
index 0570f43c2..db5fc2755 100644
--- a/backend/core/models/migrationscripts/20230223_remove_createddateafter_from_collector_state.go
+++ b/backend/core/models/migrationscripts/20230223_remove_createddateafter_from_collector_state.go
@@ -40,13 +40,13 @@ type removeCreatedDateAfterFromCollectorMeta20230223 struct{}
 func (script *removeCreatedDateAfterFromCollectorMeta20230223) Up(basicRes context.BasicRes) errors.Error {
 	db := basicRes.GetDal()
 	// step 1: rename bp.settings.createdDateAfter to timeAfter
-	bp := &blueprint20230223{}
-	cursor, err := db.Cursor(dal.From(bp), dal.Where("mode = ?", "NORMAL"))
+	cursor, err := db.Cursor(dal.From(&blueprint20230223{}), dal.Where("mode = ?", "NORMAL"))
 	if err != nil {
 		return err
 	}
 	defer cursor.Close()
 	for cursor.Next() {
+		bp := &blueprint20230223{}
 		err = db.Fetch(cursor, bp)
 		if err != nil {
 			return err
diff --git a/backend/core/models/migrationscripts/20230829_normalize_bp_settings.go b/backend/core/models/migrationscripts/20230829_normalize_bp_settings.go
index 99dd3b07a..5b5fc044d 100644
--- a/backend/core/models/migrationscripts/20230829_normalize_bp_settings.go
+++ b/backend/core/models/migrationscripts/20230829_normalize_bp_settings.go
@@ -73,12 +73,12 @@ func (script *normalizeBpSettings) Up(basicRes context.BasicRes) errors.Error {
 		return errors.BadInput.New("invalid encKey")
 	}
 	db := basicRes.GetDal()
-	bp := &blueprint20230829{}
-	cursor := errors.Must1(db.Cursor(dal.From(bp)))
+	cursor := errors.Must1(db.Cursor(dal.From(&blueprint20230829{})))
 	defer cursor.Close()
 
 	for cursor.Next() {
 		// load row
+		bp := &blueprint20230829{}
 		errors.Must(db.Fetch(cursor, bp))
 		// decrypt and unmarshal settings
 		settingsJson := errors.Must1(plugin.Decrypt(encKey, bp.Settings))
diff --git a/backend/helpers/migrationhelper/migrationhelper.go b/backend/helpers/migrationhelper/migrationhelper.go
index b1215ac3b..2625109dd 100644
--- a/backend/helpers/migrationhelper/migrationhelper.go
+++ b/backend/helpers/migrationhelper/migrationhelper.go
@@ -155,8 +155,8 @@ func TransformColumns[S any, D any](
 				return errors.Default.Wrap(err, fmt.Sprintf("failed to instantiate BatchSave for table [%s]", tableName))
 			}
 			defer batch.Close()
-			src := new(S)
 			for cursor.Next() {
+				src := new(S)
 				err = db.Fetch(cursor, src)
 				if err != nil {
 					return errors.Default.Wrap(err, fmt.Sprintf("fail to load record from table [%s]", tableName))
diff --git a/backend/helpers/pluginhelper/api/api_extractor.go b/backend/helpers/pluginhelper/api/api_extractor.go
index a2cd8a1eb..aa13f6d5d 100644
--- a/backend/helpers/pluginhelper/api/api_extractor.go
+++ b/backend/helpers/pluginhelper/api/api_extractor.go
@@ -93,8 +93,6 @@ func (extractor *ApiExtractor) Execute() errors.Error {
 	}
 	logger.Info("get data from %s where params=%s and got %d", extractor.table, extractor.params, count)
 	defer cursor.Close()
-	row := &RawData{}
-
 	// batch save divider
 	divider := NewBatchSaveDivider(extractor.args.Ctx, extractor.args.BatchSize, extractor.table, extractor.params)
 
@@ -108,6 +106,7 @@ func (extractor *ApiExtractor) Execute() errors.Error {
 			return errors.Convert(ctx.Err())
 		default:
 		}
+		row := &RawData{}
 		err = db.Fetch(cursor, row)
 		if err != nil {
 			return errors.Default.Wrap(err, "error fetching row")
diff --git a/backend/helpers/pluginhelper/api/graphql_collector.go b/backend/helpers/pluginhelper/api/graphql_collector.go
index bf750409e..122e1a575 100644
--- a/backend/helpers/pluginhelper/api/graphql_collector.go
+++ b/backend/helpers/pluginhelper/api/graphql_collector.go
@@ -309,9 +309,8 @@ func (collector *GraphqlCollector) ExtractExistRawData(divider *BatchSaveDivider
 	}
 	logger.Info("get data from %s where params=%s and got %d", collector.table, collector.params, count)
 	defer cursor.Close()
-	row := &RawData{}
 
-	// prgress
+	// progress
 	collector.args.Ctx.SetProgress(0, -1)
 	ctx := collector.args.Ctx.GetContext()
 	// iterate all rows
@@ -323,6 +322,7 @@ func (collector *GraphqlCollector) ExtractExistRawData(divider *BatchSaveDivider
 		}
 		// get the type of query and variables. For each iteration, the query should be a different object
 		query, variables, _ := collector.args.BuildQuery(nil)
+		row := &RawData{}
 		err = db.Fetch(cursor, row)
 		if err != nil {
 			return errors.Default.Wrap(err, "error fetching row")
diff --git a/backend/plugins/ae/tasks/commits_convertor.go b/backend/plugins/ae/tasks/commits_convertor.go
index 007d0d177..600186581 100644
--- a/backend/plugins/ae/tasks/commits_convertor.go
+++ b/backend/plugins/ae/tasks/commits_convertor.go
@@ -28,12 +28,9 @@ import (
 func ConvertCommits(taskCtx plugin.SubTaskContext) errors.Error {
 	db := taskCtx.GetDal()
 	data := taskCtx.GetData().(*AeTaskData)
-
-	aeCommit := &aeModels.AECommit{}
-
 	// Get all the commits from the domain layer
 	cursor, err := db.Cursor(
-		dal.From(aeCommit),
+		dal.From(&aeModels.AECommit{}),
 		dal.Where("ae_project_id = ?", data.Options.ProjectId),
 	)
 	if err != nil {
@@ -53,7 +50,7 @@ func ConvertCommits(taskCtx plugin.SubTaskContext) errors.Error {
 		}
 		// uncomment following line if you want to test out canceling feature for this task
 		//time.Sleep(1 * time.Second)
-
+		aeCommit := &aeModels.AECommit{}
 		err = db.Fetch(cursor, aeCommit)
 		if err != nil {
 			return err
diff --git a/backend/plugins/refdiff/tasks/commit_diff_calculator.go b/backend/plugins/refdiff/tasks/commit_diff_calculator.go
index 678fdcbc6..bfc14151f 100644
--- a/backend/plugins/refdiff/tasks/commit_diff_calculator.go
+++ b/backend/plugins/refdiff/tasks/commit_diff_calculator.go
@@ -76,7 +76,6 @@ func CalculateCommitsDiff(taskCtx plugin.SubTaskContext) errors.Error {
 	insertCountLimitOfCommitsDiff := int(65535 / reflect.ValueOf(code.CommitsDiff{}).NumField())
 
 	// load commits from db
-	commitParent := &code.CommitParent{}
 	cursor, err := db.Cursor(
 		dal.Select("cp.*"),
 		dal.Join("LEFT JOIN repo_commits rc ON (rc.commit_sha = cp.commit_sha)"),
@@ -94,6 +93,7 @@ func CalculateCommitsDiff(taskCtx plugin.SubTaskContext) errors.Error {
 			return errors.Convert(ctx.Err())
 		default:
 		}
+		commitParent := &code.CommitParent{}
 		err = db.Fetch(cursor, commitParent)
 		if err != nil {
 			return errors.Default.Wrap(err, "failed to read commit from database")
diff --git a/backend/plugins/refdiff/tasks/deployment_commit_diff_calculator.go b/backend/plugins/refdiff/tasks/deployment_commit_diff_calculator.go
index 841cdb288..0887f8920 100644
--- a/backend/plugins/refdiff/tasks/deployment_commit_diff_calculator.go
+++ b/backend/plugins/refdiff/tasks/deployment_commit_diff_calculator.go
@@ -156,13 +156,13 @@ func loadCommitGraph(ctx context.Context, db dal.Dal, data *RefdiffTaskData) (*u
 	}
 	defer cursor.Close()
 
-	commitParent := &code.CommitParent{}
 	for cursor.Next() {
 		select {
 		case <-ctx.Done():
 			return nil, errors.Convert(ctx.Err())
 		default:
 		}
+		commitParent := &code.CommitParent{}
 		err = db.Fetch(cursor, commitParent)
 		if err != nil {
 			return nil, err
diff --git a/backend/plugins/refdiff/tasks/refs_pr_cherry_pick_calculator.go b/backend/plugins/refdiff/tasks/refs_pr_cherry_pick_calculator.go
index c9dda30fe..a3242cdf6 100644
--- a/backend/plugins/refdiff/tasks/refs_pr_cherry_pick_calculator.go
+++ b/backend/plugins/refdiff/tasks/refs_pr_cherry_pick_calculator.go
@@ -68,7 +68,6 @@ func CalculatePrCherryPick(taskCtx plugin.SubTaskContext) errors.Error {
 
 	defer cursor.Close()
 
-	pr := &code.PullRequest{}
 	var parentPrKeyInt int
 	taskCtx.SetProgress(0, -1)
 
@@ -79,7 +78,7 @@ func CalculatePrCherryPick(taskCtx plugin.SubTaskContext) errors.Error {
 			return errors.Convert(ctx.Err())
 		default:
 		}
-
+		pr := &code.PullRequest{}
 		err = db.Fetch(cursor, pr)
 		if err != nil {
 			return errors.Convert(err)
diff --git a/backend/server/services/pipeline_runner.go b/backend/server/services/pipeline_runner.go
index 8bd51d37d..b16dc2dc3 100644
--- a/backend/server/services/pipeline_runner.go
+++ b/backend/server/services/pipeline_runner.go
@@ -145,9 +145,8 @@ func ComputePipelineStatus(pipeline *models.Pipeline, isCancelled bool) (string,
 
 // GetLatestTasksOfPipeline returns latest tasks (reran tasks are excluding) of specified pipeline
 func GetLatestTasksOfPipeline(pipeline *models.Pipeline) ([]*models.Task, errors.Error) {
-	task := &models.Task{}
 	cursor, err := db.Cursor(
-		dal.From(task),
+		dal.From(&models.Task{}),
 		dal.Where("pipeline_id = ?", pipeline.ID),
 		dal.Orderby("id DESC"), // sort it by id so we can hit the latest task first for the RERUNed row/col
 	)
@@ -160,6 +159,7 @@ func GetLatestTasksOfPipeline(pipeline *models.Pipeline) ([]*models.Task, errors
 	type rowcol struct{ row, col int }
 	memorized := make(map[rowcol]bool)
 	for cursor.Next() {
+		task := &models.Task{}
 		if e := db.Fetch(cursor, task); e != nil {
 			return nil, errors.Convert(e)
 		}