From 577f7178516e80f46faa1b33b5540043ed7a4bf9 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 cae1f4e376..cb6b8837bd 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 @@ -810,7 +812,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 a6af9f439c73121c825015bfcf97e7d377610071 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 cb6b8837bd..686eb7279f 100644 --- a/sqle/model/workflow.go +++ b/sqle/model/workflow.go @@ -826,7 +826,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 { @@ -844,7 +845,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)