@@ -18,6 +18,7 @@ package workflowStatusLatest
18
18
19
19
import (
20
20
"fmt"
21
+ util2 "github.com/devtron-labs/devtron/internal/util"
21
22
"time"
22
23
23
24
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
@@ -83,18 +84,35 @@ func (impl *WorkflowStatusLatestServiceImpl) SaveCiWorkflowStatusLatest(tx *pg.T
83
84
return fmt .Errorf ("invalid ciWorkflowId: %d" , ciWorkflowId )
84
85
}
85
86
86
- now := time . Now ()
87
- model := & pipelineConfig. CiWorkflowStatusLatest {
88
- PipelineId : pipelineId ,
89
- AppId : appId ,
90
- CiWorkflowId : ciWorkflowId ,
87
+ // Check if entry exists
88
+ existingEntry , err := impl . workflowStatusLatestRepository . GetCiWorkflowStatusLatestByPipelineId ( pipelineId )
89
+ if err != nil && ! util2 . IsErrNoRows ( err ) {
90
+ impl . logger . Errorw ( "error in getting ci workflow status latest" , "err" , err , "pipelineId" , pipelineId )
91
+ return err
91
92
}
92
- model .CreatedBy = userId
93
- model .CreatedOn = now
94
- model .UpdatedBy = userId
95
- model .UpdatedOn = now
96
93
97
- return impl .workflowStatusLatestRepository .SaveCiWorkflowStatusLatest (tx , model )
94
+ now := time .Now ()
95
+ if util2 .IsErrNoRows (err ) {
96
+ // Create new entry
97
+ model := & pipelineConfig.CiWorkflowStatusLatest {
98
+ PipelineId : pipelineId ,
99
+ AppId : appId ,
100
+ CiWorkflowId : ciWorkflowId ,
101
+ }
102
+ model .CreatedBy = userId
103
+ model .CreatedOn = now
104
+ model .UpdatedBy = userId
105
+ model .UpdatedOn = now
106
+
107
+ return impl .workflowStatusLatestRepository .SaveCiWorkflowStatusLatest (tx , model )
108
+ } else {
109
+ // Update existing entry with latest workflow ID
110
+ existingEntry .CiWorkflowId = ciWorkflowId
111
+ existingEntry .UpdatedBy = userId
112
+ existingEntry .UpdatedOn = now
113
+
114
+ return impl .workflowStatusLatestRepository .UpdateCiWorkflowStatusLatest (tx , existingEntry )
115
+ }
98
116
}
99
117
100
118
func (impl * WorkflowStatusLatestServiceImpl ) GetCiWorkflowStatusLatestByPipelineIds (pipelineIds []int ) ([]* pipelineConfig.CiWorkflowStatusLatest , error ) {
@@ -109,21 +127,37 @@ func (impl *WorkflowStatusLatestServiceImpl) SaveCdWorkflowStatusLatest(tx *pg.T
109
127
return fmt .Errorf ("invalid workflowRunnerId: %d" , workflowRunnerId )
110
128
}
111
129
112
- // Create new entry (always save, don't update)
113
- now := time .Now ()
114
- model := & pipelineConfig.CdWorkflowStatusLatest {
115
- PipelineId : pipelineId ,
116
- AppId : appId ,
117
- EnvironmentId : environmentId ,
118
- WorkflowType : workflowType ,
119
- WorkflowRunnerId : workflowRunnerId ,
130
+ // Check if entry exists
131
+ existingEntry , err := impl .workflowStatusLatestRepository .GetCdWorkflowStatusLatestByPipelineIdAndWorkflowType (tx , pipelineId , workflowType )
132
+ if err != nil && err != pg .ErrNoRows {
133
+ impl .logger .Errorw ("error in getting cd workflow status latest" , "err" , err , "pipelineId" , pipelineId , "workflowType" , workflowType )
134
+ return err
120
135
}
121
- model .CreatedBy = userId
122
- model .CreatedOn = now
123
- model .UpdatedBy = userId
124
- model .UpdatedOn = now
125
136
126
- return impl .workflowStatusLatestRepository .SaveCdWorkflowStatusLatest (tx , model )
137
+ now := time .Now ()
138
+ if err == pg .ErrNoRows {
139
+ // Create new entry
140
+ model := & pipelineConfig.CdWorkflowStatusLatest {
141
+ PipelineId : pipelineId ,
142
+ AppId : appId ,
143
+ EnvironmentId : environmentId ,
144
+ WorkflowType : workflowType ,
145
+ WorkflowRunnerId : workflowRunnerId ,
146
+ }
147
+ model .CreatedBy = userId
148
+ model .CreatedOn = now
149
+ model .UpdatedBy = userId
150
+ model .UpdatedOn = now
151
+
152
+ return impl .workflowStatusLatestRepository .SaveCdWorkflowStatusLatest (tx , model )
153
+ } else {
154
+ // Update existing entry with latest workflow runner ID
155
+ existingEntry .WorkflowRunnerId = workflowRunnerId
156
+ existingEntry .UpdatedBy = userId
157
+ existingEntry .UpdatedOn = now
158
+
159
+ return impl .workflowStatusLatestRepository .UpdateCdWorkflowStatusLatest (tx , existingEntry )
160
+ }
127
161
}
128
162
129
163
func (impl * WorkflowStatusLatestServiceImpl ) GetCdWorkflowLatestByPipelineIds (pipelineIds []int ) ([]* CdWorkflowStatusLatest , error ) {
0 commit comments