From 9b17bb2d0649721acc3c283a0e9b47460604811e Mon Sep 17 00:00:00 2001 From: taolx0 Date: Mon, 7 Aug 2023 14:39:50 +0800 Subject: [PATCH 1/2] refactor:extract scoped func --- sqle/model/workflow.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sqle/model/workflow.go b/sqle/model/workflow.go index 304c7d9716..bd123bbd6c 100644 --- a/sqle/model/workflow.go +++ b/sqle/model/workflow.go @@ -10,6 +10,8 @@ import ( "github.com/jinzhu/gorm" ) +var UnScopedFunc = func(db *gorm.DB) *gorm.DB { return db.Unscoped() } + type WorkflowTemplate struct { Model Name string @@ -789,7 +791,7 @@ func (s *Storage) GetWorkflowDetailById(id string) (*Workflow, bool, error) { func (s *Storage) GetWorkflowExportById(id string) (*Workflow, bool, error) { w := new(Workflow) - err := s.db.Preload("CreateUser", func(db *gorm.DB) *gorm.DB { return db.Unscoped() }). + err := s.db.Preload("CreateUser", UnScopedFunc). Preload("Record").Where("id = ?", id).First(&w).Error if err == gorm.ErrRecordNotFound { return nil, false, nil From 4bb063ead864883ef00aa55af89166f4f6a0803a Mon Sep 17 00:00:00 2001 From: taolx0 Date: Mon, 7 Aug 2023 14:41:03 +0800 Subject: [PATCH 2/2] chore:fix scopedFunc --- sqle/model/workflow.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sqle/model/workflow.go b/sqle/model/workflow.go index bd123bbd6c..8d6b188f7b 100644 --- a/sqle/model/workflow.go +++ b/sqle/model/workflow.go @@ -805,7 +805,8 @@ func (s *Storage) GetWorkflowExportById(id string) (*Workflow, bool, error) { } instanceRecordList := make([]*WorkflowInstanceRecord, 0) - err = s.db.Preload("Instance").Preload("Task").Preload("User"). + err = s.db.Preload("Instance", UnScopedFunc).Preload("Task"). + Preload("User", UnScopedFunc). Where("workflow_record_id = ?", w.Record.ID). Find(&instanceRecordList).Error if err != nil { @@ -823,7 +824,7 @@ func (s *Storage) GetWorkflowExportById(id string) (*Workflow, bool, error) { steps := make([]*WorkflowStep, 0) err = s.db.Where("workflow_record_id = ?", w.Record.ID). - Preload("OperationUser"). + Preload("OperationUser", UnScopedFunc). Find(&steps).Error if err != nil { return nil, false, errors.New(errors.ConnectStorageError, err)